COSC212 - Java Programming (Weekend)
COSC212 - Java Programming (Weekend)
1. You are given the code for an abstract class Employee (12.5 marks)
1. // Employee.java
2. package employees;
3. public abstract class Employee{
4. // instance variable
5. private int payrollNo;
6.
7. // constructor
8. public Employee(int payrollNo){
9. this.payrollNo = payrollNo;
10. }// end of constructor
11.
12. // abstract method
13. public abstract double calculateSalary();
14.
15. // to string
16. public String toString(){
17. return String.format("Payroll No: %03d", payrollNo);
18. }// end of toString()
19. }// end of class Employee
2. Write a GUI class (Student.java) that will display the window shown in Figure 2.1. The
window (JFrame) has a fixed size of 205 by 200 pixels. The first two rows have a label
(JLabel) and a text box (JTextField) each. Each text box is wide enough for at least ten
characters. Following these is a check box (JCheckBox) labelled Registered. Next there is a
text area (JTextArea) capable of holding three lines of fifteen characters. The text can be
wrapped, see the hint below. Following that there are two buttons (JButton). One marked
Clear will clear the contents of the text area. One marked Ok will act as follows; names
may be entered into each edit box. The check box may or may not be checked. When the Ok
button is clicked a phrase like Muhammad Ali is registered or Muhammad Ali is not
registered will appear in the text area, depending upon the state of the check box, see Figure
2.2.
3. The following source code defines the class Top. (12.5 marks)
1. //Top.java
2.
3. public class Top{
4. // class variable
5. protected int x;
6.
7. //constructor
8. public Top(int x){
9. this.x = x;
10. } // end of constructor
11.
12. // process
13. public void process(){
14. x = 2 * x;
15. }// end of process
16.
17. // to string
18. public String toString(){
19. return String.format("In top: x is now %d", x);
20. }// end of toString()
21. }// end of class Top
Top has a subclass, Sub. Here is the source code for Sub.
1. //Sub.java
2.
3. public class Sub extends Top{
4. // instance variables
5. private int a;
6. private int b;
7.
8. // constructor
9. public Sub(int x, int a, int b){
10. super(x);
11. this.a = a;
12. this.b = b;
13. }
14.
15. // process
16. public void process(){
17. a = 3 * a;
18. b = 3 * b;
19. }// end of process
20.
21. // process
22. public void process(int num){
23. a = num + a;
24. b = num + b;
25. }// end of process
26.
27. // to string
28. public String toString(){
29. return String.format("In sub: x is now %d; a is now
%d; b is now %d",
30. x, a, b);
31. }// end of toString()
32. }// end of class Sub1
(a) Explain what it means when it is said that a class is a subclass of another.
(b) Line eight in the definition of Sub uses the keyword super. Explain what this line does.
(c) Give an example of method overloading from the code above. Explain how the
compiler distinguishes between two methods overloaded with the same name.
4. Examine the following abstract class source code, stored in the file Worker.java.
1. //Worker.java
2.
3. public abstract class Worker{
4. private String name;
5. private String type;//line worker, foreman,
6. //or engineer only
7. public String getName(){
8. return name;
9. }
10.
11. public String getType(){
12. return type;
13. }
14.
15. public void setName(String name){
16. this.name = name;
17. }
18.
19. public void setType(String type){
20. this.type = type;
21. }
22.
23. public Worker(String name, String type){
24. setName(name);
25. setType(type);
26. }
27.
28. public abstract double calculatePay();
29. }//end of class Worker
(d) Design a class LineWorker that extends Worker. The class should have the following:
(i) a constant named basicHours, given the value 40. This is the number of
hours to be worked before the employee starts earning at the overtime rate.
(ii) a double variable named basicRate. This is the normal amount earned by
an employee per hour. This varies with the employee.
(iii) a constant named overtimeMultiplier, given the value 1.5. This is the
amount by which the basicRate is multiplied to give the overtime rate.
(iv) an int variable named hoursWorked. This is the number of hours that an
employee works in a given week.
(v) a suitable constructor that initialises the above variables.
(vi) appropriate setters and getters.
(viii) an implementation of the calculatePay() method.
5. (a) What is an interface? Give details about any variables and methods that an interface may
contain. Illustrate with a simple example.
6. Consider the following shapes; Ellipse and Triangle. Each shape should have a name, a
method to compute its perimeter, and another method to compute its area. The name of each
shape should be displayed using getClass().getName() method. Design your inheritance
hierarchy with the common features in the superclass Shape (interface). Notice that the area
and perimeter are common to all Shapes, but we do not know how to compute the area or
perimeter for a general shape. The Ellipse class has a major and minor axes a and b,
respectively. The constructor should assign the largest value to a, and smallest to b. The area
and perimeters of an ellipse are:
Area = A = ab
The triangle has three instance variables a, b, and c. The formula for the area and
perimeter of a general Triangle with sides a, b, and c is given by
perimeter
Area = s(s a)(s b)(s c) , Perimeter = a b c , s =
2
The circle has radius, r. The formula for the area and perimeter of circle is given by:
Area = r 2 , perimeter = 2 r
The condition for any three positive values to make sides of a Triangle is a + b>c, b +
c> a, c + a >b. This condition MUST be checked inside the constructor. If it is not
satisfied, print an error message and terminate, otherwise make your Triangle object.
Make a Test class where you make objects from the different classes and store them in an
array of type Shape. Then make a loop and print the objects name, area, perimeter through
toString().