CSCI A202 - Introduction to Programming (II)

Lecture 22: New Toys

We'll look at:

The code and the lecture will be summarized here in the afternoon.

Here's the code for the second part of the lecture:


tucotuco.cs.indiana.edu% ls
One.java Three.java Two.java
tucotuco.cs.indiana.edu%
tucotuco.cs.indiana.edu% cat Three.java
import java.util.*; import java.io.*; class Three { public static void main(String[] args) { try { List list = null; System.out.println("Please create some lists."); BufferedReader b = new BufferedReader( new InputStreamReader( System.in)); while (true) { System.out.print("input> "); StringTokenizer s = new StringTokenizer(b.readLine()); if (s.hasMoreTokens()) { String cmd = s.nextToken(); if (cmd.equals("new")) { list = makeList(s); if (list != null) { System.out.println(list.show()); } else { System.out.println("Empty list."); } } else { if (cmd.equals("quit")) { System.out.println("Thanks for visiting."); System.exit(0); } else { System.out.println("Unknown command: " + cmd); } } } else { System.out.println("Empty line."); } } } catch (Exception e) { System.out.println("E: " + e.toString()); } } static List makeList(StringTokenizer s) { List result = null; if (s.hasMoreTokens()) { Object value = s.nextToken(); return new List(value, makeList(s)); } else { return result; } } } class List { Object value; List nextElement; List(Object value, List nextElement) { this.value = value; this.nextElement = nextElement; } String show() { if (nextElement == null) { return (String)value; } else { return value + " " + nextElement.show(); } } }
tucotuco.cs.indiana.edu% javac Three.java
tucotuco.cs.indiana.edu% java Three
Please create some lists. input> okay Unknown command: okay input> new Empty list. input> Empty line. input> new 1 2 3 1 2 3 input> new three two one three two one input> nice Unknown command: nice input> bye Unknown command: bye input> quit Thanks for visiting.
tucotuco.cs.indiana.edu%