Program 8.10 class Books { int pages=1500; public void pagemessage() { System.out.println("Number of pages: "+pages); } }

EXERCISE 2: OUTPUT AND ERRORS TRACING [LATIHAN 2: SURIHAN HASIL DAN RALAT] 1. Trace the output for Program 8.10. Understand what each line of the stat...
Author: Zoe Porter
8 downloads 0 Views 308KB Size
EXERCISE 2: OUTPUT AND ERRORS TRACING [LATIHAN 2: SURIHAN HASIL DAN RALAT] 1. Trace the output for Program 8.10. Understand what each line of the statement in the program does. Based on the program, draw the class diagram. In your own words, explain why such output is printed. [Kompil dan laksanakan Aturcara 8.10. Fahamkan apa penyataan pada setiap baris di dalam aturcara tersebut lakukan. Berdasarkan aturcara tersebut, lukis Rajah Kelas. Di dalam ayat kamu sendiri, terangkan kenapa hasil tersebut dicetak.] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

//Program 8.10 class Books { int pages=1500; public void pageMessage() { System.out.println("Number of pages:  "+pages); } } class Dictionary extends Books { private int definitions = 52500; public void definitionMessage() { System.out.println("Number of definitions:  "+definitions); System.out.println("Definitions per page:  "+definitions/pages); } }          public class Words { public static void main(String[]arg) { Dictionary webster = new Dictionary(); webster.pageMessage(); webster.definitionMessage(); } }

2. Trace the output for Program 8.11. Understand what each line of the statement in the program does. Based on the program, draw the class diagram. In your own words, explain why such output is printed.

[Kompil dan laksanakan Aturcara 8.11. Fahamkan apa penyataan pada setiap baris di dalam aturcara tersebut lakukan. Berdasarkan aturcara tersebut, lukis Rajah Kelas. Di dalam ayat kamu sendiri, terangkan kenapa hasil tersebut dicetak.] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

//Program 8.11 class X { private int d; public X(int c) { d = c; } public int f() { return d+1; } } class Y extends X { private int g; public Y (int s,int t) { super(s); g = t; } public int h() { return g-1; } } class Test { public static void main( String[] args) { X e = new X(3); Y n = new Y(2,7); System.out.println(e.f()); System.out.println(n.f()); System.out.println(n.h()); } }

3. Trace the output for Program 8.12. Understand what each line of the statement in the program does. Based on the program, draw the class diagram. In your own words, explain why such output is printed. [Surih hasil Aturcara 8.12. Fahamkan apa penyataan pada setiap baris di dalam aturcara tersebut lakukan. Berdasarkan aturcara tersebut, lukis Rajah Kelas. Di dalam ayat kamu sendiri, terangkan kenapa hasil tersebut dicetak.] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

//Program 8.12 public class Messages { public static void main(String[]arg) { Thought territory = new Thought(); Advice cliche = new Advice(); territory.message(); cliche.message(); } } class Thought { public void message() { System.out.println("Some people get lost in thought”); } } class Advice extends Thought { public void message() {System.out.println("Avoid cliches like the plague"); } }

Now, trace the output for Program 8.13. What would be the output. In your own words, explain why

such output is printed. [ Sekarang, surih hasil Aturcara 8.13. Apakah hasilnya? Di dalam ayat kamu sendiri, terangkan kenapa hasil tersebut dicetak.] 1 2 3 4 5 6 7

//Program class { {

