• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 11 pages
Exam (elaborations)

CS1410 Java Program Development

Document preview thumbnail
Preview 2 out of 11 pages

CS1410 Java Program Development 1. (Compulsory Question) Read the following partial problem specification. The task is to develop a simulation of an air traffic control system. There are several different types of aircraft, so an abstract base class has been created. Each aircraft has an integer code, initialised in the constructor. It is also possible to find out the remaining flying time (or CAPACITY), measured in minutes, before the aircraft must land. The following block of code contains a skeleton implementation of the Aircraft class. Beginning of code 1 public abstract class Aircraft { 2 protected int code; 3 4 public Aircraft(int code) { 5 = code; 6 } 7 8 public abstract int getFlyingCapacity(); 9 } End of code a) Aircraft is an ‘abstract class’. What are the implications of this? (4 marks) Model Answer: • It is impossible to instantiate Aircraft objects. • Any subclass of Aircraft can only be concrete if all abstract methods are defined. (2 marks) for each point. b) Define the difference between public and protected access. Explain why it is appropriate to make the code field protected. (6 marks) Model Answer: public members can be accessed by any other class (2 marks) ; protected members can only be accessed by the same class, its subclasses (and other classes in the same package) (2 marks) . It is appropriate to make code protected since subclasses may need direct access to it, and it shouldn’t be public as that would break encapsulation (2 marks) . c) Plane is a concrete subclass of Aircraft. In the constructor for Plane, as well as the code, a field for the flying capacity (in minutes) corresponding to the initial fuel load should be initialised. This field value should be returned by the getFlyingCapacity method. Write down a definition of this class. (8 marks) Model Answer: 1 OF 11 Downloaded by Austine Wanjala () lOMoARcPSD| ANSWERS CS1410 Java Program Development Beginning of code 1 public class Plane extends Aircraft { 2 private int flyingCapacity; 3 4 public Plane(int code, int flyingCapacity) { 5 super(code); 6 gCapacity = flyingCapacity; 7 } 8 9 public int getFlyingCapacity() { 10 return flyingCapacity; 11 } 12 } End of code (2 marks) for the class declaration, (1 mark) for the field declaration, (1 mark) for the constructor declaration, (2 marks) for the call to super, (1 mark) for initialising the field, and (1 mark) for the body of the method getFlyingCapacity. d) Consider the following code fragment (arguments omitted on line 1, but you may as￾sume that they are correct). Beginning of code 1 Plane p = new Plane(....); 2 Aircraft a = p; 3 Sln(FlyingCapacity()); End of code i) Define the ‘static type’ of a variable. In the above code fragment, what are the static types of p and a? (5 marks) Model Answer: The static type of a variable is its declared type and is determined at compile time (3 marks) . p has static type Plane and a has static type Aircraft; (1 mark) each. ii) Define the ‘dynamic type’ of a variable. In the above code fragment, what are the dynamic types of p and a? (5 marks) Model Answer: The dynamic type of a variable is the type of the object to which it refers; this can change during the execution of a programme (3 marks) . p has dynamic type Plane and a has dynamic type Plane; (1 mark) each. iii) Define the term ‘method overriding’. Explain, with the aid of a diagram, the se￾quence of events in the JVM when the method call FlyingCapacity() occurs on line 3. (7 marks) Model Answer: 2 OF 11 Downloaded by Austine Wanjala () lOMoARcPSD| ANSWERS CS1410 Java Program Development Method overriding occurs when a subclass and superclass both declare a method with the same signature. When this method is called on a variable whose static type is that of the superclass and dynamic type that of the subclass, then the method defined in the subclass is executed (4 marks) . a:Aircraft Plane Aircraft +getFlyingCapacity : int +getFlyingCapacity : int The diagram shows how this process works in this example: when FlyingCapacity is called, the inheritance tree is searched from the class with the correct dynamic type (Plane in this case) upwards (3 marks) . e) Suppose that it were decided to model gliders as a subclass of Aircraft. Gliders don’t use fuel, but can only fly during daylight. For this reason, the getFlyingCapacity method for Glider takes a parameter to represent the number of minutes until darkness: Beginning of code 1 public int getFlyingCapacity(int minsTillDark) End of code Briefly write down the changes that would be needed to Aircraft and Plane and explain why they are necessary. (You may assume in the rest of the exam paper that gliders are NOT being modelled, so that the definition of Aircraft is as printed above.) (5 marks) Model Answer: Both Aircraft and Plane must change the declaration of getFlyingCapacity to match that of Glider (3 marks) . This is required so that method overriding still works (2 marks) . 3 OF 11 Downloaded by Austine Wanjala ()

