A courier company uses the following Java class to record information about packages that they deliver around New Zealand. Each package is uniquely identified by a barcode number. They also record the weight of the package in kilograms, its destination city and customer, and a contents indicator that is either N for normal, F for fragile, or E for explosive. public class ShippingItem { private int barcode; private String customer; private String city; private char contents; private double weight; public ShippingItem(int code, char cont, double wt) { barcode = code; customer = "Chh"; city = "Akl"; contents = cont; weight = wt; } void printSummary() { System.out.println(barcode + ":" + contents + "-" + weight + "-" + city); } void setCity(String ct) { city = ct; } } ====================================================================== 1. Understanding Classes and Objects [10 marks] What output does the following test class produce? public class ShippingTest { public static void main(String[] args) { ShippingItem c1 = new ShippingItem(3145, 'N', 67.5); c1.printSummary(); c1.setCity("Wel"); c1.printSummary(); c1 = new ShippingItem(4096, 'F', 310.0); c1.printSummary(); } } ANSWER: 3145:N-67.5-Akl 3145:N-67.5-Wel 4096:F-310.0-Akl ====================================================================== 2. Accessor methods: getters and setters [10 marks] Implement a method ``getBarcode'' that returns the barcode of a ShippingItem, and a method ``setBarcode'' that allows to set the barcode of a ShippingItem: ANSWER public int getBarcode() { return barcode;} public void setBarcode(int bc) { barcode = bc;} ====================================================================== 3. Text Output [10 marks] Implement a method ``printFull'' that outputs the full information about a ShippingItem on a single line, where all data member values are separated by commas (","). So e.g. the ShippingItem(3145, 'N', 67.5) of question 1 should print as: 3145,Chh,Akl,N,67.5 ANSWER public void printFull() { System.out.println(barcode + "," + customer + "," + city + "," + contents + "," + weight); } ====================================================================== 4. Constructor magic [25 marks] What output does the following set of class definitions produce? public class Test { public static void main(String[] args) { A a = new A(0); a.show(); B b = new B(3); b.show(); b = new B(); b.show(); } } public class A { protected int aValue; public A() {aValue = 1;} public A(int n) {aValue = n;} public void show() { System.out.println("A.aValue = " + aValue); } } public class B extends A { protected int bValue; public B() {super(2); bValue = 1;} public B(int n) {bValue = n;} public void show() { System.out.println("B.aValue = " + aValue); System.out.println("B.bValue = " + bValue); } } ANSWER A.aValue = 0 B.aValue = 1 B.bValue = 3 B.aValue = 2 B.bValue = 1 ====================================================================== 5. Debugging [20 marks] Mark all errors in the following class definition. Always explain what is wrong in a short single sentence each. ANSWERS are comments! public class Buggy { public static final int magicNumber = 42; private int privateMagicNumber = 0; public void Buggy() { // "void" constructors have no return type // or if somebody claims that this is a method: should be // lowercase "buggy()" then System.out.println("Creating Buggy instance"); magicNumber = magicNumber + 1; // cannot change a final variable } public getPrivateMagicNumber() { // no return type: should be "public int getPrivateMagicNumber() {" return privateMagicNumber; } public setPrivateMagicNumber(int i) { // no return type: should be "public int setPrivateMagicNumber() {" // given the code privateMagicNumber = i; return i; } public static void main(String[] args) { Buggy buggy = new Buggy(); buggy.setPrivateMagicNumber("4711"); // wrong argument type "4711", should be an int! // or: no method setPrivateMagicNumber(String v) is defined System.out.println( buggy.privateMagicNumber + "/" + Buggy.magicNumber); } } ====================================================================== 6. Command Line stuff [25 marks] Implement a class ``CountingEcho'' that allows for an arbitrary number of command line arguments. It should print out each command line argument on a separate line, prefixing each output line with the size of the respective string. Finally output the total number of command-line arguments supplied, and the sum of their string sizes. [Hint: the class String supplies a public method "length()" that returns the size of a string.] Here is the output of some example runs of the program: >java CountingEcho good day 4: good 3: day 2 arguments, 7 characters >java CountingEcho 0 arguments, 0 characters >java CountingEcho wreck a nice beach 5: wreck 1: a 4: nice 5: beach 4 arguments, 15 characters ANSWER public class CountingEcho { public static void main(String[] args) { int nChars = 0; for(int i = 0; i < args.length; i++) { int n = args[i].length(); nChars += n; System.out.println(n + ": " + args[i]); } System.out.println(args.length + " arguments, " + nChars + " characters"); } }