Title: What can we do with iterations
1What can we do with iterations?
Life is just one damn thing after another
Mark Twain
2Comparisons
- lt, gt, lt and gt can only be used with numbers and
characters - Is true greater than false?
public class JackandJill public static
void main(String args) String s1
"Jack went up the hill." String s2
"Jack went up the hill." if (s1 s2)
System.out.println( "The strings are
the same.")
3Comparison
else if (s1 ! s2)
System.out.println( "The strings are
different.")
The output is The strings are different.
4Comparison
- A Correct Test for String Equality
public class JackandJill public static
void main(String args) String s1
"Jack went up the hill." String s2
"Jack went up the hill." if
(s1.equals(s2)) System.out.println(
"The strings are the same.")
else System.out.println( They the
different.")
5How Much Does the King Owe
- For every square on the chessboard, Ill double
the wheat debt
public class CountWheat public static void
main(String args) int current,
totalAmount final int NUM_OF_SQUARES
64 current 1 totalAmount
0 for (int i1 i lt NUM_OF_SQUARES
i) current 2
totalAmount current
System.out.print(totalAmount \t )
6How Much Does the King Owe
if (i 4 0)
System.out.println()
What Happened???
The output is
- 6 14
30 - 126 254
510 - 2046 4094
8190 - 32766 65534
131070 - 262142 524286
1048574 2097150 - 4194302 8388606
16777214 33554430 - 67108862 134217726 268435454
536870910 - 1073741822 2147483646 -2
-2 - -2 -2
-2 -2 - -2 -2
-2 -2
7How Much Does the King Owe
public class CountWheat public static void
main(String args) int current,
totalAmount final int NUM_OF_SQUARES
64 current 1 totalAmount
0 for (int i1 i lt NUM_OF_SQUARES
i) current 2
totalAmount current if (current
lt 0) System.out.println("Error
Overflow") break
8How Much Does the King Owe
System.out.print(totalAmount \t ) if (i
4 0) System.out.println()
The output is
- 6 14
30 - 126 254
510 - 2046 4094
8190 - 32766 65534
131070 - 262142 524286
1048574 2097150 - 4194302 8388606
16777214 33554430 - 67108862 134217726 268435454
536870910 - 1073741822 2147483646 Error Overflow
9Unstoppable for loop
for (long iLong.MAX_VALUE 2 ilt
Long.MAX_VALUE i) / .../
- How many times will that for loop execute? Until
you kill the process!
- It will loop endlessly because I can never get
bigger than Long.MAX_VALUE to terminate the loop.
10Walls, walls everywhere
- How can you make your way out of a maze ?
Exit
- Simple! Walk along the walls.
11Walls, walls everywhere
public class MazeSolver public static void
main(String args) while (notAtExit)
if (!wallToRight) turnRight()
moveForward()
else if (!wallAhead) moveForward()
else
turnLeft()
12Walls, walls everywhere
- What does the solution look like?
13Palindrome Checkup
- Palindromes can be read from either direction
- For example Racecar
public class PalindromeTester
//------------------------------------------------
----------------- // Tests strings to see if
they are palindromes. //----------------------
-------------------------------------------
public static void main (String args)
String str, another "y" int left,
right while (another.equalsIgnoreCase("y"
)) // allows y or Y
System.out.println ("Enter a potential
palindrome") str readString()
14Palindrome Checkup
left 0 right str.length() -
1 while (str.charAt(left)
str.charAt(right) left lt right)
left right--
System.out.println() if (left lt right)
System.out.println ("That string is
NOT a palindrome.") else
System.out.println ("That string IS a
palindrome.") System.out.println()
System.out.print ("Test another palindrome
(y/n)? ") another readString()
15An Alarm Clock
- Develop an alarm clock and set its timer
public class AlarmClock public static
void main (String args) int hour,
minute, second, alarmHour, alarmMinute,
alarmSec // set the current time
do System.out.print(Enter the hour
) hour readInt() while
((hour gt HOURS_PER_DAY - 1) (hour lt 0))
do System.out.print(Enter the
minutes ) minute readInt()
while ((minute gt MINUTES_PER_HOUR - 1)
(minute lt 0))
16An Alarm Clock
do System.out.print(Enter the
seconds ) seconds readInt()
while ((seconds gt SECONDS_PER_MINUTE - 1)
(seconds lt 0)) // set the alarm do
System.out.print(Enter the alarm
hour ) aralmHour readInt()
while ((aralmHour gt HOURS_PER_DAY-1)
(aralmHour lt 0)) do
System.out.print(Enter the alarm minutes )
alarmMinute readInt() while
((minute gt MINUTES_PER_HOURS-1) (alarmMinute lt
0))
17An Alarm Clock
do System.out.print(Enter the
alarm seconds ) alarmSeconds
readInt() while ((alarmSeconds gt
SECONDS_PER_MINUTE-1)
(alarmSeconds lt 0)) // run the
clock while ((hour ! alarmHour)
(minute ! alarmMinute)
(second ! alarmSeconds)) for (int
time0 time lt SECOND time)
seconds if (seconds
SECONDS_PER_MINUTE) seconds
0 minute
18An Alarm Clock
if (minute MINUTES_PER_HOUR)
minute 0 hour
if (hour HOURS_PER_DAY)
hour 0
// alarm goes off System.out.println(
Wake up!!! Wake up!!!)
19Drawing using loops
- Using for loops to draw shapes on the screen
public class Drawer public static
void main (String args) for (int i0
i lt 5 i) for (int j0 j lt I1
j) System.out.print()
System.out.println()
The output is
20Factorial calculation
- Given a number, calculate its factorial
- N! N(N-1)(N-2).21
public class Factorial public static
void main (String args) long result
1 int number do
System.out.print(enter a number )
number readInt() while (number lt 0)
for (int inumber i gt 0 i--)
result i System.out.println(result
is result)
21Fibonacci series Golden Ratio
- Each term is the sum of the two previous ones
- 0 1 1 2 3 5 8 13 21 34 55.
public class Fibonacci public static
void main (String args) long lower
0 long higher 1 int
iterations readInt() for (int i1 i
lt iterations i)
System.out.print(higher \t )
long temp higher higher lower
lower temp if (i 10
0) System.out.println()
22Fibonacci series Golden Ratio
- The Golden Ratio is the ratio of the last two
terms in this infinite series. - It is supposed to be the ideal proportion in
architecture and art, and was used in the design
of ancient Greek temples. - If the number of iterations is 45, the last two
terms are 701,408,733 and 1,134,903,170 and
their ratio is 0.618034 Close enough for
government work.
23Calculator
- Create the equivalent of a four-function
calculator.
public class Calculator public static
void main (String args) int operand1,
operand2 char operator
while(true) System.out.print(Enter
the first operand ) operand1
readInt() do
System.out.print(Enter the operator
(, -, , /)) operator
readChar() if ((operator
) (operator -) (operator )
(operator /))
break while
(true)
24Calculator
System.out.print(Enter the second
number ) operator2 readInt()
switch (operator)
case
System.out.println(operator1 operator2
(operator1 operator2))
break case -
System.out.println(operator1 - operator2
(operator1 - operator2))
break case
System.out.println(operator1 operator2
(operator1 operator2))
break
25Calculator
case /
System.out.println(operator1 / operator2
(operator1 / operator2))
break
System.out.print(Another calculation ?)
char answer readChar() if
(answer y) (answer Y)
continue else // anything buy y or
Y means no break
26Bank Investment
- What will my balance be in X years?
public class Investment public static
void main (String args) double
amount double rate int years
do
System.out.print(Enter initial amount )
amount readDouble() while
(amount lt 0.0) do
System.out.print(Enter annual rate )
rate readDouble() while (rate lt
0.0)
27Bank Investment
do System.out.print(Enter
number years ) years
readInt() while (year lt 0)
for (int I0 I lt year I) amount
(1.0 rate) System.out.println(After
years years, you will have amount
)
28Y2K
switch (month) case 4 case 6 case 9 case
11 numOfDays 30 break case
2 switch (year 4) case
0 switch (year 400)
case 100 case 200 case 300
numOfDays 28
break default
numOfDays 29
break default
numOfDays 28 break
default numOfDays 31