int x = 3; if (x > 1) if (x > 2) { if (x > 3) ...
Post on 19-Jan-2016
25 Views
Preview:
DESCRIPTION
TRANSCRIPT
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");
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");
} else System.out.println("three");
else System.out.println("four");
x > 1
x = 3
true
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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!
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!
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.
Let’s take a look at the lab assignment.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Now let’s talk about the homework problems…
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
top related