Answer:
//==============================
// Book class
//==============================
public class Book {
private String title;
private double cost;
public Book(String title) {
this(title, 0.00);
}
public Book(String title, double cost) {
this.title = title;
this.cost = cost;
}
public String getTitle() {
return this.title;
}
public double getCost() {
return this.cost;
}
}
//==============================
// Dictionary class
//==============================
public class Dictionary extends Book {
private double numWords;
// Create Contructor
public Dictionary(String title, double cost, double numWords){
super(title, cost);
this.numWords = numWords;
}
}
Step-by-step explanation:
The actual response to this question is shown in bold face in the above.
Line 1:
public Dictionary(String title, double cost, double numWords){
Here the header for the constructor for the Dictionary class is written which takes in three parameters - title, cost and numWords. These parameters will be used to set the values of the instance variables of the Dictionary class.
Remember that the Dictionary class extends from the Book class. Therefore, in addition to its numWords attribute, the Dictionary class also has the title and cost attributes.
Line 2:
super(title, cost)
Here, since the Dictionary class inherits from the Book class, it can also call the constructor of Book class to initialize two of its three variables which are title and cost. Hence the need for the super() method keyword. In other words, the super() in the Dictionary class is the constructor from the parent class and it is used here to set the dictionary's title and cost attributes.
Line 3:
this.numWords = numWords;
Here, the only attribute left to be set is the numWords. Therefore, the 3rd line sets this attribute to the parameter passed into the constructor in line 1.
Line 4:
}
Here, the block containing the Dictionary class constructor is being closed.