IBDP Computer Science B3.1 Fundamentals of OOP for a single class HL Paper 1 - New Syllabus

Question 

A company sells healthcare products. Each product type is associated with a particular brand and price.

A system is designed to manage product sales, customers, and suppliers.

The Product class keeps details of a product. The following shows part of the code for this class:

public class Product {

    private String prodCode;        // eg X123
    private String prodType;        // eg Sunscreen
    private String prodDescription; // about the product
    private Brand prodBrand;        // an object of type Brand
    private int prodSale;           // number of units

    // Constructor
    // code missing for constructor method

    public int getProdSale() {
        return prodSale;
    }

    public Brand getProdBrand() {
        return prodBrand;
    }

    // all accessor and mutator methods are present but not shown

} // end of Product class

The Brand class keeps details of a particular brand. The following shows part of the code for this class:

public class Brand {

    private String brandName;  // eg Safesun
    private float brandPrice;  // price of the product of this brand

    public Brand(String brandName, float brandPrice) {
        this.brandName = brandName;
        this.brandPrice = brandPrice;
    }

    public float getBrandPrice() {
        return brandPrice;
    }

    // all accessor and mutator methods are present but not shown

} // end of Brand class
(a)
(i) Define the term private. [1]
(ii) Define the term accessor method. [1]
(iii) Construct an accessor method in the Product class that returns the description for a product. [2]
(b)
(i) Construct a UML diagram for the given Product class. [3]
(ii) Construct the code for the constructor method for the Product class that initializes all attributes for a new product. [3]
(c)
(i) Construct the code to create an instance of the Brand class that has the brand name Safesun and a brand price of 2.17. [3]
(ii) Describe the difference between a class and an instance of a class. [2]
(d) Identify two features of modern programming languages. [2]

Most-appropriate topic code

B3.1: Fundamentals of OOP for a single class — parts (a), (b) and (c)
B2.1: Programming fundamentals — part (d)
▶️ Answer/Explanation

(a) (i)
For the correct answer:

private is an access modifier or access specifier used to make an attribute or method accessible only from within the class. A private variable or method cannot normally be accessed directly from outside the class.

(a) (ii)
For the correct answer:

An accessor method is a method that returns the value of a private or protected variable belonging to an object, allowing the value to be accessed from outside the class.

(a) (iii)
For the correct answer:

public String getProdDescription()
{
    return prodDescription;
}

The method header must specify an appropriate return type and method name, and the method must return the value of prodDescription.

(b) (i)
For the correct answer:

The UML diagram should contain the class name, all five attributes with their data types, and the accessor methods shown in the supplied Product class. The minus sign represents private and the plus sign represents public.

Product
− prodCode: String
− prodType: String
− prodDescription: String
− prodBrand: Brand
− prodSale: int
+ getProdSale(): int
+ getProdBrand(): Brand

(b) (ii)
For the correct answer:

public Product(String prodCode, String prodType,
               String prodDescription, Brand prodBrand,
               int prodSale)
{
    this.prodCode = prodCode;
    this.prodType = prodType;
    this.prodDescription = prodDescription;
    this.prodBrand = prodBrand;
    this.prodSale = prodSale;
}

An equivalent constructor using different parameter names is also valid:

public Product(String pCode, String pType, String pDesc,
               Brand pBrand, int pSale)
{
    prodCode = pCode;
    prodType = pType;
    prodDescription = pDesc;
    prodBrand = pBrand;
    prodSale = pSale;
}

The constructor must have the correct name and parameters and initialize all five attributes. The default constructor is not sufficient because the question requires a new object to be initialized with values.

(c) (i)
For the correct answer:

Brand b = new Brand("Safesun", 2.17f);

Explanation: b is the reference variable, new Brand() creates the object, and "Safesun" and 2.17f are passed to the constructor as the required arguments.

(c) (ii)
For the correct answer:

  • A class is a blueprint or template that defines the attributes and behaviours of objects.
  • An instance is an actual object created from the class and contains specific values for its attributes.

For example, Brand is a class, while b created using new Brand("Safesun", 2.17f) is an instance of that class.

(d)
For the correct answer, any two valid features of modern programming languages may be identified:

  • Libraries or frameworks containing pre-written code
  • Type checking
  • Data abstraction
  • Exception handling
  • Separate compilation
  • Concurrency mechanisms
  • Unicode support for multilingual text and special characters

Explanation: These features make modern programming languages more powerful, reliable, reusable, and suitable for developing complex software systems.

Scroll to Top