cheat sheet java
inheritance syntax
A subclass can be inherited into another class thereby creating aninheritance hierarchy. Example: public class Student { ... } public class GraduateStudent extends Student { ... } public class InternationalGradStudentextends GraduateStudent { ... }
random digit 0-100
(int)(Math.random( ) * 100) +);
case and break
-
Absolute value a
Math.abs(a)
Square-root of a
Math.sqrt(a)
Creating Strings Syntax
String str1 = "Melody"; String str2 = new string ("Yoseph"); String str3 = str1 + " " + str2;
random 4 digit integer java
To generate a four-digits random integer, use the following :int randomFourDigitInteger = (int)(Math.random() * 10000) + 1;
Boolean *change or check this one*
boolean (condition1) = true; boolean (condition2) = false; boolean result = condition1 && condition2 return boolean;
rectangle
exam1 program
for loop syntax
for (int i = 0; i < 5, i++) { System.out.println(i); }
Write a Java program that reads a four-digit positive integer N and checks whether it is a palindrome(an integer is a palindrome if its reversed integer is equal to the given integer; e.g., 7447. Programming requirements You must read N as input and must validate whether it is a four-digit positive integer. If the userenters an incorrect value, force the user to enter the correct value (you need a while loop here). You must write a separate method that accepts an integer and returns the reverse of that integer
import java.util.Scanner; public class PalindromeChecker { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int number = getValidFourDigitNumber(scanner); if (isPalindrome(number)) { System.out.println(number + " is a palindrome."); } else { System.out.println(number + " is not a palindrome."); } } public static int getValidFourDigitNumber(Scanner scan) { int number; do { System.out.print("Enter a four-digit positive integer: "); number = scanner.nextInt(); } while (number < 1000 || number > 9999); return number; } //calling the method public static boolean isPalindrome (int number) { return number == reverse(number); } //method public static int reverse(int number) { int reversedNumber = 0; while (number != 0) { int digit = number % 10; reversedNumber = reversedNumber * 10 + digit; number /= 10; } return reversedNumber; } }
String Example
import java.util.Scanner; public class StringExample { public static void main (String[ ] args) { scanner scan = new scanner(System.in); String str1, str2; System.our.println ("Enter two strings: "); str1 = scan.nextLine ( ); str2 = scan.nextLine ( ); String str = str1 + " " str2; System.out.println (str); } }
Write a Java program that reads a positive integer N from input, computes the sum of all numbersfrom 1 to N , and prints the sum. Programming requirements - You must read N from input and must validate whether it is positive. -You must print N and the sum of all numbers from 1 to N
import java.util.Scanner; public class SumOfNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer N: "); int N = scanner.nextInt(); // Validate whether N is positive if (N <= 0) { System.out.println("Please enter a positive integer."); return; } // Compute the sum of all numbers from 1 to N int sum = 0; for (int i = 1; i <= N; i++) { sum += i; } // Print N and the sum System.out.println("N = " + N); System.out.println("Sum of all numbers from 1 to N = " + sum); scanner.close(); } }
Write a Java program that reads a set of positive integers N1, N2, . . . , Nk from input, computes andprints the sum of all these numbers. Notice that you do not know upfront how many integers you arereading, but they all must be positive integers though. So, when you enter a zero or negative integer,the program should stop adding and should then print the sum. Programming requirements You must read every Ni from input and must validate whether it is positive. You must print the sum of all positive numbers that were read.
import java.util.Scanner; public class SumOfPositiveNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum = 0; int number = 0; while (number >= 0) { System.out.print("Enter a positive integer (enter a negative integer to stop): "); number = scanner.nextInt(); // Validate whether the number is positive if (number > 0) { sum += number; } else if (number == 0) { System.out.println("Zero is neither positive nor negative."); } else { System.out.println("Stopping input. Negative integer entered."); } } // Print the sum of all positive numbers that were read System.out.println("Sum of all positive numbers: " + sum); scanner.close(); } }
user input
import scanner declare varaibles print statement to enter then scan.next- if statement
Arrays
int [ ] myArray; myArray = new int [10]; for (int i = 0, i < myArray.length; i++) { myArray[i] = i + 1; or reading 10 integers and storing them in an array (practice)
Array Elements syntax
int [ ] numbers = {1,2,3}; int firstNumber = numbers [0];
create an array with random values
int [] a = new int [n]; for (<int i = 0; i < n; i++) a [ i ] = Math.random( ) ;
do-while loop
int count = 0; do { System.out.print(count); count++; } while (count < 5); // Outputs: 01234
while loop syntax
int i = 0; while (condition like i <5) { System.out.println ( i ); i++; }
if-else statement
int num = 5; if (num > 0 ) System.out.println (" pos #:"); } else if (num < 0) { System. out.println("zero"); } else { System. out.println ("Zero"); }
Methods (defining and calling methods) syntax
public class Calculator { public int add (int a, int b) { return a + b; } } Calculator calc = new Calculator ( ); int sum = calc.add (1, 2);
object-oriented programming
public class Rectangle { int width; int height; //* Calling method? public int calculateArea( ) { return width = height; } } Rectangle rect = new Rectangle ( ); rect. width = 5; rect.height = 3; int area = rect.CalculateArea ( ); or exam 1 example:
Inheritance syntax example
public class Student { protected String name, id, major; public Student (String name, String id, String major) { this.name = name; this.id = id; this.major = major; } /** This method changes the major. */ public void changeMajor (String newMajor) { this.major = newMajor; } } Assume that the 'get' methods for all attributes are included /** This is the subclass 'GraduateStudent'. */ public class GradStudent extends Student { private String status; public String getStatus() { return status; } public GraduateStudent (String name, String id, String major){ super(name, id, major); // invoke constructor of superclass this.status = "GRAD"; } /** This method overrides superclass's method. */ public void changeMethod (String newMajor) { // does nothing and so major not changed}
create an empty string in java
string str = " "; check if string is empty using: str. isEmpty()
triangle/ check side lengths
unit 1 examples