• Tidak ada hasil yang ditemukan

657.209B Test Two 2001 JAVA 1

N/A
N/A
Protected

Academic year: 2023

Membagikan "657.209B Test Two 2001 JAVA 1"

Copied!
8
0
0

Teks penuh

(1)

657.209B Test Two 2001 JAVA 1

First Name: . . . . Last Name: . . . . Id Number: . . . .

Answer all questions. Write your answers on this test paper, in the space provided. You have 50 minutes to complete this test.

(2)

The definitions on this page are used in Questions 1 to 3.

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 contentsindicator that is eitherNfor normal, Ffor fragile, orEfor 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;

} }

2

(3)

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();

} }

(4)

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:

4

(5)

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

(6)

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);

} }

6

(7)

5. Debugging [20 marks]

Mark all errors in the following class definition. Always explain what is wrong in a short single sentence each.

public class Buggy {

public static final int magicNumber = 42;

private int privateMagicNumber = 0;

public void Buggy() {

System.out.println("Creating Buggy instance");

magicNumber = magicNumber + 1;

}

public getPrivateMagicNumber() { return privateMagicNumber;

}

public setPrivateMagicNumber(int i) { privateMagicNumber = i;

return i;

}

public static void main(String[] args) { Buggy buggy = new Buggy();

buggy.setPrivateMagicNumber("4711");

System.out.println( buggy.privateMagicNumber + "/" + Buggy.magicNumber);

} }

(8)

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

8

Referensi

Dokumen terkait

The results and discussion of cross-site data show that the marketing management of Vocational High Schools at SMK Negeri 1 Tamiang Layang and SMK Negeri 2 Tamiang

In this study, the authors develop a “gravity–underreporting modelling framework” that they used to analyse and compare: (i) data on the legal trade in