运行简单程序时编译错误

时间:2014-10-13 13:10:40

标签: java

我有一个编程任务,我被要求用各种方法等编写一个名为Account的类和一个测试程序。我已经完成了所有事情,但事情有些不对,但我无法确定它是什么。 Annyone可以帮助我吗?该计划不会编译。

import java.util.Date;
public class Account {

    private int id = 0; 
    private double balance = 0;
    private double annualInterestRate = 0;
    private static Date dateCreated = new Date();

    Account(){
        id = 0;
        balance = 0;
        annualInterestRate = 0;
        dateCreated = new java.util.Date();
    }
        Account(int newId, double newBalance, double newAnnualInterestRate){
            id = newId;
            balance = newBalance;
            annualInterestRate = newAnnualInterestRate;

        }

         public int getId(int id){
            return id;
        }

        public int setId(int newId){
            return newId;

        }

        public double getBalance(double balance){
            return balance;
        }

        public double setBalance(double newBalance){
            return newBalance;

        }

        public double getAnnualInterestRate(double newAnnualInterestRate){
        return newAnnualInterestRate;

        }

        public double setAnnualInterestRate(double newAnnualInterestRate){
            return newAnnualInterestRate;

        }

        public Date getDateCreated(Date getDateCreated){
            return dateCreated;
        }

        public double getMonthlyInterestRate(){
            return balance * annualInterestRate / 12;
        }

        public double getMonthlyInterest(double getMonthlyInterestRate){
            return balance * getMonthlyInterestRate;

        }

        public double withdraw(double amount){
            return balance - (balance - amount);
        }

        public double deposit(double amount){
            return (balance + amount) - balance;
        }

    }





public class testAccount {

    public static void main(String[] args) {

        Account account = new Account(1122, 20000, 4.5);
            account.setId(1122);
            account.setBalance(20000);
            account.setAnnualInterestRate(4.5);
            account.withdraw(2500);
            account.deposit(3000);
            account.dateCreated();

            System.out.println("Balance is: " + account.balance);

        }


    }

3 个答案:

答案 0 :(得分:2)

只是瞥一眼你的代码,有一件事是错误的:

account.dateCreated();

你没有一个名为的方法。你有一个名为getDateCreated(),这是你的意思吗?

另一个问题是这一行:

account.balance();

同样,你可能意味着account.getBalance()

这两种方法在代码中也都有必需的参数,但它们可能不应该(如JonK的评论中所述)。吸气者通常不会接受任何争论。

无论哪种方式,都要发布你得到的错误。

答案 1 :(得分:1)

我可以在这里看到三个问题:

由于余额在帐户类中具有私人访问权限,因此您无法使用account.balance。这意味着您只能在Account类中使用此变量。 您可以将余额变量公开,也可以使用getBalance()方法。

您声明的所有get方法(getIdgetBalance)将始终返回传递给它们的参数,而不是实际变量。这是因为参数与实例变量具有相同的名称,因此将其隐藏。 典型的吸气剂没有任何参数,所以你可以摆脱它们。如果确实需要在具有相同名称的参数的方法中引用实例变量,请使用this关键字(即this.balance引用实例变量,balance引用参数)。

dateCreated方法似乎也不存在。

答案 2 :(得分:1)

在你的print语句中,最后使用account.getBalance()。toString()而不是account.balance

还要删除公共类testAccount中的关键字“public”。

对于你的方法getDateCreated(Date getDateCreated),你可以删除参数,而只是使用getDateCreated()。

当您使用account.dateCreated()时,请改用account.getDateCreated()。

编译,它应该工作!