Download presentation
Presentation is loading. Please wait.
1
CS Week 4 Jim Williams, PhD
2
Lab this Week Debugging Skills Planet Explorer 1st team
work on either Terrain or Humidity map 2nd team split up and work with others integrating your maps.
3
This and next week Challenges Conditionals, Loops, Scanner Arrays
Way of storing multiple values of same type Single dimension, multiple dimension
4
What is the value of msg? boolean flag = true;
String msg = "before if"; if ( flag = false); { msg = "" + flag; } before if true false
5
What is the value of sum? 3 9 12 other 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); 3 9 12 other
6
What is the difference in the loops?
int a = 1; while ( a < 5) { System.out.println("hello"); a++; } not much not appropriate for while for is better for ( int a = 1; a < 5; a++) { System.out.println("hell o"); }
7
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
8
UML Activity Diagram (Flow Chart)
9
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 { //declarations are assumed... off = false; do { System.out.println(“Sleeping”); System.out.println("RING, RING”); System.out.print(“Enter to snooze or ‘off’"); response = input.nextLine(); off = response.equals("off"); } while ( !off);
10
Scanner num: 0 str4: 3 str4: line note. num: 3 str4:
String note = "Hmm\na \n3\nline note."; Scanner input = new Scanner( note); int num = 0; String str1 = input.nextLine(); String str2 = input.next(); if ( input.hasNextInt()) { num = input.nextInt(); } String str4 = input.nextLine();
11
Arrays A reference type (not primitive) Hold multiple values
Access individual values with an index
12
Key Array Concepts Variable to hold array reference
Allocation of memory for the array Initialization of the array Accessing Array Elements
13
Which are valid ways to create arrays?
1, 2, 3, 4 1, 2, 4 3, 4 2 int [] list1; //1 int [] list2 = new int[5]; //2 int list3 = new int[]{1,2,3}; //3 int [] list4 = new int[1,2,3,4];//4
14
Which array initializations are valid?
int [] listA = new int[4]; //1 int [] listB = new int[]{8, 7, 6}; //2 Scanner input = new Scanner( System.in); int sizeC = input.nextInt(); int [] listC = new int[ sizeC]; //3 1, 2, 3 1,3 1,2 none
15
What will this print? int [] list; list[0] = 10;
System.out.print( list[1]); 30 1020 compiler error runtime error
16
Will these show the contents of the arrays?
both will both won't one but not the other char [] chars = {'a','b','c','d','e'}; System.out.println(chars); int [] list = {10, 2, 23, 64}; System.out.println(list);
17
What is the value of sum? int sum; int [] list = {1,3,5};
9 3 compiler error runtime error int sum; int [] list = {1,3,5}; sum = list[0] + list[1] + list[2];
18
What is the value of z? int [] list = new int[3]; list[1] = 6;
int z = list[3]; 3 compiler error runtime error
19
What is the data type of listG?
String array of String compiler error JVM error String [ ] listG = {"first","middle","last"}; Try to draw a memory diagram of this.
20
Which are true? 1,2,3,4 String []arr = new String[3];
arr[0] = "hello"; arr[1] = "hello"; arr[2] = new String("hello"); System.out.println( arr[1] == "hello" ); //1 System.out.println( arr[1].equals( "hello")); //2 System.out.println( arr[2] == arr[1]); //3 System.out.println( arr[2].equals(arr[1])); //4 1,2,3,4 1,2,4 2,3,4 2,4
21
Which are true? 1,2,3,4 String []arr = new String[4];
arr[0] = "hello"; arr[1] = "hello"; arr[2] = new String("hello"); arr[3] = arr[2]; System.out.println( arr[2] == "hello" ); //1 System.out.println( arr[2].equals( "hello")); //2 System.out.println( arr[3] == arr[2]); //3 System.out.println( arr[3].equals(arr[2])); //4 1,2,3,4 1,2,4 2,3,4 2,4
22
Which are true? 1,3,4 String []arr = new String[4];
arr[0] = new String("party"); arr[1] = "party"; System.out.println( arr[0] == arr[1] ); //1 String line = scan.nextLine(); arr[2] = line; System.out.println( arr[2] == arr[1]); //2 System.out.println( arr[2] == arr[0]); //3 System.out.println( arr[2].equals(arr[0])); //4 1,3,4 1,2,4 3,4 4
23
Which are true? 1,2 String prefix = "par"; String suffix = "ty";
String combine = prefix + suffix; String str = "party"; System.out.println( str == combine ); //1 System.out.println( str.equals( combine)); //2 1,2 1 2 neither 1 or 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.