370 likes | 382 Views
This week includes Exam 1 on Thursday covering topics like loops, methods, and packages. Learn about loop types, method overloading, and import statements. Understand how loops control statement execution and practice tracing loop execution.
E N D
CS 200 Loops Jim Williams, PhD
This Week • Exam 1: Thursday • P5 (Eclipse & zyBooks): Thursday • Maze: nested ifs • Team Lab: More Primitives, Objects, Branches and Methods • Lecture: More Methods, Loops • Loops are Not on Exam 1 • Marc Lecturing Thurs, I'm away Wed to Sat
Midterm Exam 1 What is the location of your exam?
Midterm Exam - Thursday • Bring ID and #2 pencil • Exam seating • directly in front/behind, 1 empty seat to each side • Multiple choice - focus on tracing/explaining Java • 30 questions, 1.5 hours • Review Questions posted on Piazza
What is print out? static void one() { System.out.print( "one "); } static void two(int num) { System.out.print("two "); one(); System.out.print("two "); } public static void main( String []args) { two( 3); System.out.print("main "); }
Parameter Passing • Java has Only Pass-by-Value • For both primitive and reference types only the value of a variable is passed as the argument. • The argument may be either a primitive or a reference. • The term Pass-by-Reference means the reference of the variable containing the value is passed as the argument. • Java does NOT have Pass-by-Reference
What is print out? public static void showFruit( String name) { name =new String("apple"); } public static void main(String[] args) { String name = "grape"; showFruit( name); System.out.println( name); }
What prints out? static void print( double value) { System.out.println( "print(double)"); } static int print( int value) { System.out.print( "print(int)"); return 1; } public static void main( String [] args) { print( 10); }
Method Overloading • Overloaded methods have same name but differ in number or data types of parameters. • Method signature in yellow public static int myMethod(int a, double b) { } • Compiler uses name and data types of arguments to decide which method to call int result = myMethod( 10,23.4);
What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); } public static void main( String [] args) { double value = 10.0; print( value, 20); } static void print( int val1, int val2) { System.out.println( "print(int)"); } }
What prints out? static void print( double val1, double val2) { System.out.println( "void print"); } static int print( double num1, double num2) { System.out.print( "int print"); return 1; } public static void main( String [] args) { print( 10.0, 20.0); }
What prints out? static void print( double val1, double val2) { System.out.println( "print(double)"); } static int something( int val1, int val2) { System.out.print( "something(int)"); return 1; } public static void main( String [] args) { print( 10, 20); }
packages: import vs. fully qualified • Fully qualified class name, don't need import statement. java.util.Scanner input = new java.util.Scanner( System.in); • Not fully qualified so need import statement Scanner input = new Scanner( System.in);
Method Value • The value of a method is the value returned. • The method may also read from a Scanner, print output or have another side effect. static boolean isJava(String name){ System.out.println(name); return name.endsWith(".java"); }
Loops https://www.wikihow.com/Race-Your-Car
Loops • Various ways of repeating statements, over and over and over and over and over… • Different purposes (while, for, do-while) • Everything can be done with a while loop but other loops may be better choices for code readability. • Keywords that change loop execution • break, continue, return • Nesting Loops • Diagram
Common Operators in Loops Increment means add 1, decrement subtract 1 prefix and postfix operators int m = 2; m = m + 1; //add 1 to m store in m m += 1; //add 1 to m, store in m m++; //retrieve the value, then increment m ++m; //increment m, then retrieve the value
Value of i, j after this executes? int i = 5; int j = i++;
Value of m after this executes? int i = 5; int k = 6; int m = i++ + ++k;
Trace a Loop: Factorial public static long factorial(int n) { long fact = 1; int i = 2; while ( i <= n); fact = fact * i++; return fact; } public static void main(String []args) { System.out.println( factorial(4)); // 1 * 2 * 3 * 4 }
3 loops - Different Purposes do { //indefinite - body executes at least once } while ( !finished); while ( !finished) { //indefinite - body may never execute } for ( int i=1; i < 5; ++i) { //definite }
Which loop is the better choice? int a = 1; while ( a < 5) { System.out.println("hello"); ++a; } for ( int a = 1; a < 5; ++a) { System.out.println("hello"); }
How many "hello"? int a = 0; while ( a <= 5 ) { System.out.print("hello "); a++; }
What is CONTROL so loops have same result? int a = 0; while ( a <= 5 ) { System.out.print( a++ ); } for ( CONTROL ) { System.out.print( a ); }
How many times will this execute? Scanner input = new Scanner( System.in); int num; int sum = 0; do { num = input.nextInt(); sum += num; } while ( num > 0);
Body of While will execute... boolean finished = false; while ( ! finished) { String line = input.nextLine(); if ( line.equals("done")) { finished = true; } else { System.out.println("echo:" + line); } }
Arrange Code to Match Diagram //declarations... off = true; System.out.println("RING, RING”); System.out.println(“Sleeping”); } while ( !off); System.out.print(“Enter to snooze or ‘off’"); if ( response.equals("off")) { off = false; } do {
Nested Loops Race within a Race
How many "bye"? for ( int i = 1; i <= 5; i++) { for ( int j = 2; j < 6; j++) { System.out.print("bye "); } } //same output line or different?
What does this print? for ( int i = 1; i <= 5; i++) { for ( int j = 1; j <= 3; j++) { System.out.print(i+","+j+" "); } System.out.println(); }
What is the value of count? int count = 0; for ( int i = 1; i < 4; i++) { for ( int j = 6; j > 3; j--) { count += 1; } } System.out.println("count=" + count);
Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately
What is the value of sum? int sum= 0; boolean done = false; for ( int i = 1; i < 4 && !done; i++) { for ( int j = 6; j > 3; j--) { if ( j > 4) continue; sum += 1; } if ( i == 3) break; } System.out.println("sum=" + sum);
How many times does this loop execute? for ( int i = 9; i >= 0; i--) { System.out.println( "i=" + ++i); }
Potential Confusion: Difference between if and while boolean doIt = true; if ( doIt ) { System.out.println( doIt ); } while ( doIt ) { System.out.println( doIt ); }