IBDP Computer Science B3.2 Fundamentals of OOP for multiple classes HL Paper 1 - New Syllabus
Question
An art shop sells the work of several artists. Each artist has many artworks for sale. A system is designed to manage artworks, sales and customers.
The following shows part of the code for the two classes, Artist and Artwork:
public class Artist {
private String artistName; // name of the artist
private String artistLocation; // current location of the artist
private int noOfArtworks; // number of works of art the artist has
// stored in the array theArtworks
private Artwork[] theArtworks; // details of the works of art produced
// by the artist
public Artist(String artistName, String artistLocation){
this.artistName = artistName;
this.artistLocation = artistLocation;
noOfArtworks = 0;
theArtworks = new Artwork[100]; // an artist has a maximum of 100
// artworks
}
public String getName() {
return artistName;
}
public String getLocation() {
return artistLocation;
}
public Artwork getArtwork(int x) {
return theArtworks[x];
}
public void addArtwork(Artwork x) {
theArtworks[noOfArtworks] = x;
noOfArtworks = noOfArtworks + 1;
}
public double commissionToPay() {
// code missing
}
public Artwork[] sortArt() {
// code missing
}
} // end of class ArtistThe Artwork class is:
public class Artwork {
private String artworkTitle; // name of the artwork
private int artworkPrice; // price of artwork
private boolean isSold; // if the artwork has been sold or not
public Artwork(String artworkTitle, int artworkPrice) {
this.artworkTitle = artworkTitle;
this.artworkPrice = artworkPrice;
isSold = false; // default value is not sold
}
public String getArtworkTitle() {
return artworkTitle;
}
public void isSold() {
isSold = true;
}
} // end of class ArtworkArtist class and Artwork class. [1]Artist and Artwork classes, outline the use of the modifier private. [2]Artist class. [3]Most-appropriate topic code
▶️ Answer/Explanation
(a)
For the correct answer:
The relationship between the Artist and Artwork classes is aggregation. An Artist has multiple Artwork objects, represented by the theArtworks array.
(b)
For the correct answer:
- Instantiation is the process of creating a new object from a class, normally by calling the constructor of the class.
- During instantiation, memory is allocated for the object and its attributes are initialized using the values supplied to the constructor.
For example:
Artist A = new Artist("Thomas Lucas", "Ireland");Here, Artist is the class and A refers to an instantiated object of that class.
(c)
For the correct answer:
Artist A = new Artist("Thomas Lucas", "Ireland");Explanation: A is the reference variable, new Artist() creates the new object, and the constructor receives "Thomas Lucas" and "Ireland" as its arguments.
(d)
For the correct answer:
- The
privatemodifier means that attributes such asartistName,artistLocation,theArtworks,artworkTitle,artworkPrice, andisSoldcannot be accessed directly from outside their respective classes. - Access to these attributes must be controlled through appropriate methods, such as
getName(),getLocation(),getArtwork(), andgetArtworkTitle().
For example, artworkTitle in the Artwork class is private and can be accessed using the appropriate accessor method rather than directly accessing the attribute.
(e)
For the correct answer, two advantages should be outlined:
Easier and faster debugging: smaller individual modules contain fewer possible sources of errors, making it easier to identify and correct mistakes.
Faster project completion: different programmers or teams can work on different modules simultaneously, allowing development tasks to be carried out in parallel.
Other valid advantages include:
- Collaborative development: different teams can work on separate modules according to their areas of expertise.
- Code reusability: existing modules can be reused in other parts of the program or other programs.
- Improved organization: smaller, manageable modules provide better logical organization of the code.
- Reduced coupling: changes to one module are less likely to affect other modules, making maintenance easier.
(f)
For the correct answer:
| Artist |
| − artistName : String − artistLocation : String − noOfArtworks : int − theArtworks : Artwork[] |
| + Artist(artistName : String, artistLocation : String) + getName() : String + getLocation() : String + getArtwork(x : int) : Artwork + addArtwork(x : Artwork) : void + commissionToPay() : double + sortArt() : Artwork[] |
The − symbol represents private members and the + symbol represents public members. The UML diagram contains the class name, attributes with their data types, and methods with their return types.
