Decorative banner

D.1 Objects as a programming concept

Question 1
SLPaper 2

A company provides car parking services in large cities and needs a computer system to keep track of the number of vehicles using its parking areas.

When a vehicle arrives, its registration plate is recorded on the system and it is allocated a number that identifies where it should park. When the vehicle leaves, that space is made available for other vehicles to use.

Vehicles are identified by their unique registration plate, which is an alphanumeric code of eight characters (e.g. X1234567). This is clearly displayed on the vehicle.

A programmer created the classes ParkingAreaandVehicleto model the above situation.

public class ParkingArea {  
private Vehicle vehicles$$;  
private String name;  

ParkingArea(String name, int capacity) {  
    this.name = name;  
    if (capacity > 300) capacity = 300;  
    this.vehicles = new Vehicle$capacity$;  
}  

String getName() {  
    return name;  
}  
  
public int getCapacity() {  
    return vehicles.length;  
}  

public int findVehicle(String reg) {  
    //find where the vehicle is located in the array and  
    //return the index not yet written  
}  
}

public class Vehicle {  
private String registration;  
private byte colour;  
private boolean broken;  

public final static byte BLACK=1;  
public final static byte WHITE=2;  
public final static byte BLUE=3;  
public final static byte RED=4;  
public final static byte GREEN=5;  
private final static double ADMIN\_FEE = 3;  

public Vehicle() {}  

public Vehicle(String registration) {  
    this.registration = registration;  
}  
public Vehicle(String registration, byte colour) {  
    this.registration = registration;  
    this.colour=colour;  
}  
public void setBroken(boolean broken) {  
    this.broken=broken;  
}  
public void setColour(byte colour) {  
    this.colour=colour;  
}  
public boolean getBroken() {  
    return broken;  
}  
public String getRegistration() {  
    return registration;  
}  
public double pay(int hours) {  
    // code to return admin fee - only if applicable  
}  
}
1.

Construct code to create an instance of the Vehicleclass that has a registration of X1234567.

[2]
2.

Outline why it is necessary to use the keyword thisin the setBrokenmethod of the Vehicleclass.

[2]
3.

Construct code that sets the colour of the object created in part (i) as black.

[2]
4.

Describe the relationship between the classes Vehicleand ParkingArea.

[3]
5.

Outline one effect of using the modifier staticwhen declaring a variable.

[2]
Question 2
SLPaper 2

When the number of customers in the supermarket is low, some check-out counters will close. When there are many customers waiting, a new check-out counter will open.

When a new check-out counter opens, some customers from the nearest line will choose to move their carts to this check-out counter.

For this simulation, the assumption is made that every second cart in the old line will move to the new line and the other carts will remain in the original line.

1.

Outline why the variable activein the UML of the class POSlinewas defined as a boolean data type.

[2]
2.

Construct the code for a method split(part of the class POSsystem), that takes an existing, non-empty POSlineas a parameter. It should copy every second cart from the original line into a new line. Afterwards it should delete every second cart from the original line.

An example call in POSsystemwould be: POSline number2 = split(number1), where number1is an existing POSline.

You may use any method declared or developed.

[8]
3.

As a result of the simulation, the company has decided to create a new POS system for their supermarkets.

There have been discussions about adapting existing open source code when developing modules of the new POS system.

Describe two ethical issues that may arise if modules of the new POS system are developed from open source code.

[4]
Question 3
SLPaper 2

An online education platform tracks students and their courses. The platform has a Student class and a Course class.

    private String courseName;
    private int courseCode;
    private Student[] enrolledStudents;

    public Course(String courseName, int courseCode, int capacity) {
        this.courseName = courseName;
        this.courseCode = courseCode;
        this.enrolledStudents = new Student[capacity];
    }

    public void enrollStudent(Student student) {
        // code to add a student to the enrolledStudents array
    }
}

public class Student {
    private String studentID;
    private String name;
    private boolean activeStatus;

