我正在初学者Java课程,我正在进行我的第一个面向对象的任务。我确实理解OOP的基础知识,但我没有足够的知识将其付诸实践。如果有人能指出任何新手的错误或给我一些指示,我将非常感激。
这是作业:
创建类SavingsAccount。使用私有静态类变量来存储每个保护程序的annualInterestRate。该类的每个对象都包含一个私有实例变量savingsBalance,表示存储器当前具有的存储量。提供方法calculateMonthlyInterest通过将balanceInterestRate乘以12来计算月利息;这种兴趣应该加到savingsBalance。提供静态方法modifyInterestRate,将annualInterestRate设置为新值。编写一个驱动程序来测试类SavingsAccount。实例化两个不同的savingsAccount对象,saver1和saver2,余额分别为$ 2,000.00和$ 4,000.00。将annualInterestRate设置为3%,然后计算每月利息并打印每个储户的新余额。然后将annualInterestRate设置为5%并计算下个月的兴趣并打印每个储户的新余额。
这是我目前的代码:
class savings{
public static void main(String[] args){
double annualInterestRate = 0;
double monthlyInterest = 0;
SavingsAccount saver1 = new SavingsAccount(2000, .03);
SavingsAccount saver2 = new SavingsAccount(4000, .03);
calculateMonthlyInterest(saver1);
System.out.println("Monthly Interest at 3%: " + monthlyInterest);
calculateMonthlyInterest(saver2);
System.out.println("Monthly Interest at 3%: " + monthlyInterest);
modifyInterestRate(saver1);
modifyInterestRate(saver2);
calculateMonthlyInterest(saver1);
System.out.println("Monthly Interest at 5%: " + monthlyInterest);
calculateMonthlyInterest(saver2);
System.out.println("Monthly Interest at 5%: " + monthlyInterest);
}
}
class SavingsAccount{
static double annualInterestRate;
static private double savingsBalance;
public static double calculateMonthlyInterest(double annualInterestRate){
double monthlyInterest = 0;
monthlyInterest = savingsBalance * annualInterestRate / 12;
return monthlyInterest;
}
public static void modifyInterestRate(double annualInterestRate){
annualInterestRate = .05;
}
SavingsAccount(double savingsBalance, double annualInterestRate){
this.savingsBalance = savingsBalance;
this.annualInterestRate = annualInterestRate;
}
}
以下是编译错误:
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:7: error: cannot find symbol
calculateMonthlyInterest(saver1);
^
symbol: method calculateMonthlyInterest(SavingsAccount)
location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:9: error: cannot find symbol
calculateMonthlyInterest(saver2);
^
symbol: method calculateMonthlyInterest(SavingsAccount)
location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:11: error: cannot find symbol
modifyInterestRate(saver1);
^
symbol: method modifyInterestRate(SavingsAccount)
location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:12: error: cannot find symbol
modifyInterestRate(saver2);
^
symbol: method modifyInterestRate(SavingsAccount)
location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:13: error: cannot find symbol
calculateMonthlyInterest(saver1);
^
symbol: method calculateMonthlyInterest(SavingsAccount)
location: class savings
F:\Archive\Homework_Spring_2014\Java\Project 2\Savings\savings.java:15: error: cannot find symbol
calculateMonthlyInterest(saver2);
^
symbol: method calculateMonthlyInterest(SavingsAccount)
location: class savings
6 errors
Tool completed with exit code 1
我相信这些错误来自跨类引用的方法,但我不知道正确的方法。
答案 0 :(得分:0)
您似乎对如何在对象上运行方法存在一些误解。
例如,当你这样做时:
SavingsAccount saver1 = new SavingsAccount(2000, .03);
calculateMonthlyInterest(saver1);
您没有告诉saver1
执行calculateMonthlyInterest
,而是尝试在savings
类上调用方法,并将saver1
作为参数。
如果您尝试这样做:
SavingsAccount saver1 = new SavingsAccount(2000, .03);
saver1.calculateMonthlyInterest();
你仍然会收到错误,因为出于某种原因你已经使calculateMonthlyInterest
静态,并取annualInterestRate
,即使你已经在对象的构造函数中传递了利率。
你真正想要做的是这样的事情:
double annualInterestRate = 0;
double monthlyInterest = 0;
SavingsAccount saver1 = new SavingsAccount(2000, .03);
SavingsAccount saver2 = new SavingsAccount(4000, .03);
monthlyInterest = saver1.calculateMonthlyInterest();
System.out.println("Monthly Interest at 3%: " + monthlyInterest);
monthlyInterest = saver2.calculateMonthlyInterest();
System.out.println("Monthly Interest at 3%: " + monthlyInterest);
// etc
将方法签名更改为:
public double calculateMonthlyInterest(){
您的其他方法和对它们的调用存在同样的问题。
答案 1 :(得分:0)
抱歉,您的 一切 搞砸了。