Tutorial 3 Lecturer: Steve Bellantoni Tutor: Andria Hunter ========== 981005 @10am (RW229) @1pm (EM108) CSC108, Fall 1998 Tutorial notes, T3 ================================================================== Topics: - Collect Assignments - Using Course Web page - contains hints (~sjb) - Using JWS System, Compiling, Printing - Questions about assignment (not solution) - StringTokenizer example - Constructing objects and running methods on objects Collect Assignments ------------------- They must put their name, birthday (day/month), student number, tutor name, and lecture time on their assignment. Course Web Page --------------- http://www.cs.toronto.edu/~sjb http://www.cs.toronto.edu/~andria/csc/108f98 Make sure you use it. It contains hints about the assignment and lecture notes. Using JWS System ---------------- Answer any questions on the JWS environment and on printing. This file explains how to get the 3 files that are required for assignment 1 into your JWS environment. First go to the Java WorkShop Project Manager Window, and then follow the steps below. Our objective is to organize your files in the Project Manager as follows (the directory names will be different if you are working from campus. It will be H:\jws\...): My Portfolio + C:\Windows\jws\personal.psf [Portfolio] - C:\Windows\jws\andria.psf (substitute your name here) [Portfolio] - C:\Windows\jws\Applications\Applications.prj [Project] C:\Windows\jws\Applications\ApplicationCentre.java [File] C:\Windows\jws\Applications\Applications.java [File] C:\Windows\jws\Applications\Student.java [File] (1) Create New Portfolio using your name File -> New -> Portfolio... Portfolio: andria.psf (2) Create Project using name of class containing main method File -> New -> Project Project name: Applications * Standalone * No GUI next> Where should java store files? C:\Windows\jws\Applications [H:\jws\Applications if working from campus] finish NOTE: You do not have to insert your portfolio name, but if you don't, you cannot use the same project name in a different portfolio. (3) Remove template if not needed (remove Applications.java) File -> Remove File from Project (4) Copy 3 java files to C:\Windows\jws\Applications. You'll either copy the files from the (a) web page, or (b) from some other location such as the floppy drive or another folder on your hard drive. (a) To copy from web page, start up Netscape http://www.cs.toronto.edu/~clarke/108web Go to assignments page, and click on Applications.java File -> Save As... Save in: C:\Windows\jws\Applications [H:\jws\Applications if on campus] Filename: Applications.java NOTE: The above "Save in" is the same as the what you specified in step (2). Repeat for ApplicationCentre.java and Student.java. (b) To copy from the floppy A: drive or the hard drive C: Start up explorer by right clicking on the start button and then selecting "Explore" On the left side of Explorer, go to the directory where you have your files stored (either on the A: drive or the C: drive). Then highlight (select) all 3 java files (use CTRL+Left Click to select), and then copy using File->Copy. Then go to the directory where you want to place the files, so C:\Windows\jws\Applications is showing on the left side of Explorer. Double click on the Applications folder and paste using File->Paste. [H:\jws\Applications if on campus] (5) Add these 3 existing java files to project File -> Add -> File... (select Applications.java from C:\Windows\jws\Applications folder) [H:\jws\Applications if on campus] Repeat for ApplicationsCentre.java and Student.java. StringTokenizer Example ----------------------- // Program: Tokens.java // This program reads in a line from the user, and then prints out // each token, one per line. import java.io.*; import java.util.*; public class Tokens { public static void main(String args[]) throws IOException { // Declare 'in' so that data can be read DataInputStream in = new DataInputStream (System.in); // Read in a line of input System.out.println ("Enter a line that has many tokens:"); String line = in.readLine(); // Declare StringTokenizer object made from line read in. StringTokenizer tokes = new StringTokenizer (line); // As long as there are more tokens in our 'tokes' object, // keep looping and printing out each token. while (tokes.hasMoreTokens()) { System.out.println ("Token: " + tokes.nextToken()); } // End of program System.out.println ("Program complete."); } } Question: How could this be modified to read in several lines of text that have tokens in these lines, until "quit" is entered? Sample Execution ---------------- Enter a line that has many tokens: This is a line that has many tokens in it. Token: This Token: is Token: a Token: line Token: that Token: has Token: many Token: tokens Token: in Token: it. Program complete. Constructing Objects & Running Methods on them ---------------------------------------------- There was not time to do this example. Instead we looked at how the ApplicationCentre object "appCentre" was constructed in Assignment #1. Note that the CD_Collection in the text book also helps to illustrate how objects are constructed and methods are run on objects. // Program: Store.java // This program reads data from the user and uses this to // construct an item object. It then calls methods on this // object to change its contents and to print it out. import java.io.*; public class Store { public static void main(String args[]) throws IOException { // Declare 'in' so that data can be read DataInputStream in = new DataInputStream (System.in); // Enter item name, price, and count. System.out.println ("Item name? "); String name = in.readLine(); System.out.println ("Item price? "); int price = Integer.parseInt (in.readLine()); System.out.println ("Item count? "); int count = Integer.parseInt (in.readLine()); // Construct an item object using the information entered Item theItem = new Item (name, price, count); // Change price of item System.out.println ("Enter new price: "); int newPrice = Integer.parseInt (in.readLine()); theItem.setPrice (newPrice); // Add more inventory System.out.println ("Number of stock ordered: "); int newStock = Integer.parseInt (in.readLine()); theItem.increaseInventory (newStock); // Display the object's data System.out.println ("\nSummarizing information about the item:"); System.out.println (theItem.toString()); } } // Class that represents a single item in the store. This class stores the // name and price of the item, as well as a count of how many of this item // are currently in stock. public class Item { // Data Declarations private String name; // name of item private int price; // cost of item private int count; // number of items in stock // Constructor public Item (String n, int cost, int number) { name = n; price = cost; count = number; } // Increase county by 'increase' amount public void increaseInventory (int increase) { count = count + increase; } // Change price to the price passed. public void setPrice (int newCost) { price = newCost; } // return string to represent Item object public String toString () { String result = "Name: " + name + "\n"; result = result + "Price: $" + price + "\n"; result = result + "Number in stock: " + count; return result; } } Sample Execution ---------------- Item name? Apple Item price? 20 Item count? 8 Enter new price: 25 Number of stock ordered: 2 Summarizing information about the item: Name: Apple Price: $25 Number in stock: 10