8.13 Messages public static void main(String[]arg) Thought territory = new Thought(); Advice cliche = new Advice(); territory.message(); cliche.message();

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

} } class Thought { public void message() { System.out.println("Some people get lost in thought”); } } class Advice extends Thought { public void message() { System.out.println("Avoid cliches like the plague"); super.message(); } }

4. Trace the output for Program 8.14. Understand what each line of the statement in the program does. In your own words, explain why such output is printed. [Surih hasil Aturcara 8.14. Fahamkan apa penyataan pada setiap baris di dalam aturcara tersebut lakukan. Berdasarkan aturcara tersebut, lukis Rajah Kelas. Di dalam ayat kamu sendiri, terangkan kenapa hasil tersebut dicetak.] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

//Program class { {

8.14 TestFinal public static void main(String[]arg) Final obj=new Final(); Final1 obj1=new Final1(); obj.sum(); obj1.sum();

} } class Final { int a = 60; public void sum() { int total=a+80; System.out.println(total); } } class Final1 extends Final { int a=70; public void sum() { int total=a + super.a + 80; System.out.println(total);

}}

5. Based on the following program; [Berdasarkan atur cara berikut] i. Draw the UML class diagram that shows the relationship among classes. [Lukis rajah kelas UML yang menunjukkan hubungan antara kelas] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

//Program 8.15

class A { public A() { System.out.println( "Default constructor A is invoked"); } public A(String x) { System.out.println(x); } } class B extends A { public B() { this ("Another constructor of B is invoked"); System.out.println( "Default constructor of B is invoked"); } public B(String s) { System.out.println (s); } public B(String s, String x) { super(x); System.out.println (s); } }

ii. Assume that the driver class is as below, what is the output if the instantiation of object at line 3 is as follows. If error, then state the reason for the error. [Andaikan kelas pemacu adalah seperti berikut, nyatakan output setelah objek-objek (i,ii,iii,iv) dicipta pada baris 3. Jika terdapat ralat, nyatakan sebabnya ] a. B b1 = new B(); b. A b1 = new A(); c. B b1 = new B("Thank you"); d. B b1 = new B("Thank you", "Come again"); 1 public class Test1 { 2 public static void main(String[] args) 3 ____________________; 4 }

{

5 } 6. Find the error in the Program 8.16. Correct the error. [Kenalpasti ralat di dalam Aturcara 8.16. Betulkan ralat tersebut.] 1 2 3 4 5 6 7 8 9

//Program 8.16 public class ClassB extends ClassA { public ClassB() { int init = 10; super(40); } }

7. Look at the following code. The method in line _______ will override the method in line ____. [ Metod pada baris ______ akan override metod pada baris _________. ] 1 2 3 4 5 6 7 8 9 10 11 12 13 14

//Program 8.17 public class ClassA { public ClassA() {} public int method1(int a){} public double method2(int b){} } public ClassB extends ClassA { public ClassB(){} public int method1(int b, int c){} public double method2(double c){} }

8. What is wrong with the following subclass definition?. [ Apakah kesilapan yang terdapat pada pengistiharan kelas sub berikut?.] 1 2 3 4 5 6 7 8 9

//Program 8.18 public class ClassB extends ClassA { public ClassB() { super(40); System.out.println("The last statement."); } }

9. Based on Program 8.19, which method will be executed as a result of the following statements? ClassB item1 = new ClassA(); item1.method1(); [ Boleh kah Aturcara 8.19 berikut di kompil? Sekiranya tidak boleh dikompil, berikan sebabsebabnya. Seterusnya, tuliskan versi aturcara yang boleh dikompil.] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

//Program 8.19 public class ClassA { public ClassA() {} public void method1(int a){} } public class ClassB extends ClassA { public ClassB(){} public void method1(){} } public class ClassC extends ClassB { public ClassC(){} public void method1(){} }

10. State whether the following program can be compiled? If the program cannot be compiled, give reasons and write the correct program. [Nyatakan samada aturcara berikut boleh dikompil Jika aturcara tidak dapat dikompil, beri sebab dan kemudian tulis atur cara yang betul.] 1 2 3 4 5 6 7 8 9 10 11 12 13 14

//Program 8.20 public class TestVehicle { public static void main (String[] args) { Car car1 = new Car(45000, 4); } } class Vehicle{ private double cost; public Vehicle (double c) { cost = c; System.out.println ("Cost = RM"+cost); }

15 16 17 18 19 20 21 22 23 24 25

} class Car extends Vehicle { private int passengers; public Car(double c, int p) { passengers = p; System.out.println ("Number of passengers:"+passengers); } }

