Source of ObjectData.java


  2: import java.util.Scanner;

  4: /**
  5:  * A program to show how objects of the same class can hold different data.
  6:  *
  7:  * @author Mark Young (A00000000)
  8:  */
  9: public class ObjectData {

 11:     public static void main(String[] args) {
 12:         // create some variables
 13:         Scanner kbd = new Scanner(System.in);   // one Scanner
 14:         String firstName = "Mark";  // one String
 15:         String lastName = "Young";  // another String

 17:         // Introduce yourself
 18:         Utilities.printTitle("Object Data Demo");
 19:         Utilities.printParagraph("This program creates String and Scanner "
 20:                 + "objects and calls methods on them. "
 21:                 + "Each object has different data that it works with, "
 22:                 + "and so the results can be different "
 23:                 + "even if the method calls are the same.");

 25:         // Use some canned data
 26:         Utilities.printParagraph("For example, "
 27:                 + "my first name is " + firstName + ", "
 28:                 + "and my last name is " + lastName + ". "
 29:                 + "Thus:");
 30:         System.out.println(""
 31:                 + "\tfirstName == \"" + firstName + "\".\n"
 32:                 + "\tlastName == \"" + lastName + "\".\n");
 33:         Utilities.printParagraph("When I ask firstName what its length is "
 34:                 + "(firstname.length()), it returns " 
 35:                 + firstName.length() + ". "
 36:                 + "But when I ask lastName what its length is "
 37:                 + "(lastName.length()), it returns " 
 38:                 + lastName.length() + ".");

 40:         // Use some user data
 41:         System.out.print("What is YOUR first name? ");
 42:         firstName = kbd.nextLine();
 43:         System.out.print("And what is your LAST name? ");
 44:         lastName = kbd.nextLine();
 45:         System.out.println();
 46:         Utilities.printParagraph("Repeating that for your name "
 47:                 + "(" + firstName + " " + lastName + "), "
 48:                 + "firstName.length() returns " + firstName.length() + ", "
 49:                 + "and lastName.length() returns " + lastName.length() + ".");

 51:         // pause
 52:         System.out.print("Press Enter...");
 53:         kbd.nextLine();
 54:         System.out.println("\n\n\n\n");

 56:         // Now, Scanners!
 57:         String line = "10 20 30 40";    // but first, yet another String

 59:         Utilities.printParagraph("We can do similar things with Scanners. "
 60:                 + "Our usual Scanner is declared like this:");
 61:         System.out.println("\tScanner kbd = new Scanner(System.in);\n");
 62:         Utilities.printParagraph("Thus when we ask kbd for the \"next int\" "
 63:                 + "(kbd.nextInt()), "
 64:                 + "it gets that int from System.in "
 65:                 + "(that is, the keyboard). "
 66:                 + "And similarly for next double, next line, and so on.");
 67:         Utilities.printParagraph("But we can make Scanners "
 68:                 + "that read from other locations. "
 69:                 + "Next term (in 1228) "
 70:                 + "we'll make Scanners that read from files. "
 71:                 + "Right now, let's make a Scanner that reads from a String. "
 72:                 + "First we need to create the String. "
 73:                 + "We'll use the String line == \"" + line + "\", "
 74:                 + "which has some integer numerals on it.");

 76:         // pause
 77:         System.out.print("Press Enter...");
 78:         kbd.nextLine();
 79:         System.out.println("\n\n\n\n");

 81:         // create our String scanner
 82:         Scanner str = new Scanner(line);    // another Scanner!

 84:         // report on how it's created and used
 85:         Utilities.printParagraph("The Scanner is created much like kbd is, "
 86:                 + "but instead of saying System.in, "
 87:                 + "we put the String we want to use. ");
 88:         System.out.println("\tScanner str = new Scanner(line);\n");
 89:         Utilities.printParagraph("By replacing System.in "
 90:                 + "with a String variable, "
 91:                 + "we are telling the computer "
 92:                 + "that we want THIS Scanner "
 93:                 + "to get its data from THAT String. "
 94:                 + "And so str will NOT read from the keyboard; "
 95:                 + "it will read from the String that line contains. "
 96:                 + "(Note: it reads from \"" + line + "\", "
 97:                 + "because that's the String that line contains "
 98:                 + "when the Scanner was created. "
 99:                 + "Changing the value of the variable line "
100:                 + "won't change the String the Scanner is reading from!)");
101:         Utilities.printParagraph("So when we ask str for the next int "
102:                 + "(str.nextInt()), "
103:                 + "it returns " + str.nextInt() + ". "
104:                 + "When we ask for the next int after that "
105:                 + "(str.nextInt() again), "
106:                 + "it returns " + str.nextInt() + ".");
107:         Utilities.printParagraph("Note that it's str we're asking "
108:                 + "for the next int, not kbd! "
109:                 + "kbd will continue to read from System.in, "
110:                 + "just like we told it to. "
111:                 + "And that's a good thing, "
112:                 + "because we're going to be asking you to type more stuff in "
113:                 + "in just a few moments.");

115:         // pause
116:         System.out.print("Press Enter...");
117:         kbd.nextLine();
118:         System.out.println("\n\n\n\n");

120:         // Scanner for String the user enters
121:         Utilities.printParagraph("And just like with our names above, "
122:                 + "we can use data the user enters in our code. "
123:                 + "All we need to do is ask the user to enter the String "
124:                 + "(and HOPE that the user co-operates "
125:                 + "by entering the correct kind of String).");

127:         // get user's line
128:         System.out.print("Please enter a String "
129:                 + "starting with at least three int values: ");
130:         String userLine = kbd.nextLine();
131:         System.out.println();
132:         Utilities.printParagraph("The String you entered was "
133:                 + "\"" + userLine + "\", which we called userLine. "
134:                 + "(If that line doesn't start with three int values, "
135:                 + "then this program will very soon crash!)");

137:         // pause
138:         System.out.print("Press Enter...");
139:         kbd.nextLine();
140:         System.out.println("\n\n\n\n");

142:         // create Scanner for user's line
143:         Scanner scanLine = new Scanner(userLine);   // yet another Scanner

145:         Utilities.printParagraph("Before we can read the numbers "
146:                 + "from that line, "
147:                 + "we need to create a new Scanner:");
148:         System.out.println("\tScanner scanLine = new Scanner(userLine);\n");

150:         // check for bad input off the bat
151:         if (!scanLine.hasNextInt()) {
152:             Utilities.printParagraph("Oh, I can tell now "
153:                     + "that your input doesn't even have ONE int at the start. "
154:                     + "You disappoint me, " + firstName + ". "
155:                     + "I guess the program is going to crash....");
156:             Utilities.printParagraph("Note that the program is going to crash "
157:                     + "because we ask for the next int from a string "
158:                     + "that doesn't have a next int. "
159:                     + "The Scanner itself is created just fine.");

161:             // pause
162:             System.out.print("Press Enter...");
163:             kbd.nextLine();
164:             System.out.println("\n\n\n\n");
165:         }
166:         Utilities.printParagraph("Now we simply ask for the values, "
167:                 + "using the usual method (nextInt), "
168:                 + "but addressed to the new Scanner (scanLine). "
169:                 + "Assuming you did as you were asked, "
170:                 + "the first three calls to scanLine.nextInt() return "
171:                 + scanLine.nextInt() + ", "
172:                 + scanLine.nextInt() + ", and "
173:                 + scanLine.nextInt() + ". ");

175:         // pause
176:         System.out.print("Press Enter...");
177:         kbd.nextLine();
178:         System.out.println("\n\n\n\n");

180:         // Scanner for String the user enters
181:         Utilities.printParagraph("Finally, we can use the hasNextInt() method "
182:                 + "to read all the values on a lines of just int values. "
183:                 + "We just say "
184:                 + "\"while the Scanner has an int next, read that int\". "
185:                 + "Thus when we want the user to enter a bunch of numbers, "
186:                 + "we can have them enter all the numbers on one line, "
187:                 + "and we won't need to use a sentinel value "
188:                 + "(like -1) at all!");

190:         // get user's line
191:         System.out.print("Please enter a line "
192:                 + "with just int values on it: ");
193:         userLine = kbd.nextLine();
194:         System.out.println();
195:         Utilities.printParagraph("The String you entered was "
196:                 + "\"" + userLine + "\", "
197:                 + "which we stored in our variable userLine. ");

199:         // pause
200:         System.out.print("Press Enter...");
201:         kbd.nextLine();
202:         System.out.println("\n\n\n\n");

204:         // create Scanner for user's line
205:         scanLine = new Scanner(userLine);   // yet another Scanner object

207:         Utilities.printParagraph("Before we can read the numbers "
208:                 + "from that line, "
209:                 + "we need to create a new Scanner:");
210:         System.out.println("\tscanLine = new Scanner(userLine);\n");
211:         Utilities.printParagraph("(We can use the same Scanner variable, "
212:                 + "because we're finished scanning the earlier line. "
213:                 + "This new Scanner object replaces the old Scanner object, "
214:                 + "but uses the same variable to refer to it.)");

216:         // check for bad input off the bat
217:         String results = "The numbers you entered were ";
218:         int sum = 0;
219:         while (scanLine.hasNextInt()) {
220:             int num = scanLine.nextInt();
221:             sum += num;
222:             results += num + " and ";
223:         }
224:         results += "that was the last number "
225:                 + "(unless there was a word after it). ";
226:         Utilities.printParagraph(results);
227:         Utilities.printParagraph("By the way, "
228:                 + "the sum of all those numbers was " + sum + ".");

230:         // pause
231:         System.out.print("Press Enter...");
232:         kbd.nextLine();
233:         System.out.println("\n\n\n\n");
234:     }

236: }