Home Forums Programming Java program

Viewing 7 reply threads
  • Author
    Posts
    • #8219
      Anonymous
      Inactive

      Hi guys,

      I’m doing an account example for my java module and am stuck on a tiny bit of code. Basically when I set the annual interest rate to 4.5% in my getMonthlyInterestRate method it doesnt compute anything. Any help would be very much appreciated here is my code:

      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.getMonthlyInterestRate());
      System.out.println("This account was created at " + account.getDateCreated());

      }
      }

      public class Account
      {
      private int id ;
      private double balance ;
      private double annualInterestRate ;
      private Date dateCreated ;
      private double monthlyInterestRate ;

      public Account()
      {
      }

      public Account(int id, double balance)
      {
      this.id = id ;
      this.balance = balance ;
      }

      public int getID()
      {
      return id ;
      }

      public void setID(int id)
      {
      this.id = id ;
      }

      public double getBalance()
      {
      return balance ;
      }

      public void setBalance(double balance)
      {
      this.balance = balance ;
      }

      public double getAnnualInterestRate()
      {
      return annualInterestRate ;
      }

      public void setAnnualInterestRate(double annualInterestRate)
      {
      annualInterestRate = annualInterestRate / 100 ;
      }

      public Date getDateCreated()
      {
      return dateCreated = new Date() ;
      }

      public double getMonthlyInterestRate()
      {
      return monthlyInterestRate = getAnnualInterestRate() / 12 ;
      }

      public void withdraw(double amount)
      {
      balance = balance – amount ;
      System.out.println("Money Withdrawn") ;
      System.out.println("New Balance is " + balance) ;
      }

      public void deposit(double amountDeposited)
      {
      balance = balance + amountDeposited ;
      System.out.println("Money Deposited") ;
      System.out.println("New Balance is " + balance) ;
      }
      }

    • #47431
      Anonymous
      Inactive

      First of all this is not a programming support forum…… :wink:

      Second of all if you ever get a job in a bank let me know which one…..
      i.e setBalance in public (getBalance should total any account transactions ;-) )

      Setting a balance should never be public, it should be based on transactions, even when you add points to your "live"/psn account this is as a result of a redeem code tx or a credit card tx.

      Now to the code issue, missing this in front of annualInterestRate
      Using this refers to the class annualInterestRate as opposed to the method annualInterestRate

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

      Happy coding …. by the way get cracking on your C++ too :D

    • #47432
      Anonymous
      Inactive

      Cool excellent stuff everything duly noted! :)

      Your not a lecturer of mine by any chance with the whole c++ thing or are you an active member in the civil programming war between java and c++ :D

      Thanks

    • #47433
      Anonymous
      Inactive

      No civil war just prefer C++ 8)

    • #47434
      Anonymous
      Inactive

      BTW Java runs on top of something called a JVM …. written in C.

    • #47438
      Anonymous
      Inactive

      Raw machine code is where it’s at really. Also, C is not C++ as you know ;)

    • #47440
      Anonymous
      Inactive

      They’re all just tools. One tool suits one purpose, another tool suits a different one, and the world keeps spinning round.

    • #47441
      Anonymous
      Inactive

      Also, so save you confusing problems like this, don’t give your class and function variables the same name…that’s bad practice and will give you headaches.

Viewing 7 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.