I am getting a bracketing error in Eclipse (lines 15 & 18) "public Account myCustomAccount ... balance = initial balance; }" when I try to open my second constructor in the following program. The program is for Dietel "Introduction to Programming" chapter 9 exercise 7.
I suspect that I am creating the constructor incorrectly. What advice do you offer? (Thank you kindly in advance!!)
import java.util.Date;
public class Account {
//declare required variables
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0; //assume all accounts have the same interest rate
private Date dateCreated = new Date(); //no-argument instance stores the present date
//define default & custom constructors
public Account mydefaultaccount = new Account(); //no-argument instance of Account
public Account myCustomAccount = new Account(int identNum, double initialBalance) {
id = identNum;
balance = initialBalance;
}
//define getters
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double annualInterestRate() {
return annualInterestRate;
}
public Date getDate() {
return dateCreated;
}
//define setters
public void setId(int idSetter) {
id = idSetter;
}
public void setBalance(double balanceSetter) {
balance = balanceSetter;
}
public void setAnnualInterestRate(double annualSetter) {
annualInterestRate = annualSetter;
}
//define required monthly interest rate getter
public double getMonthlyInterestRate() {
double moInt = annualInterestRate / 12;
return moInt;
}
//define modifiers
public double withdraw(int withdraw) {
balance = balance - withdraw;
}
public double deposit(int deposit) {
balance = balance + deposit;
}
}
That isn't how you define constructors. Constructors should follow the form:
public className(parameters) {}
Then, to instantiate the class, call this:
ClassName variable = new ClassName(Parameters);
In your case,
public Account() {
/* Body */
}
public Account(int identNum, double initialBalance) {
/* Body */
}
And to instantiate,
Account ac = new Account(Parameters);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.