comp 14: midterm review june 8, 2000 nick vallidis

12
COMP 14: Midterm Review June 8, 2000 Nick Vallidis

Post on 21-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

COMP 14: Midterm Review

June 8, 2000Nick Vallidis

Page 2: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

Announcements

Midterm is tomorrow!

Page 3: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

Assignment P4

Just want to go over it to make sure you all understand...

Page 4: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

Instantiating objects

We do this with the keyword new

Examples:String name;

name = new String("Vallidis");

Die myDie;

myDie = new Die(6);

Page 5: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

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.

Page 6: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

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.

Page 7: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

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.

Page 8: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

Equivalence of loops

for (initialization; conditon; update)body;

initialization; initialization;while (condition) do{ {

body; body;update; update;

} }while (condition);

Page 9: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

Nested if statements

What's up with this?

if (x < 3)

if (y > 2)

System.out.println("yay!");

else

System.out.println("x >= 3");

Page 10: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

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?

Page 11: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

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();

}

Page 12: COMP 14: Midterm Review June 8, 2000 Nick Vallidis

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;