0% found this document useful (0 votes)
9 views

COSC212 - Java Programming (Weekday)

These are past questions for the Java Programming course at Ahmadu Bello University for the weekday of 2021.

Uploaded by

FG na Terrorists
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

COSC212 - Java Programming (Weekday)

These are past questions for the Java Programming course at Ahmadu Bello University for the weekday of 2021.

Uploaded by

FG na Terrorists
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DISTANCE LEARNING CENTRE

Ahmadu Bello University, Zaria


BSc. Computer Science (200 Level)
2021/2022 January Semester Examination (WEEKDAY GROUP)
Course Code & Title: COSC 212 – Object Oriented Programming II

Instruction: Answer four (4) questions.


Time Allowed: 2 Hours 30 minutes

1. Define a class named AClass in a package pA . This class will contain two public int
fields a and b and an abstract method compute(). Now define three classes named
ClassB , ClassC and ClassD in packages pB , pC and pD , respectively. Each of the
classes is a subclass of AClass and will implement the compute() method. In ClassB it
will compute the sum of a and b; in ClassC it will compute the product of a and b; and in
ClassD it will compute the difference of a and b. Now write a Java program that will
generate calls to the compute() method of each subclass.
(12.5 marks)

2. Items may be ordered from a store according to some measure, for example by weight or by
length. Other items may be ordered by quantity. You are to write classes that will manage
these cases. (12.5
Marks)
(a) Write the code for an abstract class called Item. It should have the following.
(i) Two instance fields itemName (String) and unitPrice (double).
(ii) Getters for these fields.
(iii) A constructor that sets the values for these fields.
(iv) The abstract method calculatePrice() that returns a double.
(v) A toString() method.
(b) Write the code for an OrderByMeasure class that is a subclass of the Item class. Objects
instantiated from this class will be items that are sold by some measure, for example
kilograms or metres. This class should have the following.
(i) Two instance fields: amount (double) and unitOfMeasure (String). The first
would be the amount purchased and the second would be kg or m or whatever.
(ii) Getters for these two fields.
(iii) A constructor that would set the values for these fields. It would also need to
set the values of the superclass fields.
(iv) The calculatePrice () method must be implemented.
(v) A toString() method.

(c) Write the code for an OrderByQuantity class that is also a subclass of the Item
class. Objects instantiated from this class will be items that are sold by quantity. This
class will be very similar to the OrderByMeasure class except that it will only
have one field – quantity (int).

(d) Write a program that will create an array of four Item objects. The objects should be
displayed giving an output something like the following

3. The following source code defines the class Top.

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) On line three of the class Top, the variable x is marked as protected. What effect does
this have?
(b) Give an example of method overriding from this code.
(c) Compare method overloading and method overriding. What are the similarities and
differences? (12.5 Marks)

4. (a) Give two circumstances in which the super keyword can be used in a class
definition. Give an example for each.
(a) Explain the circumstances in which an interface might be used.
(b) What is a final variable? What is the purpose for making a variable final? What is a
final method? What is the purpose for making a method final? (12.5 Marks)
5. A program calculates the side BC of a
triangle given the sides AB and AC, and the
included angle BAC. A GUI for the program
is shown below. The user enters the lengths
of the sides AB and AC, and the magnitude
of the angle BAC in the text boxes shown,
and then clicks on the Go button. The side
BC is then calculated and the result is
displayed in the last text box. An outline of the required class definition is shown below.
(12.5 Marks)

1. //CosineRule.java
2.
3. import java.awt.*;
4. import java.awt.event.*;
5. import javax.swing.*;
6.
7. public class CosineRule extends JFrame{
8. private JLabel firstSideLabel;
9. private JLabel secondSideLabel;
10. private JLabel includedAngleLabel;
11. private JLabel otherSideLabel;
12.
13. private JTextField firstSide;
14. private JTextField secondSide;
15. private JTextField includedAngle;
16. private JTextField otherSide;
17.
18. private JButton go;
19.
20. //constructor
21. // ...
22. // ...
23. // ...
24. //end of constructor
25.
26. //inner class event handler
27. // ...
28. // ...
29. // ...
30. //end of inner class event handler
31. }//end of class CosineRule
(a) You are to complete the class definition by writing the code for the constructor, and the
code for the event handler.
Note.
(i) The window has a size of 300 × 150 pixels and cannot be resized.
(ii) Using the standard notation for describing a triangle, the cosine rule says:
a2  b2  c2  2bc Cos A
(b) Create the program CosineRuleTest.java that will run this program

6. You are given the following code for the interface Measurable (12.5 Marks)

1. // Measurable.java
2.
3. public interface Measurable{
4. public double getArea();
5. public double getPerimeter();
6. }// end of interface Measurable

(a) Design two classes that implement this interface. The first, Circle, has just one field,
radius (double). The second, Rectangle, has two fields, length (double) and
height (double). Each should have suitable getters, and a constructor that sets the
values of its fields. Each should also have a suitable toString() method.

(b) Design a program that creates an array of four objects, two of type Circle and two of type
Rectangle. The program should display the area and perimeter of the objects. It should also
find and display the total area of the circles.

Here is some sample output.

Rectangle Length: 5.70 Height: 2.30 Area: 13.11 Perimeter 16.00


Circle Radius: 10.30 Area: 333.29 Perimeter 64.72
Rectangle Length: 10.90 Height: 5.40 Area: 58.86 Perimeter 32.60
Circle Radius: 5.30 Area: 88.25 Perimeter 33.30

The total area of the circles is 421.54

You might also like