Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Fix a program that has if Online time for Monday’s Program

Similar presentations


Presentation on theme: "Java Fix a program that has if Online time for Monday’s Program"— Presentation transcript:

1 Java Fix a program that has if Online time for Monday’s Program
Quiz next Friday. Fix a program that has if Online time for Monday’s Program Review if with Dry Run Take a look at the for loop.

2 Learning Objectives Be able to correct a program that uses if
Be able to write a program that uses if Be able to know when and how to use a for loop.

3 Fix9-19-2016.java on the class website
Fix the following Fix java on the class website You might need to copy this to BlueJ.

4 Programs: Complete one of the following from each section.
Modify the quadratic equation program to also calculate imaginary roots. If b2 – 4ac >= 0 then display the real roots Else show the imaginary roots Input three real numbers, and print the numbers in sorted order. Write a program that inputs a persons name, rate of pay, and the number of hours worked during the week. The program will calculate the pay for the person. For every hour of overtime work (over 40 hours in the week) the person gets paid 150 percent of the regular wage. Example: Input: Sue, $10.00 per hour rate, 45 hours Calculation: pay = 10* *10*5 = $475 Input a value representing a person’s percent score (example inputting 80 represents an 80% score), write a program that will output the corresponding letter grade. ‘A’ for , ‘B’ for 80 to 89, etc.

5 Order of operations for comparisons
() *, /, % Multiplicative operators +, - Additive operators <, >, >=, <= Relational operators ==, != Then do any comparisons for equality and inequality && Logical and || Logical or = Assignment operator

6 Warm-up: T, F or Invalid a=3, b=7, c=10, d = -20

7 Dry run the following int x = 20, y = 15, z; if (x<y) z = 10; else
System.out.println(z); int x = 2; if (3 / x == 1) System.out.println(“Equal”); else System.out.println(“Not equal”);

8 What does the following do?
public class ForDryRun { public static void main(String[] args) int a=0, b=3, c=10; for (int d=0; d<=b; d++) a+= b; c = d % 2; System.out.printf("a = %d b = %d c = %d d = %d\n", a, b, c, d); }

9 For loop int count = 0; Creates the count variable Sets count to 0
When do you use it? Semantics: Syntax: for (init ; continueTest; updateOp) { commands; } Example for (int count = 0;count<10;count = count + 1) count<10 Boolean condition Like a while loop, while this is true, it will repeat the loop again. You can use &&, || in this condition count = count + 1 AFTER the loop is completed, this is executed

10 counter only exists during the for loop.
For loop Example // Fig. 5.2: ForCounter.java // Counter-controlled repetition with the for repetition statement. public class ForCounter { public static void main( String args[] ) // for statement header includes initialization, // loop-continuation condition and increment for ( int counter = 1; counter <= 10; counter++ ) System.out.printf( "%d ", counter ); System.out.println(); // output a newline } // end main } // end class ForCounter counter only exists during the for loop. ?

11 Ways of incrementing a variable
Increment/Decrement Meaning count++; count=count+1; count--; count=count-1; count+=4; count =count+4; count-=5; count=count-5; count*=2; count = count*2; count /=3; count = count/3; count %= 5; count = count % 5;

12 Your Turn Write the code (No imports, No public static…) to find the total of 10 scores (integers) input from the user. Push: Find the highest of these scores.

13 int score, total=0, highest = 0;
Scanner input = new Scanner(System.in); for ( int counter = 1; counter <= 10; counter++ ) { System.out.println( "Please enter a score" ); score = input.nextInt(); total+=score; if (score>highest) highest = score; } System.out.println("Total = " + total); System.out.println("Highest = " + highest);

14 Multiple Initializers and Incrementers
Sometimes it's necessary to initialize several variables before beginning a for loop. Similarly you may want to increment more than one variable. Java lets you do this by placing a comma between the different initializers and incrementers like this: for (int i = 1, j = 100; i < 100; i = i+1, j = j-1) { System.out.println(i + j); } You can't, however, include multiple test conditions, at least not with commas. The following line is illegal and will generate a compiler error. for (int i = 1, j = 100; i <= 100, j > 0; i = i-1, j = j-1) { }

15 Programs if/for (At least one from each section)
For Program Options Enter a number: Output it’s factorial Enter a base and an exponent: Using a for loop calculate base^exponent using the for loop. (No Math.pow()) Combined Program options Enter 10 scores. Output the total, average, highest and lowest score. Enter 5 ages and calculate the number of teenagers in the list.


Download ppt "Java Fix a program that has if Online time for Monday’s Program"

Similar presentations


Ads by Google