Not sure I am using inheritance/polymorphism issue?
Posted
by
planker1010
on Stack Overflow
See other posts from Stack Overflow
or by planker1010
Published on 2014-05-30T03:10:07Z
Indexed on
2014/05/30
3:25 UTC
Read the original article
Hit count: 179
java
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();
}
}
© Stack Overflow or respective owner