1. Suppose you want to create a new InsuredCar class that is derived from the Ca
ID: 3682161 • Letter: 1
Question
1. Suppose you want to create a new InsuredCar class that is derived from the Car class. It adds one new booleanattribute, insured. Here is a constructor call for the class:
InsuredCar c = new InsuredCar("Ford", 15.0, 4.0, true);
Using super appropriately, write the constructor for the InsuredCar class. (Hint: model your solution after the constructor for the UsedCar class).
public class InsuredCar extends Car{
private boolean insured;
2. Suppose you create a new PricedApt class that is derived from the RentalApt class (so it's derived from a derived class). It adds one new double attribute, price, which tells what the monthly rental for the apartment is. Here is a constructor call for the class:
PricedApt p = new PricedApt("jill", 900, true, "jack", 1050.00);
The class is missing its constructor. In the box provided below, write this missing piece in its entirety.
public class PricedApt extends RentalApt {
private double price;
3. Suppose you create a new PricedApt class that is derived from the RentalApt class (so it's derived from a derived class). It adds one new double attribute, price, which tells what the monthly rental for the apartment is. Here is a constructor call for the class:
PricedApt p = new PricedApt("jill", 900, true, "jack", 1050.00);
Using super appropriately, write a toString method for the class which, in addition to telling about jill as owner and jack as tenant, adds to the toString output the phrase " price: " + price. (Note: a super call to toString inPricedApt will use the toString method from its base class, RentalApt.
public class PricedApt extends RentalApt {
private double price;
//constructor goes here
public String toString() {
Explanation / Answer
public class InsuredCar extends Car {
// Add one new field
private boolean insured;
public InsuredCar (String whatMake, double cap, double amt, boolean cover) {
super(whatMake,cap,amt);
insured = cover;
}
public boolean getInsured() {
return insured;
}
public void setInsured(boolean isInsured) {
insured = isInsured;
}
// Test your code :)
public static void main(String[] args) {
// Create new InsuredCar
InsuredCar c = new InsuredCar("Ford", 15.0, 4.0, true);
System.out.println(c.getMake());
System.out.println(
c.getCapacity());
System.out.println(c.getFuel());
System.out.println(
c.getInsured());
}
}
public class RentalApt extends Apartment{
private String tenant;
private boolean rented;
public RentalApt(String owner, int size, boolean rented, String who){
super(owner,size);
tenant = who;
this.rented = rented;
}
public class PricedApt extends RentalApt {
private double price;
public PricedApt(String owner, int size, boolean rented, String who, double priceTag) {
super(owner,size,rented,who);
price = priceTag;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.