Download presentation
Presentation is loading. Please wait.
1
For Loops & Iterators CSC 171 FALL 2001 LECTURE 7
2
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
3
While loop
4
While loop Code int year = 0 ; While (year<=20){ balance += balance * interest; year++; }
5
For loop
6
For loop Code for (int year = 1;year<=20;year++){ balance += balance * interest; }
7
For Loop Initialization Test Body Increment for(initialzation;test;increment){ //Body }
8
Nested Loops Sometimes, we want to perform 2D operations Tables – Addition – Multiplication – Interest rates
9
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)); }
10
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(); }
11
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
12
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:
13
General Case Enumeration e = v.elements() ; while(e.hasMoreElements()) { System.out.println(e.nextElement()); }
14
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);
15
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
16
String Tokeizer II StringTokenizer tokenizer = new StringTokenizer(inputLine); while (tokenizer.hasMoreTokens()) { // print each word String word = tokenizer.nextToken(); System.out.println(word); }
17
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)
18
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; }
19
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; }
20
Constructor & Accesor Methods In order to prevent inadvertent (buggy) changes to an object, we want to limit access to the object’s data
21
Some instance varriables public class Student { public String studentName; public int studentNumber; }
22
Usage Student s1 = new Student(); s1.studentName = "Holly Yashi"; s1.studentNumber = 123456789;
23
Control access with a constructor public class Student { private String studentName; private int studentNumber; public Student(String name; int number) studentName=name; studentNumber = number; }
24
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;} }
25
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.