For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

25
For Loops & Iterators For Loops & Iterators CSC 171 FALL 2001 LECTURE 7
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    2

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

Page 1: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

For Loops & IteratorsFor Loops & Iterators

CSC 171 FALL 2001

LECTURE 7

Page 2: For 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

Page 3: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

While loopWhile loop

Page 4: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

While loop CodeWhile loop Code

int year = 0 ;

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

year++;

}

Page 5: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

For loopFor loop

Page 6: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

For loop CodeFor loop Code

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

}

Page 7: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

For LoopFor Loop

InitializationTestBody Increment

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

}

Page 8: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

Nested LoopsNested Loops

Sometimes, we want to perform 2D operations

Tables– Addition– Multiplication– Interest rates

Page 9: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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));

}

}

Page 10: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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();

}

Page 11: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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

Page 12: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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:

Page 13: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

General CaseGeneral Case

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

}

Page 14: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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);

Page 15: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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

Page 16: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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); } } } }}

Page 17: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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)

Page 18: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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; }

Page 19: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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;

}

Page 20: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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

Page 21: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

Some instance varriablesSome instance varriables

public class Student {

public String studentName;

public int studentNumber;

}

Page 22: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

UsageUsage

Student s1 = new Student();

s1.studentName = "Holly Yashi";

s1.studentNumber = 123456789;

Page 23: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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;

}

}

Page 24: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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;}

}

Page 25: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.

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