Java has a Math class with multiple methods. You can call a method with the following format:
Math.(method goes here)The absolute value just returns the positive value of the number. If you input an int the method returns an int. If you input a double, it returns a double.
System.out.println(Math.abs(-4));//prints out 4System.out.println(Math.abs(-4.0)); //prints out 4.0Java can also do exponents. The method takes in two parameters: the base and the exponent values. The method returns base raised to the value of the exponent. So Math.pow(2,3) returns 8.0 (this method returns a double always) as 2 * 2 * 2 = 8.
The square root method takes in a parameter and returns the square root of that number in a form of a double.
Math.sqrt(4); //returns 2.0The method Math.random() takes in no parameters but returns a double between 0.0 and .999999 (inclusive). Here is a sample question that commonly shows up on the AP exam.
What is the minimum and maximum value of x returned by the following code?
int x = (int) (Math.random()*9 - 10);A) Min: 0 Max: 2
B) Min: -10 Max: -2
C) Min: -10 Max: - 1
D) Min: -10 Max: 0
E) Min: -2 Max: -10
When you cast a double using (int), it just removes everything after the decimal, but it doesn't round.
Let's break this down. We will calculate the smallest value possible. Math.random() returns 0 as its smallest value so 0 * 9 -10 = -10.
We can eliminate A and E as their minimum value is wrong. This leaves the max. Maximum is .99999999(repeating 9) * 9 = 8.999 (largest case). 8.999 -10 = -1.001. Remove the decimal and it leaves -1. So the answer is C.
Booleans in Java are either or true or false. They are used to tell what should be done if a certain condition is met. If you want multiple conditions met you just use logical-AND - indicated by &&. If you want only one of the conditions to be met you just use logical-OR ||.
Here is a truth table. 1 indicates true for a boolean and 0 indicates false.
There are other Boolean expressions. When comparing two Strings you use the .equals method : stringA.equals(stringB).
For numbers, you would use == : intA == intB
For NOT, you would use !: A != B; !stringA.equals(stringB).
You can distribute the ! when simplifying expressions (known as DeMorgan's Law).
For example:
!(A || B) = !A && !BThe opposite of AND is OR.
You evaluate expression in the parentheses first, then evaluate AND, then OR - sort of like PEMDAS but for boolean expressions.
If- statements are code that checks a certain condition, then run the code inside the if-statement if the condition is met. else-if is used when you have multiple conditions can be met. Code in an else statement is run if all the other if and else-if statements are not met. If is used first, followed by else-if, and then else.
if(condition1){//runs if condition1 is true and then exits the entire if-else structure}else if(condition2){//runs if condition2 is true and then exits the entire if-else structure}else{//runs only condition1 and condition2 are both false }Note that, if condition1 is true and condition2 are both true, only the code in condition1 is run. Java breaks out of the if-else structure. However, if you had two separate if-statements that were true then both codes would run.
if(condition1){...}//runs if condition1 is trueif(condition2){...}//runs if condition2 is true, regardless of condition1For comparing doubles or ints use these signs:
boolean a = (5 == 5);//a = true; == checks to see if both values are equalboolean b = (5 != 5); //b = false; != checks to see if both values are NOT equalboolean c = (5 >= 6);//c = false; >= checks to see if first value is greater than or equal to //second valueboolean d = (5 > 6);//d = false; > checks to see if first value is greater than second valueboolean e = (5 <= 6);//e = true; >= checks to see if first value is less than or equal to second //valueboolean f = (5 < 6);//f = true; > checks to see if first value is less than second valueNull is simply nothing. It may seem obvious but null variables and objects can cause a ton of problems for your program:
Dog d = null;d.size();//doesn't work! can't call methods on null objectsTo check if an object is null use == or !=:
if(d != null){ //checks to see if d is not nulld.size();}A primitive can't be null.
https://github.com/aceAPCS/lab1
Download it by clicking clone or download button and then click on download as zip. The README.MD file has instructions in it. Extract the zipped folder, and in Dr. Java click on file -> open folder. Select lab1.