Content preview

lOMoARcPSD|47166518




AN
1. (Compulsory Question)
CS1410 Java Program Development




Read the following partial problem specification.

The task is to develop a simulation of an air traffic control system. There are several
different types of aircraft, so an abstract base class has been created. Each aircraft
has an integer code, initialised in the constructor. It is also possible to find out the
remaining flying time (or CAPACITY), measured in minutes, before the aircraft
must land.
The following block of code contains a skeleton implementation of the Aircraft
class.
Beginning of code
SW
1 public abstract class Aircraft {
2 protected int code;
3

4 public Aircraft(int code) {
5 this.code = code;
6 }
7

8 public abstract int getFlyingCapacity();
9 }
End of code


a) Aircraft is an ‘abstract class’. What are the implications of this?
(4 marks)
Model Answer:

• It is impossible to instantiate Aircraft objects.
• Any subclass of Aircraft can only be concrete if all abstract methods are defined.

(2 marks) for each point.
ER
b) Define the difference between public and protected access. Explain why it is
appropriate to make the code field protected.
(6 marks)
Model Answer:

public members can be accessed by any other class (2 marks) ; protected members can only
be accessed by the same class, its subclasses (and other classes in the same package) (2 marks) . It is
appropriate to make code protected since subclasses may need direct access to it, and it shouldn’t be
public as that would break encapsulation (2 marks) .

c) Plane is a concrete subclass of Aircraft. In the constructor for Plane, as well as
the code, a field for the flying capacity (in minutes) corresponding to the initial fuel load
should be initialised. This field value should be returned by the getFlyingCapacity
S
method. Write down a definition of this class.
(8 marks)
Model Answer:

1 OF 11

Downloaded by Austine Wanjala ()

, lOMoARcPSD|47166518




AN 1
CS1410 Java Program Development



Beginning of code
public class Plane extends Aircraft {
2 private int flyingCapacity;
3

4 public Plane(int code, int flyingCapacity) {
5 super(code);
6 this.flyingCapacity = flyingCapacity;
7 }
8

9 public int getFlyingCapacity() {
10 return flyingCapacity;
11 }
12 }
End of code
SW
(2 marks) for the class declaration, (1 mark) for the field declaration, (1 mark) for the
constructor declaration, (2 marks) for the call to super, (1 mark) for initialising the field, and
(1 mark) for the body of the method getFlyingCapacity.

d) Consider the following code fragment (arguments omitted on line 1, but you may as-
sume that they are correct).
Beginning of code
1 Plane p = new Plane(....);
2 Aircraft a = p;
3 System.out.println(a.getFlyingCapacity());
End of code

i) Define the ‘static type’ of a variable. In the above code fragment, what are the static
types of p and a?
(5 marks)
Model Answer:
ER
The static type of a variable is its declared type and is determined at compile time (3 marks) . p has
static type Plane and a has static type Aircraft; (1 mark) each.

ii) Define the ‘dynamic type’ of a variable. In the above code fragment, what are the
dynamic types of p and a?
(5 marks)
Model Answer:

The dynamic type of a variable is the type of the object to which it refers; this can change during the
execution of a programme (3 marks) . p has dynamic type Plane and a has dynamic type Plane;
(1 mark) each.

iii) Define the term ‘method overriding’. Explain, with the aid of a diagram, the se-
quence of events in the JVM when the method call a.getFlyingCapacity()
occurs on line 3.
(7 marks)
S
Model Answer:


2 OF 11

Downloaded by Austine Wanjala ()

Document information

Uploaded on
October 8, 2024
Number of pages
11
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$19.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Sold
20
Followers
8
Items
1904
Last sold
6 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions