Tutorial 12 Lecturer: Craig MacDonald Tutorial: Mon@10:10am 971201 =========== CSC 108, Fall 1997 Tutorial notes, T12 ========================================================================== Topics ====== - Collect the project - (1) object oriented example - (2) 1-D array example - (3) 2-D array example - tracing a program (see material that was not finished in tutorial 11) - other examples (you might like to go thru these examples on your own) (1) Object Oriented Example --------------------------- Suppose you are writing a program to keep track of inventory in a warehouse. For each inventory item, you will store an identification number, and a count of the number of units currently in stock. The following operations can be performed on an Inventory Item: - record arrival: Record the fact that a given number of units of the Inventory Item have arrived at the warehouse. - record shipment: Record the fact that a given number of units of the Inventory Item have been shipped out of the warehouse. - number of units: Return the number of units of the Inventory Item that are currently in stock. Write a Java class for InventoryItem. Write a main program that would use this class to create an object, and that would call each of the three methods on this object. ----- class InventoryItem { private int itemID; // item ID number private int inStock; // number of items currently in stock // constructor public InventoryItem(int identifier) { itemID = identifier; inStock = 0; } public void recordArrival (int numUnits) { inStock = inStock + numUnits; } // note that we could add a check to this method to make sure // that the number of items in stock never becomes negative. public void recordShipment (int numUnits) { inStock = inStock - numUnits; } public int numberOfUnits () { return inStock; } } class WareHouse { public static void main (String[] args) { InventoryItem stereo = new InventoryItem (55); stereo.recordArrival (100); stereo.recordShipment (10); System.out.println ("Item 55: Currently "+ stereo.numberOfUnits()+" in stock."); } } 2) 1D Array Example ------------------- Write a method to reverse the elements in an array of integers. public static void reverset (int[] A) { // Subtle: Can't go all the way to the end! Why not? // Subtle: What if length is odd? for (int i=0; i biggestSum) biggestSum = thisSum; } return biggestSum; } ************************************************** Please see Tutorial 11 Fall 1997 notes for tracing examples. ************************************************** *************************************************** The Remainder of this page contains examples that were not discussed in the tutorial. You might want to work through these on your own. *************************************************** 4) Strings & StringTokenizers ----------------------------- Complete the method below. It is given (1) a String s containing a sequence of words separated by blanks, and (2) a reference to a String array called list. The method must construct an actual array for list to refer to (anything it may have formerly referred to is lost), and put one word from s into each slot of the array. No blanks should be stored in the array, either on their own or as part of a word. For full marks, the array must be constructed to be exactly the right size to store the number of words in s. public static void pullApartWords (String s, String[] list) SOLUTION: import java.io.*; import java.util.*; class TokenizerExample { // This method is passed a string 's' that contains several // words separated by spaces. It creates an array called // 'list' that contains each word in 's.' public static void pullApartWords (String s, String[] list) { // Make 's' a StringTokenizer object StringTokenizer words = new StringTokenizer (s); // Create an array to hold the tokens // ** Note that we cannot do "new" here, or else it loses // the reference from the main method. ** // list = new String[words.countTokens()]; int count =0; // Counts each token // As long as there are more tokens in 'words,' put // the next token into the 'list' while (words.hasMoreTokens()) { list[count] = new String (words.nextToken()); count++; } } // This method shows how the 'pullApartWords' method could be // used. It creates a string, and calls the 'pullApartWords' method // to create an array containing the words in this string. It // then prints out the words in this string. public static void main (String[] args) { String sentence = "This is the sentence to be broken up into tokens."; String[] tokenArray; // This array will store the tokens tokenArray = new String[new StringTokenizer(sentence).countTokens()]; // Takes each token in 'sentence' and puts it into the 'tokenArray' pullApartWords (sentence, tokenArray); // Output Section System.out.println ("The sentence is:"); System.out.println (" "+sentence); System.out.println (); System.out.println ("There are "+tokenArray.length+" tokens in the sentence."); System.out.println (); // Print each token in the 'tokenArray' for (int i=0; i=offset) set[n-offset] = true; } // Return true iff n is in the set. public boolean contains (int n) { if (n=offset) return (set[n-offset]); else return false; } // Return the proportion of integers between smallest and greatest // inclusive that are actually in the set. public float density () { int true_count = 0; for (int i=0; imax) max = list[i]; } return max; } // Returns the smallest integer in a list of integers private static int minInList (int[] list) { int min = list[0]; for (int i=1; i