Blakes 21 Days Chapter 99 Document
- Internal Links
- Chapter 5 pages 125 to 156 | 123 - 95 = 31 pages | 13 is the mid way point for this chapter | Current Page 152 - 125 = 27 | Comments: 4 more pages till the end | reserve
- page 000 | reserve | reserve | reserve | reserve | reserve | reserve
- reserve | reserve | reserve | reserve | reserve | reserve | reserve
- reserve | reserve | reserve | reserve | reserve | reserve | reserve
- reserve | reserve | reserve | reserve | reserve | reserve | reserve
- reserve | reserve | reserve | reserve | reserve | reserve | reserve
- reserve | First Program | Second Program | Third Program | Fourth Program | Fifth Program | reserve | Summary | reserve
Day 0, Title Goes Here
Listing 4.1
- Top
- page 100
package com.java21days;
class HalfDollars {
public static void main(String[] arguments) {
int[] denver = {1_700_000, 4_600_000, 2_100_000 };
int[] philadelphia = new int[denver.length];
int[] total = new int[denver.length];
int average;
philadelphia[0] = 1_800_000;
philadelphia[1] = 5_000_000;
philadelphia[2] = 2_500_000;
total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2]) / 3;
System.out.print("2012 production: ");
System.out.format("%,d%n", total[0]);
System.out.print("2013 production: ");
System.out.format("%,d%n", total[1]);
System.out.print("2014 production: ");
System.out.format("%,d%n", total[2]);
System.out.print("Average production: ");
System.out.format("%,d%n", average);
}
}
Listing 4.1 (screenshot from NetBeans) goes here
- Note: I forgot to add the first line:
- but it still ran using the default package
- Top
- 100 The HalfDollars application uses three integer arrays to store production totals for U.S. half-dollar coins produced at the Denver and Philadelphia mints.
- When you run the program, it displays the output shown in Figure 4.1
- 100 The class created here, HalfDollars, has three instance variables that hold arrays of integers.
- The first, which is named denver, is declared and initialized on Line 5 to contain three integers:
- 1_700_000 in element 0
- 4_600_000 in element 1
- 2_100_000 in element 2
- Top
- 101 These figures are the total half-dollar production at the Denver mint for three years.
- The integers use an underscore character _ after every three digits to make the numbers more human-readable.
- The compiler ignores the underscores.
Figure 4.1 (the output pane) goes here
- Top
- 101 The second and third instance variables, philadelphia and total, are declared in lines 6 and 7.
- The philadelphia array contains the production totals for the Philadelphia mint, and total is used to store the overall production totals.
- 101 No initial values are assigned to the slots of the philadelphia and total arrays in lines 6 and 7.
- For this reason, each element is given the default value for integers: 0.
- 101 The denver.length variable is used to give both of these arrays the same number of slots as the denver array.
- Every array contains a length variable that you can use to keep track of the number of elements it contains.
- 101 The rest of the main() method of this application does the following:
- Line 8 creates an integer variable called average.
- Lines 10 - 12 assign new values to the three elements of the philadelphia array.
- Lines 14 - 16 assign new values to the elements of the total array.
- In line 14, total element 0 is given the sum of denver element 0 and philadelphia element 0.
- Similar expressions are used in lines 15 and 16.
- Line 17 sets the value of the average variable to the avaerage of the three total elements.
- Because average and the three total elements are integers, the average is expressed as an integer rather than a floating-point number.
- Lines 19 - 26 display the values stored in the total array and the average variable,
- using the System.out.format() method to display the numeric values in a more readable form using commas.
- Top
- 101 This application handles arrays inefficiently.
- The statements are almost identical, except for the subscripts that indicate the array element to which you are referring.
- If the HalfDollars application were being used to track 100 years of production totals instead of three, this approach would require a lot of redundant statements.
- 102 When dealing with arrays, you can use loops to cycle through an array's elements instead of dealing with each element individually.
- This makes the code a lot shorter and easier to read.
- When you learn about loops later today, you see a rewrite of the current example.
Reserve
Everything below this, is from a previous chapter.
Listing 3.2
- Top
- page 073
package com.java21days;
import java.awt.Point;
class PointSetter {
public static void main(String[] arguments) {
Point location = new Point(4, 13);
System.out.println("Starting location:");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
System.out.println("\nMoving to (7, 6)");
location.x = 7;
location.y = 6;
System.out.println("\nEnding location: ");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
}
}
Listing 3.2 (screenshot from NetBeans) goes here
- Top
- 074 When you run this application, the output should match Figure 3.2
- reserve
Figure 3.2 (screenshot from NetBeans Output pane) goes here
- Top
- 074 In this application, you create an instance of Point where x equals 4 and y equals 13 (line 8).
- These individual values are retrieved using dot notation.
- 074 The value of x is changed to 7 and y to 6 (lines 15-16).
- The values are displayed again to show how they have changed.
Listing 3.3
- Top
- page 076
package com.java21days;
class StringerChecker {
public static void main(String[] arguments) {
String str = "A Lannister always pays his debts";
System.out.println("The string is: " + str);
System.out.println("Length of this string: "
+ str.length() );
System.out.println("The character at position 6: "
+ str.charAt(6) );
System.out.println("The substring from 12 to 18: "
+ str.substring(12, 18) );
System.out.println("The index of the first 't': "
+ str.indexOf('t') );
System.out.println("The index of the begining of the "
+ "substring \"debts\": " + str.indexOf("debts") );
System.out.println("The string in uppercase: "
+ str.toUpperCase() );
}
}
- 076 Running the program produces the output shown in Figure 3.3
- reserve
Figure 3.3 Goes Here
- Top
- 076 In line 6, you create a new instance of String by using the string literal "A Lannister always pay his debts".
- The remainder of the program simply calls different string methods to do different operations on that string:
- Line 7 prints the value of the string
- Line 9 calls the length() method in the new String object to find out how many characters it contains
- 077
- continue
- Line 11 calls the charAt() method, which returns the character at the given position in the string.
- Note that string positions start at position 0 rather than 1
- so the character at position 6 is 'i'.
- Line 13 calls the substring() method, which takes two integers indicating a range and returns the substring with those starting and ending points.
- The substring() method also can be called with only one argument, which returns the substring from that position to the end of the string.
- Line 15 calls the indexOf() method, which returns the position of the first instance of the given character.
- Character literals are surrounded by single quotation marks
- so the argument is 't' (not "t").
- Line 17 shows a different use of the indexOf() method, which takes a string argument and returns the index of the beginning of that string.
- String literals always are surrounded by double quotation marks.
- Line 19 uses the toUpperCase() method to return a copy of the string in all uppercase.
- 077 NOTE: If you compare the output of the StringChecker application to the characters in the string,
- you might be wondering how 't' could be at position 6 when it is the seventh character in the string.
- All of the methods look like they're off by one (except for length()).
- The reason is that the methods are zero-based, which means they begin counting with 0 instead of 1.
- So 'A' is at position 0, a space at position 1, 'L' at position 2 and so on.
- This kind of numbering is something you encounter often in Java.
Listing 3.4
- Top
- page 080-081
package com.java21days;
import java.awt.Point;
class RefTester {
public static void main(String[] arguments) {
Point pt1, pt2;
pt1 = new Point(100, 100);
pt2 = pt1;
pt1.x = 200;
pt1.y = 200;
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
}
}
Listing 3.4 (screenshot from NetBeans) goes here
- Top
- 081 Save and run the application. The output is shown in Figure 3.4
Figure 3.4 displays the Ouput pane