-1

有人可以看看我的代码并建议我在工作代码的路径上徘徊的地方。

作业内容如下: 编写一个名为 UseLoan 的应用程序,该应用程序使用一个名为 PersonalLoan 的抽象类和子类来显示两种不同类型的贷款——房屋和汽车——以及每种贷款的每月费用。每个子类都包含一个构造函数,该构造函数在提示用户输入至少一个用于成本确定决策的数据输入项后,根据贷款类型设置每月成本。(例如,对于汽车贷款,您可能会询问汽车的年龄,或者它是否是一辆跑车。)在 PersonalLoan 类中包含一个抽象的 toString() 方法,该方法构造一个包含所有相关数据的字符串。提示用户选择保险类型,然后创建并显示相应的对象。将文件保存为 PersonalLoan.java、CarLoan.java、homeLoan.java 和 UseLoan.java。

当我编译代码时出现以下错误

线程“main”java.lang.Error 中的异常:未解决的编译问题:无法实例化类型 CarLoan 无法实例化类型 CarLoan 无法实例化类型 CarLoan 无法实例化类型 CarLoan 无法实例化类型 HomeLoan

at UseLoan.main(UseLoan.java:7)

到目前为止,我的代码如下:

public abstract class PersonalLoan {

String title = new String();
double sportsPrice, suvPrice, hybridPrice, pickupPrice, price30, price15, price5;

public PersonalLoan(String t) {
    title = t;
}

public String getTitle() {
    return title;
}

public double getSportsPrice() {
    return sportsPrice;
}

public abstract void setSportsPrice();

public double getSUVPrice() {
    return suvPrice;
}

public abstract void setSUVPrice();

public double getHybridPrice() {
    return hybridPrice;
}

public abstract void setHybridPrice();

public double getPickupPrice() {
    return pickupPrice;
}

public abstract void setPickupPrice();

public double getPrice30() {
    return price30;
}

public abstract void setPrice30();

public double getPrice15() {
    return price15;
}

public abstract void setPrice15();

public double getPrice5() {
    return price5;
}

public abstract void setPrice5();
}

这是第二个子类 PersonalLoan:

public class CarLoan extends PersonalLoan {

public CarLoan(String title) {
    super(title);
    setSportsPrice();
    setSUVPrice();
    setHybridPrice();
    setPickupPrice();
}
 public void setSportsPrice() {
    super.sportsPrice = 1.99;
 }

public void setSUVPrice() {
    super.suvPrice = 2.99;
}

public void setHybridPrice() {
    super.hybridPrice = 3.99;
}

public void setPickupPrice() {
    super.pickupPrice = 4.99;
}
}

这是 HomeLoan 类:

public class HomeLoan extends PersonalLoan {

public HomeLoan(String title) {
    super(title);
    setPrice30();
    setPrice15();
    setPrice5();
}

public void setPrice30() {
    super.price30 = 1.99;
}

public void setPrice15() {
    super.price15 = 2.99;
}

public void setPrice5() {
    super.price5 = 3.99;
}
}

这是主类 UseLoan:

public class UseLoan {

 public static void main(String[] args) {
    PersonalLoan aPersonalLoan[] = new PersonalLoan[5];
    int x;
    aPersonalLoan[0] = new CarLoan("Sports Car");
    aPersonalLoan[1] = new CarLoan("SUV");
    aPersonalLoan[2] = new CarLoan("Hybrid");
    aPersonalLoan[3] = new CarLoan("Pickup Truck");
    aPersonalLoan[5] = new HomeLoan("30 Year Mortgage");

    for (x = 0; x < aPersonalLoan.length; ++x) {
        System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getSportsPrice());
        //System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getSUVPrice());
        //System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getHybridPrice());
        //System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[x].getPickupPrice());
        //System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[X].getPrice30());
        //System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() + " costs $" + aPersonalLoan[X].getPrice15());
        //System.out.println("Book #" + (x + 1) + ": " + aPersonalLoan[x].getTitle() +          " costs $" + aPersonalLoan[X].getPrice5());
    }
}
}
4

2 回答 2

1
Uncompilable source code - CarLoan is not abstract and does not override abstract method setPrice5() in PersonalLoan

编译器错误信息很清楚。setPrice5()你还没有在PersonalLoan课堂上提供实现。

与类中setPrice5()的方法一样HomeLoan,您也需要将 setPrice5() 方法添加到PersonalLoan类中。

于 2013-08-13T18:42:43.530 回答
0

Basic problem from stack trace is CarLoan does not implement setPrice5() in PersonalLoan class.

But by design your classes is not right

PersonalLoan is loan type independent and hence should not have loan specific info like setSportsPrice, etc

PersonalLoan should probably have methods such as 1) setLoanAmount() - abstract, 2) setLoanAmount() 3) setEMI() - abstract 4) getEMI() which are set in the contructor of your subclasses based on input parameters as mentioned in your assignment statement

public HomeLoan(double loanAmount, int loanTenure) {
    super(title);
    setLoanAmount(loanAmount)
    int homeLoanRate = 10;
    double emi = (loanAmount*loanTenure*homeLoanRate)/100; // pls change formula. This is a just a guideline
    setEMI(emi);

}

Similarly CarLoan should take parameters such as cost of purchase of current owner, age of car, tenure and calculate based on depreciation the current price and hence the emi

Hope this helps

于 2013-08-13T19:04:50.823 回答