Source of ObjectData.java


  1: import java.util.Scanner;

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

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

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

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

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

 48:         // pause
 49:         System.out.print("Press Enter...");
 50:         kbd.nextLine();
 51:         System.out.println("\n\n\n\n");

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

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

 72:         // pause
 73:         System.out.print("Press Enter...");
 74:         kbd.nextLine();
 75:         System.out.println("\n\n\n\n");

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

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

111:         // pause
112:         System.out.print("Press Enter...");
113:         kbd.nextLine();
114:         System.out.println("\n\n\n\n");

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

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

133:         // pause
134:         System.out.print("Press Enter...");
135:         kbd.nextLine();
136:         System.out.println("\n\n\n\n");

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

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

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

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

171:         // pause
172:         System.out.print("Press Enter...");
173:         kbd.nextLine();
174:         System.out.println("\n\n\n\n");
175:     }

177: }