Transcript ALGORITHMS

For Loops & Iterators
CSC 171 FALL 2001
LECTURE 7
History: Vannevar Bush
1925 - Vannevar Bush, MIT,
built a large-scale
differential analyzer with
the additional capabilities
of integration and
differentiation.
Funded by the Rockefeller
Foundation, the
differential analyzer was
perhaps the largest
computational device in
the world in 1930
While loop
While loop Code
int year = 0 ;
While (year<=20){
balance += balance * interest;
year++;
}
For loop
For loop Code
for (int year = 1;year<=20;year++){
balance += balance * interest;
}
For Loop

Initialization
 Test
 Body
 Increment
for(initialzation;test;increment){
//Body
}
Nested Loops

Sometimes, we want to perform 2D
operations
 Tables
– Addition
– Multiplication
– Interest rates
Multiplication Table

What is the output?
int size = 5; // DON’T HARDCODE NUMS
for(int i = 0 ; i<size;i++) {
for(int j = 0 ; j<size;j++) {
System.out.println(String.toString(i*j));
}
}
Multiplication Table Fixed
int size = 5;
for(int i = 0 ; i<size;i++) {
for(int j = 0 ; j<size;j++) {
System.out.print(String.toString(i*j) + “ “);
}
System.out.println();
}
Enumerations

Integers are nice, because we always have a
clear idea of what the next one is.
 But sometimes, we have an orderd set or list
of things that aren’t numbers
– {Hearts, Spades, Diamonds, Clubs}
– {Bob, Carol, Ted, Alice}

We would like to go through them one at a
time
public interface Enumeration

An object that implements the Enumeration
interface generates a series of elements, one
at a time. Successive calls to the
nextElement method return successive
elements of the series.
 For example, to print all elements of a
vector v:
General Case
Enumeration e = v.elements() ;
while(e.hasMoreElements()) {
System.out.println(e.nextElement());
}
Our Fave: StringTokenizer

A String Tokenizer breaks strings up into tokens
(surprize!)
 String “Hello CSC 171, How are you”
 Tokens:
– “Hello”,“CSC”,”171,”,”How”,”are”,”you”
StringTokenizer tokenizer
= new StringTokenizer(inputLine);
String Tokenizer
import java.util.StringTokenizer;
public class Split
{
public static void main(String[] args)
{ ConsoleReader console = new
ConsoleReader(System.in);
boolean done = false;
while (!done)
{ String inputLine = console.readLine();
if (inputLine == null)
done = true;
else
{ // break input line into words
String Tokeizer II
StringTokenizer tokenizer
= new StringTokenizer(inputLine);
while (tokenizer.hasMoreTokens())
{ // print each word
String word = tokenizer.nextToken();
System.out.println(word);
}
}
}
}
}
The switch statement

A sequence of if/else that compares a single
integer against constants can be implemented as a
switch statement
 Consider the problem of naming digits
1 -> “one”
2 -> “two”
….
9 – “nine”

Exercise
– Write : public String digit2name(int n)
If solution
public String digit2name(int n) {
String name;
if (n == 1) name = “one”;
else if (n == 2) name = “two”;
else if (n == 3) name = “three”;
// etc . . .
else if (n == 9) name = “nine”;
else name = “”;
return name;
}
switch solution
public String digit2name(int n) {
String name;
switch(n) {
case 1: name = “one”; break; // check your breaks!
case 2: name = “two”; break;
// etc
case 9: name = “nine”; break;
default: name= “”;
}
return name;
}
Constructor & Accesor
Methods

In order to prevent inadvertent (buggy)
changes to an object, we want to limit
access to the object’s data
Some instance varriables
public class Student {
public String studentName;
public int studentNumber;
}
Usage
Student s1 = new Student();
s1.studentName = "Holly Yashi";
s1.studentNumber = 123456789;
Control access with a
constructor
public class Student {
private String studentName;
private int studentNumber;
public Student(String name; int number)
studentName=name;
studentNumber = number;
}
}
Control access with accessors
public class Student {
private String studentName;
private int studentNumber;
public Student(String name; int number)
studentName=name;
studentNumber = number;
}
public String getname() { return studentName;}
public int getnumber(){return studentNumber;}
}
Usage
Student s1 =
new Student(“Holly Yashi”,123456789);
System.out.println(
s1.getname() + “’s student number is :”
+ s1.getnumber);
// allows setting at construction time only