1 / 25

Lecture 2: Static Methods, if statements, homework uploader

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:

blythe
Download Presentation

Lecture 2: Static Methods, if statements, homework uploader

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2:Static Methods, if statements, homework uploader

  2. 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 ?

  3. 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.

  4. 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

  5. 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 );

  6. 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));

  7. 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);

  8. 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.

  9. 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() );

  10. Demo Math double x = 25; double y = 10; double z; System.out.println("\f"); System.out.println( Math.sqrt(x) ); // print the square root of x to // the screen z = Math.sqrt(x); // square root of x stores in z so we can use it later System.out.println( Math.floor(z)); // print the "floor" of z // (chop off after decimal point) System.out.println("Here's a random value: " + Math.random());

  11. Code BLOCKS • In Java we use { } to make code "blocks" • Examples:

  12. Let's "trace" that last example

  13. Basic If Statement • Simplest if uses a condition test to determine whether to execute a code block • example

  14. Flow Chart view of If Statement

  15. 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?

  16. 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: <, >, <=, >=, !=, ==

  17. Comparison Operators

  18. 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 )

  19. 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

  20. Example

  21. 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"); }

  22. Demo If int score = 95; if (score >= 65){ // say whether we passed System.out.println("Pass"); } if (score >= 65){ // say pass/fail System.out.println("Pass"); } else { System.out.println("Fail"); } if (score >= 90{ // say A, B or C System.out.println("A"); } else if (score >=80) { System.out.println("B"); } else { System.out.println("C"); }

  23. 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

More Related