Chapter 5 Code Snippets ======================= p. 140, Annotated Java Code Example package shapes; import java.lang.*; /* This class defines a shape with three dimensions */ public class Box extends Shape { int height; // override the Shape height int depth; // unique to Box public Box() { height = 4; width = 3; // width is inherited from Shape depth = 2; } public int getDepth() { return depth; } public void setDepth(int newDepth) { depth = newDepth; } // super.getWidth is the same as getWidth() here public int getVolume() { return height * super.getWidth() * getDepth(); } } -------------------------- p. 143, Subclassing (extends) example: package shapes; public class Shape { int height; int width; public Shape() { height = 1; width = 1; } public int getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } // getWidth() and setWidth() methods go here } -------------------------- p. 146, Annotated Use of the Box Example Class package shapes; public class TestBox { public static void main(String[] args) { // this creates an object called someBox Box someBox = new Box(); someBox.setDepth(3); // getHeight() and setHeight() are from Shape // height shows the height variable from Box System.out.println ( "The height of Shape is " + someBox.getHeight() + " and of someBox is " + someBox.height); // getDepth and getVolume are from Box System.out.println ( "The depth of someBox is " + someBox.getDepth() + " and the volume of someBox is " + someBox.getVolume()); } } -------------------------- p. 149, Conditional Branching class ShowQuarter { public static void main (String args[]) { int taxMonth = 10; String taxQuarter; if (taxMonth == 1 || taxMonth == 2 || taxMonth == 3) { taxQuarter = "1st Quarter"; } else if (taxMonth == 4 || taxMonth == 5 || taxMonth == 6) { taxQuarter = "2nd Quarter"; // more conditions would appear here } else { taxQuarter = "Not Valid"; } System.out.println("Your current Tax Quarter is: " + taxQuarter ); } } -------------------------- Ibid. class ShowQuarter2 { public static void main (String args[]) { int taxMonth = 10; String taxQuarter; // The break statement jumps out of the conditional testing switch (taxMonth) { case 1: case 2: case 3: taxQuarter = "1st Quarter"; break; case 4: case 5: case 6: taxQuarter = "2nd Quarter"; break; // more conditions would appear here default: taxQuarter = "Not Valid"; } // end of the switch System.out.println("Your current Tax Quarter is: " + taxQuarter); } // end of the main() method } // end of the class -------------------------- p. 150, Iteration or Looping class TestLoops { public static void main (String args[]) { for (int i = 1; i <= 10; i++) { System.out.println("Loop 1 count is " + i); } } } -------------------------- p. 150, Exception Handling public class TestException { public static void main(String[] args) { int numerator = 5, denominator = 0; int ratio; try { ratio = numerator / denominator ; System.out.println("The ratio is " + ratio); } catch (Exception e) { // This shows an error message on the console e.printStackTrace(); } finally { System.out.println("The end."); } } } -------------------------- p. 151, Variable Scope class TestScope { public static void main (String[] args) { int currentSalary = 0; if (currentSalary < 0) { int currentCommission = 10; System.out.println("No salary but the commission is " + currentCommission); } else { System.out.println("Salary but no commission."); } // This will cause a compilation error. System.out.println(currentCommission); } } -------------------------- Ibid. class ShowSalary { static int previousSalary = 0; int commission = 10; public static void main (String[] args) { int currentSalary = 100; if (currentSalary == 0) { System.out.println("There is only a commission."); } else { System.out.println("Current salary is " + currentSalary); } System.out.println("{Previous salary is " + previousSalary); // The following would cause a compile error. // System.out.println(commission); } } -------------------------- p. 153, top of page class TestShowSalary { public static void main(String[] args) { ShowSalary salary1 = new ShowSalary(); ShowSalary salary2 = new ShowSalary(); // System.out.println("From salary1: " + salary1.previousSalary); salary2.previousSalary = 300; System.out.println("After salary2 changed it: " + salary1.previousSalary); } } -------------------------- p. 154, Primitive Datatypes // decimal equivalent of the letter 'a' char charDecimal = 97; // using an actual character inside single quotes char charChar = 'a'; // octal equivalent of the letter 'a' char charOctal = '\141'; // Hex value for the letter 'a' char charHex = 0x0061; // Unicode (hex) value for the letter 'a' char charUnicode = '\u0061'; -------------------------- p. 155 class PetNames { public static main (String args[]) { String petFriends[ ][ ] = { {"George", "Snake", "Alligator"}, {"Denise", "Butterfly"}, {"Christine", "Tiger"}, {"Robert", "Parrot", "Dove", "Dog", "Cat"} }; } } -------------------------- p. 157, StringBuffer Class class StringAppend { public static void main (String args[]) { StringBuffer stringBuff = new StringBuffer("A string"); stringBuff = stringBuff.append(" is added"); System.out.println(stringBuff.toString()); } } -------------------------- p. 158, top of page public class TestCast { public static void main (String args[]) { byte smallNumber = 10; int largeNumber; largeNumber = smallNumber * 5; System.out.println("largeNumber is " + largeNumber); // smallNumber = largeNumber; smallNumber = (byte) largeNumber; System.out.println("smallNumber is " + smallNumber); } } -------------------------- p. 162, step 4 package shapes.client; public class Shape { int height; int width; public Shape() { height = 1; width = 1; } public int getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public int getWidth() { return width; } public void setWidth(int newWidth) { width = newWidth; } } -------------------------- p. 164, step 4 package shapes.client; public class Box extends Shape { int height; // override the Shape height int depth; // unique to Box public Box() { height = 4; width = 3; // the width from Shape depth = 2; } public int getDepth() { return depth; } public void setDepth(int newDepth) { depth = newDepth; } // super.getWidth is the same as getWidth() here public int getVolume() { return height * super.getWidth() * getDepth(); } } -------------------------- p. 165, step 3 package shapes.client; public class TestBox { public static void main(String[] args) { // this creates an object called someBox Box someBox = new Box(); someBox.setDepth(3); // getHeight() and setHeight() are from Shape // height shows the height variable from Box System.out.println ( "The height of Shape is " + someBox.getHeight() + " and of someBox is " + someBox.height); // getDepth and getVolume are from Box System.out.println ( "The depth of someBox is " + someBox.getDepth() + " and the volume of someBox is " + someBox.getVolume()); } } -------------------------- p. 166, samples package sample; public class WrappedClass { public WrappedClass() { } int sumNumbers(int number1, int number2, boolean reallySum) { int result = 0; if (reallySum){ result = number1 + number2; } return result; } } package sample; public class WrapperClass { private final WrappedClass contents; public WrapperClass(WrappedClass newContents) { contents = newContents; } public int sumNumbers(int number1, int number2) { return contents.sumNumbers(number1, number2, true); } } --------------------------