    public Student(String studentID, String name) {
        this.studentID = studentID;
        this.name = name;
        this.activeStatus = true;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setActiveStatus(boolean status) {
        this.activeStatus = status;
    }
}
1.

Construct code to create an instance of the Student class with a student ID of "S12345" and name "John Doe".

[2]
2.

Explain the purpose of the setActiveStatus method in the Student class.

[2]
3.

Describe the relationship between the Course and Student classes.

[3]
4.

Outline one benefit of setting Student attributes as private.

[2]
Question 4
HLPaper 1
1.

Outline the need for higher level languages.

[2]
2.

Explain two benefits of using sub-procedures within a computer program.

[4]
3.

Identify three characteristics of a collection.

[3]
4.

Construct in pseudocode an algorithm, using the access methods of a collection, which will iterate through the collection NUMBERS and count how many elements stored in the collection are in the interval [-1,1]. The final answer should be output.

[6]
Question 5
SLPaper 2
1.

By making reference to any of the classes from 12(a), describe two benefits that a programming team would gain from the use of encapsulation.

[6]
2.

Construct a diagram showing the relationships between the Payment, FoodItem, DrinkItem, and Itemclasses. You do not need to include the names of attributes or methods.

[3]
3.

The company that owns this restaurant also owns hotels, shops, and a car hire business. The programs that manage the finances of these different businesses include classes similar to the ones shown in this paper.

Explain how inheritance could be used as a tool to both improve the clarity of the design and to reduce the amount of code that needs to be written.

[4]
Question 6
SLPaper 2

A developer is creating an application for a pet shelter. The application has an object representing each animal in the shelter.

1.

Outline the general nature of an object in the context of this application.

[2]
2.

Identify two attributes and two methods that might be associated with an animal object.

[4]
3.

Describe one benefit of representing each animal as an object in this application.

[2]
Question 7
SLPaper 2

In a university system, a Course object is related to multiple Student objects. Each student is enrolled in one or more courses.

1.

Describe the relationship between the Course and Student objects in this scenario.

[2]
2.

Construct a UML class diagram showing Course and Student with an appropriate relationship.

[3]
3.

Explain one benefit of defining clear relationships between objects in a program.

[2]
Question 8
SLPaper 2

A developer is creating a program to model a Hospital system with objects like Doctor, Patient, and Appointment.

1.

Outline one reason for using multiple related objects to model this hospital system.

[2]
2.

Describe two different relationships that might exist between these objects.

[4]
3.

Explain why it is beneficial to reduce dependencies between the Doctor and Appointment objects.

[3]
Question 9
SLPaper 2
1.

Consider the following code fragment

Train A = new Train(123);
Engine B = new Engine(7);
A.addEngine(B);
Wagon C = new Wagon(23);
A.addWagon(C);
Wagon D = new Wagon(66);
A.addWagon(D);
Wagon E = new Wagon(71);
A.addWagon(E);
A.addEngine(new Engine(9));

Draw the mEngines array after the code fragment has been executed.

[2]
2.

State the value of mEngineCount after the code fragment has been executed.

[1]
3.

Draw the mWagons array after both the code fragment above and the code fragment below have been executed.

Wagon F = A.removeWagon();
F = A.removeWagon();
A.addWagon(new Wagon(214));
[2]
4.

Construct the getWeight() method in the Wagon class that returns the total combined weight of the parcels currently in the wagon and the wagon itself.

[4]
5.

Construct the getWeight() method in the Train class that returns the total combined weight of all the parcels, engines and wagons in a train.

[4]
6.

Explain why having a getWeight() method in both the Train and Wagon classes does not cause a compiler error, even though the Train class does not inherit from the RollingStock class.

[2]
Question 10
SL & HLPaper 2

In an e-commerce application, a Product object has several attributes, such as name, price, and stock level.

1.

Explain why different data types are used to represent these attributes.

[3]
2.

Describe two data types that could be used for the attributes in this context.

[2]
3.

Explain the impact of using incorrect data types for attributes like price or stock level.

[2]
    Jojo smirking
    hands
    cookie

    This site uses cookie tracking technologies. Learn more in our Cookie Policy.