11. Based on Program 8.21; Which method1 will be executed as a result of the following statements? [Berdasarkan Aturcara 8.21, method1 yang mana satukah yang akan setelah pernyataan berikut dilaksanakan: ] ClassA item1 = new ClassC(); item1.method1(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

//Program 8.21 public class ClassA { public ClassA() {} public void method1(){} } public class ClassB extends ClassA { public ClassB(){} public void method1(){} } public class ClassC extends ClassB { public ClassC(){} public void method1(){} }

12. Identify errors in the Program 8.22 below. You are required to give a brief explanation on each of the identified error and fix the error. [Kenalpasti ralat-ralat yang terdapat di dalam Aturcara 8.22. Sila terangkan secara ringkas ralatralat yang dikenalpasti dan betulkan ralat-ralat tersebut.] 1 2 3

//Program 8.22 public class Circle{ private double radius;

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

public Circle(double _radius) { radius=_radius; } public double getRadius() { return radius; } public double getArea() { return radius*radius*Math.PI; } } class B extends Circle { private double length; B(double radius, double length) { Circle(radius); length = _length; } public double getArea() { return getArea() * length; } }

13. Based on Program 8.23, identify which showValue function applies the overiding and overloading concept. Trace the output of the program. [ Berdasarkan Aturcara 8.23, kenalpasti fungsi showValue yang mana yang mengaplikasikan konsep overiding dan overloading.Surih hasil aturcara tersebut.]

1 2 3 4 5 6 7 8 9 10 11

//Program 8.23 public class Superclass3 { public void showValue(int a) { System.out.println(a); } public void showValue(String a) { System.out.println(a); } }

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

public class Subclass3 extends Superclass3 { public void showValue(int ar) { System.out.println("The subclass: "+ar); } public void showValue(double a) { System.out.println(a); } } public class ShowValueDemo { public static void main(String[]a) { Subclass3 obj=new Subclass3(); obj.showValue(8); obj.showValue(6.5); obj.showValue("Test"); } }

14. Consider program 8.24 (TestClass1 class) and program 8.25 (TestClass2 class). [Perhatikan aturcara 8.24 (kelas TestClass1) dan aturcara 8.25 (kelas TestClass2).] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

//Program 8.24 public class TestClass1 { public static void main(String[] args) { A a = new A(); a.p(10); } } class B { public void p(int i) { System.out.println(i + " in Class B"); } } class A extends B { public void p(int i) { System.out.println(i + " in Class A"); } }

1 //Program 8.25 2 public class TestClass2 { 3 public static void main(String[] args) {

4 5 6 7 8 9 10 11 12 13 14 15 16 17

A a = new A(); a.p(10); } } class B { public void p(int i) { System.out.println(i + " in Class B"); } } class A extends B { public void p(double i){ System.out.println(i + " in Class A"); } }

i.

Trace the program and state the output of Program 9.8 and Program 9.9. [Jejak aturcara dan nyatakan output bagi Aturcara 9.8 dan Aturcara 9.9.]

ii.

Determine which program consists of method overloading and which consists of method overriding. [Tentukan aturcara yang mana mengandungi metod sarat dan yang mana mengandungi metod langkau.]

iii.

Define the difference between method overriding and method overloading. [Terangkan perbezaan antara metod langkau dan metod sarat.]

PROGRAMMING 1.

Figure 8.1 : Employee Class

Based on the Figure 8.1: [Berdasarkan kepada Rajah 8.1:] i.

Write a program for the Employee class. The class should have the following information in member variables: [Tulis sebuah aturcara untuk kelas Employee. Kelas tersebut perlu mempunyai maklumat ahli

data berikut:] 

Employee name [Nama pekerja]



Employee number [Nombor pekerja]



Hire date [Tarikh mula bekerja]

Write one or more constructers and the appropriate accessor and mutator functions for the class. [Tulis satu atau lebih konstruktor dan fungsi pencapai dan mutator yang bersesuaian untuk kelas tersebut.] ii.

Write a program for the ProductionWorker class. The ProductionWorker class should have member variables to hold the following information: [Tulis satu aturcara untuk kelas ProductionWorker. Kelas tersebut perlu mempunyai maklumat ahli data berikut;]  

Shift (an integer) [Giliran (berjenis integer)] Hourly pay rate (a double) [Kadar bayaran per jam (berjenis double)]

The work is divided into two shifts: day and night. The shift variable will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night