+ 2

Im new in learning java, can anyone check my code and find the mistake?

It doesnt have errror, but the the output of 'Montly Interest' is zero '0'..it should be some number. i think it something wrong with the attribute and the method..but im not sure how to write it..

17th May 2020, 4:47 PM
Kuro Himitsu
Kuro Himitsu - avatar
6 Answers
+ 2
Hello Black Snow Please use the code playground and share the link here. What I see: System.out.println(a1.toString()); This is not necessary. System.out.println(a1) is enough. println() will automatically invoke toString() so you don't need to call this method explicity.
17th May 2020, 5:10 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Oh ok..thank you for the information
17th May 2020, 7:40 PM
Kuro Himitsu
Kuro Himitsu - avatar
+ 1
import java.util.Date; import java.text.*; class Account{ private Date dateCreated = new Date(); SimpleDateFormat ft = new SimpleDateFormat ("E dd.MM.yyyy 'at' hh:mm:ss a zzz"); private int id; private double balance; private double annualInterestRate; private double monthlyInterestRate; private double monthlyInterest; private double withdraw; private double deposite; public Account(){ id = 0; balance = 0; annualInterestRate = 0; monthlyInterestRate =0; monthlyInterest =0; } public void setId(int newId){ id = newId; } public void setBalance(double newBalance){ balance = newBalance; } public void setAnnualInterestRate(double newAnnualInterestRate){ annualInterestRate = newAnnualInterestRate; } public int getId(){ return id; } public double getBalance(){ balance = balance - withdraw + deposite; return balance; } public double getAnnualInterestRate(){ annualInterestRate = annualInterestRate / 100; return annualInterestRate; } public Date getDateCreated(){ return dateCreated; } public double getMontlyInterestRate(){ monthlyInterestRate = annualInterestRate / 12; return monthlyInterestRate; } public double getMontlyInterest(){ monthlyInterest = balance * monthlyInterestRate; return monthlyInterest; } public void withdraw(double newWithdraw){ withdraw = newWithdraw; } public void deposite(double newDeposite){ deposite = newDeposite; } public String toString(){ return("Balance = " + getBalance() + "\nMontly Interest = " + getMontlyInterest() + "\nDate = " + ft.format(getDateCreated())); } } class TestAccount { public static void main(String[] args) { Account a1 = new Account(); a1.setId(1122); a1.setBalance(20000); a1.setAnnualInterestRate(4.5); a1.withdraw(2500); a1.deposite(3000); System.out.println(a1.toString
17th May 2020, 4:48 PM
Kuro Himitsu
Kuro Himitsu - avatar
+ 1
Another thing: public Account(){ id = 0; balance = 0; annualInterestRate = 0; monthlyInterestRate =0; monthlyInterest =0; } If your constructor don't take arguments, you can remove it. All your variables are 0 by default. You don't need to initialize them. local variables (in a method or in a loop) get no default values. You need to initialize. But global variables like your attributes get default values. int - 0, double - 0.0, boolean - false ... Objects - null
17th May 2020, 5:14 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
//public double getMontlyInterestRate() { public double getMonthlyInterestRate() { //typo Month //monthlyInterestRate = annualInterestRate / 12; monthlyInterestRate = getAnnualInterestRate() / 12; return monthlyInterestRate; } public double getMontlyInterest() { //monthlyInterest = balance * monthlyInterestRate; monthlyInterest = balance * getMonthlyInterestRate(); return monthlyInterest; } but better is move all calculations to set methods
17th May 2020, 11:33 PM
zemiak
+ 1
Thank You so much, finally it has an output..
18th May 2020, 12:28 AM
Kuro Himitsu
Kuro Himitsu - avatar