COMP 14: Midterm Review June 8, 2000 Nick Vallidis
Announcements zMidterm is tomorrow!
Assignment P4 zJust want to go over it to make sure you all understand...
Instantiating objects We do this with the keyword new zExamples: String name; name = new String("Vallidis"); Die myDie; myDie = new Die(6);
packages zA 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 zAll loops have the same four parts: yinitialization ycondition ystatements (the loop body) yincrement (the loop update -- it doesn't have to be an increment specifically) zAs a result, you can convert between loop types.
Equivalence of loops for (initialization; conditon; update) body;initialization; while (condition)do{body;update;} while (condition);
Nested if statements zWhat's up with this? if (x < 3) if (y > 2) System.out.println("yay!"); else System.out.println("x >= 3");
Nested if statements zwrite nested ifs to do the following: yyou have one integer x ywhen 0<=x<5 print "low" ywhen 5<=x<10 print "medium" ywhen 10<=x<15 print "high" yfor any other value of x don't do anything zCan you do this as a switch statement?
nested loops zwhat 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 zWrite 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;