Я работаю над программой для хранения банковских счетов как объектов. У учетных записей есть процентная ставка, баланс, идентификатор и дата создания данных. В том, что я сделал, баланс по умолчанию, id и интерес - это то, что я понимаю. По умолчанию процентная ставка не определена. Книга, которую я изучаю, показывает, что конструктор no-arg выполняется с помощью «Circle() {}». Я использовал «account() {}» в классе учетной записи. Когда я запускаю программу в jGRASP, я получаю сообщение об ошибке «invalid method declaration, return type required» для обоих моих конструкторов. Это признание того, что я намерен быть конструкторами как методы. Что мне нужно понять, поэтому я могу заставить мои конструкторы не распознаваться как методы?Почему мои конструкторы распознаются как методы?
При запуске первого конструктора я понимаю, что мы создаем объект Account с именем account со значениями по умолчанию. Когда мы запускаем второй конструктор, мы изменяем значение объекта счета на что-то с указанного
public class Bank{
public static void main(String[] args){
Account account = new Account(1122, 20000);
account.setAnnualInterestRate(4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + account.getMonthlyInterest());
System.out.println("This account was created at " + account.getDateCreated());
}
}
class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private String dateCreated;
account(){
}
account(int newID, double newBalance){
id = newID;
balance = newBalance;
}
//accessor for ID
public int getID(){
return id;
}
//acessor for balance
public double getBalance(){
return balance;
}
//accessor for interest rate
public double getAnnualInterest(){
return annualInterestRate;
}
//mutator for ID
public void setID(int IDset){
id = IDset;
}
//mutator for balance
public void setBalance(int BalanceSet){
balance = BalanceSet;
}
//mutator for annual interest
public void setAnnualInterestRate(double InterestSet){
annualInterestRate = InterestSet;
}
//accessor for date created
public String getDateCreated(){
return dateCreated;
}
//method that converts annual interest into monthly interest and returns the value
public double getMonthlyInterest(){
double x = annualInterestRate/12;
return x;
}
//method that witdraws from account
public double withdraw(double w){
balance -= w;
return balance;
}
//method that deposits into account
public double deposite(double d){
balance += d;
return balance;
}
}
Edit: конструкторы не могут иметь типы возвращаемых – JClassic