For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

Post on 19-Dec-2015

220 views 2 download

Tags:

Transcript of For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

For Loops & IteratorsFor Loops & Iterators

CSC 171 FALL 2001

LECTURE 7

History: Vannevar Bush 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 loopWhile loop

While loop CodeWhile loop Code

int year = 0 ;

While (year<=20){balance += balance * interest;

year++;

}

For loopFor loop

For loop CodeFor loop Code

for (int year = 1;year<=20;year++){balance += balance * interest;

}

For LoopFor Loop

InitializationTestBody Increment

for(initialzation;test;increment){//Body

}

Nested LoopsNested Loops

Sometimes, we want to perform 2D operations

Tables– Addition– Multiplication– Interest rates

Multiplication TableMultiplication Table

What is the output?

int size = 5; // DON’T HARDCODE NUMSfor(int i = 0 ; i<size;i++) {

for(int j = 0 ; j<size;j++) {

System.out.println(String.toString(i*j));

}

}

Multiplication Table FixedMultiplication 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();

}

EnumerationsEnumerations

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 public interface EnumerationEnumeration

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 CaseGeneral Case

Enumeration e = v.elements() ; while(e.hasMoreElements()) { System.out.println(e.nextElement());

}

Our Fave: StringTokenizerOur 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 TokenizerString 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 String Tokeizer II

StringTokenizer tokenizer = new StringTokenizer(inputLine); while (tokenizer.hasMoreTokens()) { // print each word String word = tokenizer.nextToken(); System.out.println(word); } } } }}

The The switchswitch statement 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 digits1 -> “one”2 -> “two”….9 – “nine”

Exercise – Write : public String digit2name(int n)

If solutionIf solutionpublic 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 solutionswitch solutionpublic String digit2name(int n) {

String name;switch(n) {

case 1: name = “one”; break; // check your breaks!case 2: name = “two”; break;// etccase 9: name = “nine”; break;default: name= “”;

}return name;

}

Constructor & Accesor Constructor & Accesor MethodsMethods

In order to prevent inadvertent (buggy) changes to an object, we want to limit access to the object’s data

Some instance varriablesSome instance varriables

public class Student {

public String studentName;

public int studentNumber;

}

UsageUsage

Student s1 = new Student();

s1.studentName = "Holly Yashi";

s1.studentNumber = 123456789;

Control access with a Control access with a constructorconstructor

public class Student {

private String studentName;

private int studentNumber;

public Student(String name; int number)

studentName=name;

studentNumber = number;

}

}

Control access with accessorsControl 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;}

}

UsageUsage

Student s1 =

new Student(“Holly Yashi”,123456789);

System.out.println(

s1.getname() + “’s student number is :”

+ s1.getnumber);

// allows setting at construction time only