for loops & iterators csc 171 fall 2001 lecture 7
Post on 19-Dec-2015
221 views
TRANSCRIPT
![Page 1: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/1.jpg)
For Loops & IteratorsFor Loops & Iterators
CSC 171 FALL 2001
LECTURE 7
![Page 2: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/3.jpg)
While loopWhile loop
![Page 4: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/5.jpg)
For loopFor loop
![Page 6: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/7.jpg)
For LoopFor Loop
InitializationTestBody Increment
for(initialzation;test;increment){//Body
}
![Page 8: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/19.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/20.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/21.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/22.jpg)
UsageUsage
Student s1 = new Student();
s1.studentName = "Holly Yashi";
s1.studentNumber = 123456789;
![Page 23: For Loops & Iterators CSC 171 FALL 2001 LECTURE 7](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/23.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/24.jpg)
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](https://reader030.vdocuments.mx/reader030/viewer/2022032800/56649d3f5503460f94a188eb/html5/thumbnails/25.jpg)
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