Reputation: 75
So for this assignment I have to create a car class(parent) and a certifiedpreowned (child) and I need to have the parent class have a method to check if it is still under warranty. *checkWarrantyStatus(). that method calls the boolean isCoveredUnderWarranty() to veryify if the car still has warranty. My issue is in the certifiedpreowned class I have to call the isCoveredUnderWarranty() as well to see if it is covered under the extended warranty and then have it be called via the checkWarrantyStatus() in the car method. I hope this makes sense. So to sum it up I need to in the child class have it check the isCoveredUnderWarranty with extended warranty info. Then it has to move to the parent class so it can be called via checkWarrantyStatus. Here is my code, I have 1 error.
public class Car {
public int year;
public String make;
public String model;
public int currentMiles;
public int warrantyMiles;
public int warrantyYears;
int currentYear =java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
/** construct car object with specific parameters*/
public Car (int y, String m, String mod, int mi){
this.year = y;
this.make = m;
this.model = mod;
this.currentMiles = mi;
}
public int getWarrantyMiles() {
return warrantyMiles;
}
public void setWarrantyMiles(int warrantyMiles) {
this.warrantyMiles = warrantyMiles;
}
public int getWarrantyYears() {
return warrantyYears;
}
public void setWarrantyYears(int warrantyYears) {
this.warrantyYears = warrantyYears;
}
public boolean isCoveredUnderWarranty(){
if (currentMiles < warrantyMiles){
if (currentYear < (year+ warrantyYears))
return true;
}
return false;
}
public void checkWarrantyStatus(){
if (isCoveredUnderWarranty()){
System.out.println("Your car " + year+ " " + make+ " "+ model+ " With "+
currentMiles +" is still covered under warranty");
}
else
System.out.println("Your car " + year+ " " + make+ " "+ model+ " With "+
currentMiles +" is out of warranty");
}
}
public class CertifiedPreOwnCar extends Car{
public CertifiedPreOwnCar(int y, String m, String mod, int mi) {
super(mi, m, mod, y);
}
public int extendedWarrantyYears;
public int extendedWarrantyMiles;
public int getExtendedWarrantyYears() {
return extendedWarrantyYears;
}
public void setExtendedWarrantyYears(int extendedWarrantyYears) {
this.extendedWarrantyYears = extendedWarrantyYears;
}
public int getExtendedWarrantyMiles() {
return extendedWarrantyMiles;
}
public void setExtendedWarrantyMiles(int extendedWarrantyMiles) {
this.extendedWarrantyMiles = extendedWarrantyMiles;
}
public boolean isCoveredUnderWarranty() {
if (currentMiles < extendedWarrantyMiles){
if (currentYear < (year+ extendedWarrantyYears))
return true;
}
return false;
}
}
public class TestCar {
public static void main(String[] args) {
Car car1 = new Car(2014, "Honda", "Civic", 255);
car1.setWarrantyMiles(60000);
car1.setWarrantyYears(5);
car1.checkWarrantyStatus();
Car car2 = new Car(2000, "Ferrari", "F355", 8500);
car2.setWarrantyMiles(20000);
car2.setWarrantyYears(7);
car2.checkWarrantyStatus();
CertifiedPreOwnCar car3 = new CertifiedPreOwnCar(2000, "Honda", "Accord", 65000);
car3.setWarrantyYears(3);
car3.setWarrantyMiles(30000);
car3.setExtendedWarrantyMiles(100000);
car3.setExtendedWarrantyYears(7);
car3.checkWarrantyStatus();
}
}
Upvotes: 0
Views: 174
Reputation: 1204
In the base class Car, you set the data members as 'private'. Children cannot access private members of their super class. Consider using 'protected' or 'public' for your data members or provide getter (also protected or public) methods for those values in the Car class.
Consider the following example:
class Parent {
private int x;
protected int y;
public int z;
}
class Child extends Parent {
public void doit(){ System.out.println(x); //compiler error }
public void doit2(){ System.out.println(y); //allowed }
public void doit3(){ System.out.println(z); //allowed }
}
Edit:
For your other problem as stated in the comments on this post, the constructor of CertifiedPreownCar is currently
public CertifiedPreOwnCar(int y, String m, String mod, int mi) {
super(mi, m, mod, y);
}
and I believe it should be
public CertifiedPreOwnCar(int y, String m, String mod, int mi) {
super(y, m, mod, mi);
}
This is an easy mistake to make with your current variable names. While it is tempting to choose short variable names, it can introduce simple mistakes and can make your code harder to debug (especially for someone who didn't write it).
Consider using more expressive names such as 'year', 'make', 'model', and 'miles'
Upvotes: 3