Lecture 2: Static Methods, if statements, homework uploader Primitive Review • Suppose we have double x = 4; double y = 3; x = y + x; y = y*2; • what are the final values of x and y ? Intro to Classes in Java • Classes in Java come in two forms: – classes that package statements into static methods used to perform a routine computation (such as the Math class) • or applications such as MathTrick.java which have – public static void main(String [] args) – classes that are used to make new data types that go beyond the limits of the primitive variables we have used so far. These classes create objects (more sophisticated data variables) that we can manipulate in our program. Today: Math class • Java's Math class is of the first type. – contains static methods for doing math – use the class name in front of each method – javadoc for Math class – google "java math class" – the expression Math.sqrt(x) • computes the square-root of the number currently in x • suppose y = 9 then Math.sqrt(y) evaluates to 3 • the expression Math.sqrt(y) is replaced by the value 3 Math.sqrt() takes an argument • When you say Math.sqrt( radius ) – the variable radius is the "argument" of sqrt method – Math.sqrt( 24); now 24 is the argument – this is the value it will find the square root of • Arguments can be numbers, variables, or expressions. suppose y = 16. Find – Math.sqrt( 16); – Math.sqrt( 4*y ); – Math.sqrt( y + 9 ); Storing or Printing the result • STORE: – To store the result of a Math.sqrt() expression – assign the expression to another variable – y = Math.sqrt(x); // now y gets the 3 – then you can use the result for more computation • PRINT: – Enclose the expression in a print statement: – System.out.println( Math.sqrt(x)); Example – Pythagorean Theorem double width, height, hypotenuse; // sides of a triangle width = 42.0; height = 17.0; hypotenuse = Math.sqrt( width*width + height*height ); System.out.print("A triangle with sides 42 and 17 has hypotenuse "); System.out.println(hypotenuse); Other Math static methods • abs(double a) – Returns the absolute value of a double value. • cos(double a) – Returns the trigonometric cosine of an angle. • exp(double a) – Returns Euler's number e raised to the power of a double value. • random() – Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Math.random() takes no argument • When you say Math.sqrt( radius ) – the variable radius is the "argument" of sqrt method • Math.random() doesn't need anything in () – it always gives a random value between 0 and 1 System.out.print("\nHere is a random #: "); System.out.println( Math.random() ); Code BLOCKS • In Java we use { } to make code "blocks" • Examples: Let's "trace" that last example Basic If Statement • Simplest if uses a condition test to determine whether to execute a code block • example Let's see what this does to x and y • suppose x = 4 and y = 2 • now x = ? and y = ? • what if x was 3 and y was 9 originally? Other Boolean expressions • Every if needs a boolean expression to test whether it should do the code block • In addition to ">" there are a total of 6 comparison operators: <, >, <=, >=, !=, == Comparison Operators Try these: determine True or False • • • • • • • Assume x = 18, y = 20, z = 4 (x < y ) (z > 6 ) (y < z ) (x != y) (z == 4) (y != x ) More if variations • if can also be used with else to choose between one of two possible code blocks: • if the boolean is true we do the first block • otherwise (else) we skip it and do the second block Example Multi-way if-else • A final option is to put another if statement right after "else" if ( score > 90 ) { System.out.println("Grade is A"); } else if (score > 80) { System.out.println("Grade is B"); } else { No Boolean here. Default for "none of above" System.out.println("Grade is C"); } Lab 2 • Create Lab2 project in BlueJ • We experiment with – generating random numbers – using if and if-else to simulate coin toss • heads, tails, edge, fall off table • compare scores from two coin tosses • Turn in procedure. When finished... – Project>Create Jar File, "include source files", Lab2 – Click "Homework Uploader" link – Enter passcode, estimated score, browse to Lab2.jar – then click upload