我正在学习Java初学者课程,我正在做我的第一个面向对象的作业。我确实了解OOP的基础知识,但我不具备将其付诸实践所需的知识。如果有人能指出任何新手的错误或给我一些建议,我将非常感激。
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
我相信这些错误是由跨类引用的方法造成的,但我不知道正确的方法。
对不起,你的一切都搞砸了。