Lecture 2: Static Methods, if statements, homework uploader
description
Transcript of Lecture 2: Static Methods, if statements, homework uploader
![Page 1: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/1.jpg)
Lecture 2:Static Methods, if statements, homework
uploader
![Page 2: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/2.jpg)
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 ?
![Page 3: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/3.jpg)
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.
![Page 4: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/4.jpg)
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
![Page 5: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/5.jpg)
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 );
![Page 6: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/6.jpg)
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));
![Page 7: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/7.jpg)
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);
![Page 8: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/8.jpg)
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.
![Page 9: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/9.jpg)
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() );
![Page 10: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/10.jpg)
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());
![Page 11: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/11.jpg)
Code BLOCKS• In Java we use { } to make code "blocks"
• Examples:
![Page 12: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/12.jpg)
Let's "trace" that last example
![Page 13: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/13.jpg)
Basic If Statement
• Simplest if uses a condition test to determine whether to execute a code block
• example
![Page 14: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/14.jpg)
Flow Chart view of If Statement
![Page 15: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/15.jpg)
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?
![Page 16: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/16.jpg)
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: <, >, <=, >=, !=, ==
![Page 17: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/17.jpg)
Comparison Operators
![Page 18: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/18.jpg)
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 )
![Page 19: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/19.jpg)
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
![Page 20: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/20.jpg)
Example
![Page 21: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/21.jpg)
![Page 22: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/22.jpg)
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");}
![Page 23: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/23.jpg)
![Page 24: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/24.jpg)
Demo Ifint score = 95;if (score >= 65){ // say whether we passed
System.out.println("Pass");}
if (score >= 65){ // say pass/failSystem.out.println("Pass");
}else {
System.out.println("Fail");}
if (score >= 90{ // say A, B or CSystem.out.println("A");
}else if (score >=80) {
System.out.println("B");}else {
System.out.println("C");}
![Page 25: Lecture 2: Static Methods, if statements, homework uploader](https://reader034.fdocuments.us/reader034/viewer/2022051316/5681488d550346895db5a50d/html5/thumbnails/25.jpg)
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