Lecture 2: Static Methods, if statements, homework uploader

25
Lecture 2: Static Methods, if statements, homework uploader

description

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: - PowerPoint PPT Presentation

Transcript of Lecture 2: Static Methods, if statements, homework uploader

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

Lecture 2:Static Methods, if statements, homework

uploader

Page 2: 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 ?

Page 3: Lecture 2: Static Methods, if statements, homework uploader

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

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

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

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

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

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

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

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

Code BLOCKS• In Java we use { } to make code "blocks"

• Examples:

Page 12: Lecture 2: Static Methods, if statements, homework uploader

Let's "trace" that last example

Page 13: Lecture 2: Static Methods, if statements, homework uploader

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

Flow Chart view of If Statement

Page 15: Lecture 2: Static Methods, if statements, homework uploader

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

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

Comparison Operators

Page 18: Lecture 2: Static Methods, if statements, homework uploader

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

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

Example

Page 21: Lecture 2: Static Methods, if statements, homework uploader
Page 22: Lecture 2: Static Methods, if statements, homework uploader

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
Page 24: Lecture 2: Static Methods, if statements, homework uploader

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

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