120 likes | 217 Views
COMP 14: Midterm Review. June 8, 2000 Nick Vallidis. Announcements. Midterm is tomorrow!. Assignment P4. Just want to go over it to make sure you all understand. Instantiating objects. We do this with the keyword new Examples: String name; name = new String("Vallidis"); Die myDie;
E N D
COMP 14: Midterm Review June 8, 2000 Nick Vallidis
Announcements • Midterm is tomorrow!
Assignment P4 • Just want to go over it to make sure you all understand...
Instantiating objects • We do this with the keyword new • Examples: String name; name = new String("Vallidis"); Die myDie; myDie = new Die(6);
packages • A package is a group of classes • That's it. In Java, each class has its own .java file and all the .java files in a directory are one package.
Assignment operators mult += value is just the same thing as: mult = mult + value • This is just a shortcut so you don't have to type mult twice! • You can replace + with any of the normal math operators: -, /, %, etc.
Equivalence of loops • All loops have the same four parts: • initialization • condition • statements (the loop body) • increment (the loop update -- it doesn't have to be an increment specifically) • As a result, you can convert between loop types.
Equivalence of loops for (initialization; conditon; update) body; initialization; initialization; while (condition) do { { body; body; update; update; } } while (condition);
Nested if statements • What's up with this? if (x < 3) if (y > 2) System.out.println("yay!"); else System.out.println("x >= 3");
Nested if statements • write nested ifs to do the following: • you have one integer x • when 0<=x<5 print "low" • when 5<=x<10 print "medium" • when 10<=x<15 print "high" • for any other value of x don't do anything ------------------------------------------------------ • Can you do this as a switch statement?
nested loops • what does this code do? int i, j; for (i=1; i<=5; i++) { for (j=i; j>=1; j--) System.out.print("*"); System.out.println(); }
nested loops • Write code to print out a person's name 5 times and then ask if they want to do it again. If they do, then print their name again 5 times (response can be string or integer) Assume the name has already been read and has this declaration: String name;