1 / 37

Exam 1 Thursday: Loops, Methods, and Packages

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.

jeannew
Download Presentation

Exam 1 Thursday: Loops, Methods, and Packages

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. CS 200 Loops Jim Williams, PhD

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

  3. Midterm Exam 1 What is the location of your exam?

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

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

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

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

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

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

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

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

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

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

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

  15. Loops https://www.wikihow.com/Race-Your-Car

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

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

  18. Value of i, j after this executes? int i = 5; int j = i++;

  19. Value of m after this executes? int i = 5; int k = 6; int m = i++ + ++k;

  20. 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 }

  21. 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 }

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

  23. How many "hello"? int a = 0; while ( a <= 5 ) { System.out.print("hello "); a++; }

  24. What is CONTROL so loops have same result? int a = 0; while ( a <= 5 ) { System.out.print( a++ ); } for ( CONTROL ) { System.out.print( a ); }

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

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

  27. UML Activity Diagram (Flow Chart)

  28. Which is best Java construct for this?

  29. 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 {

  30. Nested Loops Race within a Race

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

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

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

  34. Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately

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

  36. How many times does this loop execute? for ( int i = 9; i >= 0; i--) { System.out.println( "i=" + ++i); }

  37. Potential Confusion: Difference between if and while boolean doIt = true; if ( doIt ) { System.out.println( doIt ); } while ( doIt ) { System.out.println( doIt ); }

More Related