CIS 1068 October 19, 2017
Administrative Stuff String methods due tomorrow Boston Accent Reading: up to chapter 5 Midterms
Last Time midterm discussion guessing game
Legal Identifiers Ch33zyHaX0R cous cous 5GuysBurgerCount
Legal Identifiers Ch33zyHaX0R yes cous cous 5GuysBurgerCount
Legal Identifiers Ch33zyHaX0R yes cous cous yes 5GuysBurgerCount
Legal Identifiers Ch33zyHaX0R yes cous cous yes 5GuysBurgerCount no
True-False question It s legal to have variables named whitewalkername and WhiteWalkerName in the same program, but not defined in the same method.
True-False question It s legal to have variables named whitewalkername and WhiteWalkerName in the same program, but not defined in the same method. answer False. Java is case sensitive whitewalkername and WhiteWalkerName are different bad idea but legal
What s Printed? 1 public class WhatsPrinted { 2 public static void main(string args[]) { 3 func(0); 4 } 5 6 public static void func(int start) { 7 for (int i=start; i>=0; i--) { 8 System.out.print(i); 9 } 10 } 11 }
What s Printed? 1 public class WhatsPrinted3 { 2 public static void ifelse(int a, int b) { 3 if (a < b) { 4 a++; 5 } 6 if (a < b) { 7 a++; 8 } else { 9 b++; 10 } 11 if (a >= b) { 12 b = b - 5; 13 } 14 System.out.println(a + "," + b); 15 } 16 public static void main(string args[]) { 17 ifelse(10,5); 18 ifelse(3,9); 19 } 20 }
Question About how much is a terabyte? Answer 10 12 or 2 40 or about a trillion bytes
Which analogy is most accurate? cookie cutter is to cookie as object is to class cookie is to cookie cutter as object is to class cookie cutter is to cookie as blueprint is to object cookie is to cookie cutter as blueprint-class is to class cookie is to cookie cutter as base class is to object
What s Printed? 1 public class WhatsPrinted1 { 2 public static void main(string args[]) { 3 int y=10; 4 5 func(y); 6 System.out.println(y); 7 } 8 9 public static void func(int x) { 10 x*=2; 11 } 12 }
What s Printed? 1 public class WhatsPrinted5 { 2 public static int func(int y) { 3 return y*2; 4 } 5 6 public static void main(string args[]) { 7 int x=10; 8 9 func(x); 10 System.out.println(x); 11 } 12 }
What s Printed? 1 public class WhatsPrinted2 { 2 public static void main(string args[]) { 3 int nx=0; 4 for (int i=0; i<5; i++) { 5 for (int j=0; j<3; j++) { 6 if ((i+j)%2==0) { 7 nx++; 8 } 9 } 10 } 11 // last value of j 12 System.out.println(j); 13 } 14 }
What s Printed? 1 public class WhatsPrinted4 { 2 public static void func(int x, int y, int z) { 3 x++; 4 y+=z%2; 5 z*=2; 6 System.out.println(z); 7 } 8 9 public static void main(string args[]) { 10 int x=10, y=21, z=30; 11 12 func(y, z, x); 13 } 14 }
1 public class WhatsPrinted10 { 2 public static void main(string args[]) { 3 yetanotherfunc(); 4 System.out.println(); 5 } 6 7 public static void anotherfunc() { 8 somefunc(); 9 System.out.print("b"); 10 } 11 12 public static void yetanotherfunc() { 13 somefunc(); 14 System.out.print("c"); 15 anotherfunc(); 16 } 17 18 public static void somefunc() { 19 System.out.print("a"); 20 } 21 }
What s Printed? 1 public class WhatsPrinted01 { 2 public static void func(int x, int y, int z) { 3 x+=y/2; 4 z++; 5 y%=3; 6 } 7 8 public static void main(string []args) { 9 int x=10, y=20, z=30; 10 func(z, x, y); 11 System.out.println(x); 12 } 13 }
Question What s the most important job of the Java compiler? Answer To translate my code into more primitive instructions
What s Printed? 1 public class WhatsPrinted08 { 2 public static void main(string args[]) { 3 String s1="bob"; 4 String s2="lob"; 5 String s3="law"; 6 7 for (int i=0; i<5; i++) { 8 if (i%2==0) 9 s2+=s3; 10 else 11 s1+=s2; 12 } 13 System.out.println(s1); 14 } 15 }
Expression Evaluation 516 / 10 / 5 / 2.0 * 2 + 14 / 5
Expression Evaluation 2.5 * 2 * 5 / 10 + 1.5
Expression Evaluation "hard".charat(0) + "exam".substring(2,4);
Expression Evaluation 1 + 2 + "." + (3 + 4) + 5 * 6
Expression Evaluation!(2<7%2*3 5%10>=5+3)
Expression Evaluation 72 % 10 + 2 % 5-9 % 4
Expression Evaluation 11 + 2 * 5 / 2
Question Translate each of the following statements from English to Java. For example, if the English is x is larger than 10, you d write the Java expression x > 10. Assume that we already have int x, int y, and the String s and String t already properly declared somewhere else in our program. t does not occur in s Answer s.indexof(t)==-1 OR!s.contains(t)
Question Translate each of the following statements from English to Java. For example, if the English is x is larger than 10, you d write the Java expression x > 10. Assume that we already have int x, int y, and the String s and String t already properly declared somewhere else in our program. The sum of x and y is odd. Answer (x+y)%2==1
Question Translate each of the following statements from English to Java. For example, if the English is x is larger than 10, you d write the Java expression x > 10. Assume that we already have int x, int y, and the String s and String t already properly declared somewhere else in our program. s is longer than 10 characters and begins with the letter F Answer s.length()>10 && s.beginswith("f") or s.length()>10 && s.charat(0)== F
Question Write the few lines of code that print the integers between 50 and 10000 that are evenly divisible by 7 (i.e. 56, 63, 70, 77, 84,... ).
Question Write the few lines of code that print the integers between 50 and 10000 that are evenly divisible by 7 (i.e. 56, 63, 70, 77, 84,... ). Answer for (int i=50; i<=10000; i++) { if (i%7==0) { System.out.print(i + " "); } }
Question Write a method called printtri( ) that takes an integer numlines as an argument. The method prints a triangle which is numlines high with the following format: if numlines=1: if numlines=3: if numlines=3: etc. 1 1 22 1 22 333
Question Write a method called printtri( ) that takes an integer numlines as an argument. The method prints a triangle which is numlines high with the following format: if numlines=1: if numlines=3: if numlines=3: etc. 1 Answer 1 22 1 22 333 public static void printtri(int numlines) { for (int i=1; i<=numlines; i++) { for (int j=1; j<=i; j++) { System.out.print(i); } System.out.println(); } }
Question Write a method which is passed an integer n. The method returns the sum of all of the positive integers from 1 to n. For example, if n is 5, the method returns 15, because 15 is 1 + 2 + 3 + 4 + 5. You do not have to write a complete class. You do not have to use a Scanner to read user input from the keyboard.
Question Write a method which is passed an integer n. The method returns the sum of all of the positive integers from 1 to n. For example, if n is 5, the method returns 15, because 15 is 1 + 2 + 3 + 4 + 5. You do not have to write a complete class. You do not have to use a Scanner to read user input from the keyboard. Answer public static final int sumton(int n) { int sum=0; } for (int i=1; i<=n; i++) { sum+=i; } return sum;