Download Notes For Week 4 Tutorial

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Large numbers wikipedia , lookup

Addition wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Collatz conjecture wikipedia , lookup

Transcript
Object-Oriented programming Week 8 Tutorial
1
Notes For Week 8 Tutorial
In this tutorial, you will practise some loops like “while”, “do while” and “for” loop.
1. The Hailstone Problem
There is an interesting series of integers known as hailstones. Hailstones are formed by
being given a starting integer and generating the next integer as follows: If the previous
integer was even, the next integer in the series is half of it. If the previous integer was
odd, the next integer is three times it plus one. Although the series goes up and down
(like hailstones before they fall to the ground), it eventually settles into a steady state of
4, 2, 1, 4, 2, 1. For example, starting at 21, the hailstone series is: 21, 64, 32, 16, 8, 4, 2,
1, 4, 2, 1. For 21, the series required five steps before the steady state was reached.
2. The “while” loop.
Given a seed (ie. a starting number), here is a program that prints out the next 10
numbers, using a “while” loop.
/**
* This class prints out numbers in a "Hailstone" series.
* The learning objective of this program is to give students practise
* in the use of "while" loops.
* @author Suresh Paryani
* @version April 2003
*/
import javabook.*;
public class whileTest
{
public static void main(String args[])
{
int last, next; // two numbers in the hailstone series
last = SimpleInput.getInteger(
"Enter the number that starts the Hailstone series:\t");
int counter=1;
while(counter<=10)
{
next = getNextNumber(last); // see the next page
System.out.println(next);
last = next;
counter ++;
}
System.out.println("finished");
} // main method
Object-Oriented programming Week 8 Tutorial
2
//Given the most recent number as a parameter, this method returns
//next number in the series.
public static int getNextNumber(int lastNumber)
{
if(lastNumber%2 == 0)
{
// Even number
return lastNumber/2;
}
else
{
//Odd number
return lastNumber *3 +1;
}
} // getNextNumber method
} // class whileTest
The following table may help you understand the loop (if not, ask your tutor).
Code
Comment
last next counter
last = SimpleInput.getInteger … etc …
21
int counter=1;
1
while(counter<=10)
TRUE
next = getNextNumber(last);
64
System.out.println(next);
64 outputted
last = next;
64
counter ++;
2
}
boing!
while(counter<=10)
TRUE
next = getNextNumber(last);
32
System.out.println(next);
32 outputted
last = next;
32
counter ++;
3
}
boing!
…etc …
3. Activity- “do while” loop: Modify the above program to use a “do while” loop.
4. Activity- “for” loop: Modify the above program to use a “for” loop.
5. Activity: Modify the above programs to add a user input to get the number of steps
that should be printed. For example, consider the sequence 21, 64, 32, 16, 8, 4, 2, 1. A
step would be 21 to 64. Next step would be 64 to 32. And the next would be 32 to 16 and
so on. Hence in the above example the number of steps are 7. We want the user to enter
this number (number of steps).
6. Activity: Modify the above program to stop after the numbers 4, 2, 1 are outputted.
Hint: Stop the loop/program, when the variable “last” is equal to 1
A sample solution would be posted on UTS Online for all the programs.