Question

import java.util.Date;
public class Exercise08_07 {
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 
{
//define variables 
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

//a no-arg constructor that creates a default account.

public Account()

{
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
 //constructor creates an account with the specified id and initial balance
  public Account(int id, double balance){
  this.id = id;
  this.balance = balance;

}
  public Account (int newId, double newBalance, double newAnnualInterestRate)
  { 
      id = newId;
      balance = newBalance;
      annualInterestRate = newAnnualInterestRate;

  }
  //accessor and mutator methods for id, balance, and annualInterestRate
  public int getId(){
       return id;
  }
  public double getBalance(){
  return balance;
  }
  public double getAnnualInterestRate(){
      return annualInterestRate;

  }
  public void setId(int id){
      this.id = id;
  }
  public void setBalance(double balance){
      this.balance=balance;
  }
  public void setAnnualInterestRate(double annualInteresteRate){
      this.annualInterestRate = annualInterestRate;
  }
      //accessor method for dateCreated
  public void setDateCreated(Date newDateCreated){
      dateCreated = newDateCreated;
  }
     //method named getMonthlyInterestRate()that returns the monthly interest rate.
  double getMonthlyInterest(){
      return annualInterestRate/12;      
  }
  Date getDateCreated(){
      return dateCreated;
  }
  //method named withdraw that withdraws a specified amount from the account.
  double withdraw (double amount){
      return balance -= amount;
  }
  //method named deposit that deposits a specified amount to the account
  double deposit (double amount){
      return balance += amount;
  }
  }

this is my first time posting question here, not sure how to post code here, the netbean didn't show any error, but the output is incorrect, I am not sure where the code went wrong, I am new to Java please help me with this problem, thank you! the output:

balance is 20500.0

monthly interest is 0.0

this account was created at null

I am not sure why the output is incorrect, can anyone help please?

No correct solution

OTHER TIPS

There's a spelling mistake (an extra 'e' on the input parameter annualInterest*e*Rate):

public void setAnnualInterestRate(double annualInteresteRate){
      this.annualInterestRate = annualInterestRate;
}

Therefore you are setting the this.annualInterestRate to itself, which was set to be 0.0 in the constructor. Should be:

public void setAnnualInterestRate(double annualInterestRate){
          this.annualInterestRate = annualInterestRate;
}

You are also never assigning a creation date to dateCreated, hence the null. I suggest setting it inside the constructor (as this will be called any time you create a new Account object).

annualInterestRate = 0.0;

And then you calc monthtly interest dividing annual by 12.

0/anything except 0 is 0.

That's why

Your constructors aren't set up properly.

public Account()
public Account(int id, double balance)
public Account (int newId, double newBalance, double newAnnualInterestRate)

All of them are lacking a Date initialization.

this.dateCreated = new Date(); // Add this to each constructor

You're also lacking a method for calculating the interest generated each month, need to write that as well. getMonthlyInterest() is different from getMonthlyInterestRate(), which is what you're currently doing.

There should be an earlier assignment in your textbook that shows you how to calculate the monthly interest (again not the RATE, the amount) given the annual interest rate.

Output of given program is correct. Please see below the explanation below for Each output.

monthly interest is 0.0

The following setter method is written incorectly where it is assigning class instance variable to itself where it should be assign the value passed in setter as parameter. So need to change that value assignment.

The variable name in setter parameter is = annualInteresteRate, while the variable name after = is annualInterestRate.

public void setAnnualInterestRate(double annualInteresteRate){
      this.annualInterestRate = annualInterestRate;
  }

this account was created at null

Here it is trying to access the data value from Account class as date never set to Account class it is returned as null. Either you need to assign created date or change declaration from

      private Date dateCreated;

to

private Date dateCreated = new Date();

It will initialize date at during object creation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top