I'm new to programming, this is one of our first object oriented programs that we are doing in class. I feel like I have used "this." more than I need to, but my program works properly and I get the correct output. In my getters, can I return the variable without using this? I guess my question is does this.variablename refer to the parameter variable or the data field declared at the top of my class?
import java.util.Date;
public class Account{
private int id = 0;
private double balance = 0;
private static double annualInterestRate = 0.00;
private Date dateCreated;
public Account(){}
public Account(int id, double balance){
this.id = id;
this.balance = balance;
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public double getBalance(){
return this.balance;
}
public void setBalance(double balance){
this.balance = balance;
}
public double getAnnualInterestRate(){
return this.annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public Date getDateCreated(){
return this.dateCreated = new Date();
}
public double getMonthlyInterestRate(){
return (this.annualInterestRate / 12);
}
public double getMonthlyInterest(){
return ((this.annualInterestRate / 100) / 12) * this.balance;
}
public void withdraw(double amount){
this.balance -= amount;
}
public void deposit(double amount){
this.balance += amount;
}
}
Here is my main method and test class:
public class AccountTest{
public static void main(String[] args){
Account myObject = new Account(112233, 20000.00);
myObject.setAnnualInterestRate(4.5);
myObject.withdraw(2500.00);
myObject.deposit(3000.00);
System.out.printf("The account balance is $%,.2f.", myObject.getBalance());
System.out.printf("\nThe monthly interest is $%,.2f.", myObject.getMonthlyInterest());
System.out.print("\nThe account was created at " + myObject.getDateCreated());
}
}