int x = 3; if (x > 1) if (x > 2) { if (x > 3) ...

54
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four");

Upload: gibson

Post on 19-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four");. x = 3. int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

Page 2: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x = 3

Page 3: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

true

Page 4: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 2

true

true

Page 5: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 2

{ true

true

Page 6: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

{ true

true

true

Page 7: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“one”

{ true

true

true

Page 8: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“one”

{

}

true

true

true

Page 9: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“one”

{

}

true

true

true

Page 10: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“one”

{

}

true

true

true

Page 11: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“one”

{

}

true

true

true

Page 12: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

{

}

true

true

true

Page 13: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

{

}

true

true

true

Page 14: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

{

}

true

true

true

Page 15: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

{

}

true

true

true

Page 16: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

“four”

{

}

true

true

true

Page 17: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

“four”

{

}

true

true

true

Page 18: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

“four”

{

}The end.

true

true

true

Page 19: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

x > 1

x = 3

x > 3

x > 2

“three” “one”

“four”

{

}The end. The end. Nothing gets printed.

true

true

true

Page 20: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

Old problem

Page 21: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1) {

if (x > 2)

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

New problem

Page 22: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1)

if (x > 2) {

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

Old problem

Page 23: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1) {

if (x > 2)

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

New problem

Page 24: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1) {

if (x > 2)

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

New problem

else w/out if

Does not compile!

Page 25: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1) {

if (x > 2)

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

New problem

else w/out if

Does not compile!

The only solution, then, is to carefully draw the flowchart!

Page 26: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

int x = 3;

if (x > 1) {

if (x > 2)

if (x > 3)

System.out.println("one");

} else System.out.println("three");

else System.out.println("four");

New problem

else w/out if

Does not compile!

The only solution, then, is to carefully draw the flowchart!

There really isn’t any reliable, uniform shortcut alternative to that.

Page 27: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

Let’s take a look at the lab assignment.

Page 28: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 29: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 30: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 31: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 32: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 33: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 34: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 35: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 36: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 37: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 38: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 39: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 40: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 41: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 42: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 43: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 44: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 45: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 46: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 47: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 48: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 49: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 50: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 51: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 52: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

So you can give the user the change (if any).

initialize the amount the user needs to pay

while (the amount due is more than zero) {

prompt the user for input (a line)

get the line and prepare to take it apart

while (there are still tokens available) {

get next token (call it token)

interpret it, and update amount

}

// if you get here you’re done with the line

// but the amount due can still be positive

}

// if you get here the amount due is zero or less

Page 53: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

Now let’s talk about the homework problems…

Page 54: int x = 3;  if (x > 1)    if (x > 2) {      if (x > 3)        System.out.println("one");

1. The Wizard Problem

You are to define a class of objects that (once created) can take simple

(math) questions. The following three requirements are to be met:

a) wizards know how to calculate the greatest common divisor of two integers

b) a wizard can list the prime factors of any positive integer

c) a wizard can tell you if an integer is prime or not