5 Replies - 2520 Views - Last Post: 19 November 2012 - 12:55 AM Rate Topic: -----

#1 Kinzer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 12

SavingsAccount and SavingsAccountTest

Posted 18 November 2012 - 06:48 PM

So I have to create a SavingsAccountClass which stores a savings account's annual interest rate and balance. Then I have to test the class in a program that calculates the balance of a savings account at the end of a period of time. I've written both the codes, but I'm having an issue with the SavingsAccount object in the second program. Any help would be much appreciated.

/**
   SavingsAccount class
   Chapter 6, Programming Challenge 10
*/

public class SavingsAccount
{
    double balance;
    double interestRate;
    double totalInterest;
    double totalDeposit;
    double totalWithdraw;

   /**
      The constructor initializes an object with a
      balance and an annual interest rate.
      @param bal The account balance.
      @param intRate The annual interest rate.
   */

   public SavingsAccount(double bal, double intRate)
   {
       balance = bal;
       interestRate = intRate;
       totalDeposit = 0.00;
       totalWithdraw = 0.00;
       totalInterest = 0.00;
   }

   /**
      The withdraw method withdraws an amount from
      the account.
      @param amount The amount to withdraw.
   */

   public void withdraw(double amount)
   {
     totalWithdraw += amount;
     balance -= amount;
   }
   
   /**
      The deposit method deposits an amount into
      the account.
      @param amount The amount to deposit.
   */

   public void deposit(double amount)
   {
     totalDeposit += amount;
     balance += amount;
   }

   /**
      The addInterest method calculates the monthly
      interest and adds it to the account balance.
   */

   public void addInterest()
   {
      // Get the monthly interest rate.
     totalInterest += balance * ((interestRate/12));
      
      // Calculate the last amount of interest earned.
     balance += balance * ((interestRate/12));
      
      
      
   }

   /**
      The getBalance method returns the account balance.
      @return The account balance.
   */

   public double getBalance()
   {
         return balance;  
   }

   /**
      The getInterestRate method returns the annual
      interest rate.
      @return The annual interest rate.
   */

   public double getInterestRate()
   {
          return interestRate;
   }

   /**
      The getLastInterest method returns the last amount
      of interest earned.
      @return The last amount of interest earned.
   */

   public double getLastInterest()
   {
      return totalInterest;
   }

}



And here is the second test program. Like I said the problem I'm getting seems to be with the SavingsAccount object.

import java.util.Scanner;
import java.text.DecimalFormat;

public class SavingsAccountTest
{
   public static void main(String args[])
   {
	double interestRate;                    // Annual interest rate
	double balance;                        // Starting balance
	double deposits;                      // Amount of deposits for a month
	double withdrawn;                    // Amount withdrawn in a month
	double interestEarned = 0.0;        // Interest earned
	double totalDeposits = 0;          // Deposit accumulator
	double totalWithdrawn = 0;        // Withdrawal accumulator
        double months;                   // Months that have passed

      // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);
     
      // Get the starting balance.
        System.out.print("Enter the savings account's " +
                       "starting balance: ");
      balance = keyboard.nextDouble();

      // Get the annual interest rate.
        System.out.print("Enter the savings account's " +
                       "annual interest rate: ");
      interestRate = keyboard.nextDouble();
      
      // Create a SavingsAccount object.
        SavingsAccount savings =
             new SavingsAccount(balance, interestRate);
        
      // Get the number of months that have passed.
        System.out.print("How many months have passed since " +
                       "the account was opened? ");
      months = keyboard.nextInt();
      
      // Get the deposits and withdrawals for each month.
      for (int i = 1; i <= months; i++)
      {
         // Get the deposits.
          System.out.print("Enter the amount deposited " +
                          "during month " + i + ": ");
         deposits = keyboard.nextDouble();
         totalDeposits += deposits;

         // Get the withdrawals.
            System.out.print("Enter the amount withdrawn " +
                          "during month " + i + ": ");
         withdrawn = keyboard.nextDouble();
         totalWithdrawn += withdrawn;
         
         // Add the deposits to the account.
             savings.deposits(deposits);
         
         // Subtract the withdrawals.
            savings.withdrawn(withdrawn);
         
         // Add the monthly interest.
            savings.addInterest();
         
         // Accumulate the amount of interest earned.
            interestEarned += savings.getLastInterest();
      }
      
      // Create a DecimalFormat object for formatting output.
      DecimalFormat dollar = new DecimalFormat("#,##0.00");
      
      // Display the totals and the balance.    
         System.out.println("Total deposited: $" +
                         dollar.format(totalDeposits));
         System.out.println("Total withdrawn: $" +
                         dollar.format(totalWithdrawn));
         System.out.println("Interest earned: $" +
                         dollar.format(interestEarned));
         System.out.println("Ending balance: $" +
                         dollar.format(savings.getBalance()));
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: SavingsAccount and SavingsAccountTest

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: SavingsAccount and SavingsAccountTest

Posted 18 November 2012 - 06:56 PM

View PostKinzer, on 18 November 2012 - 09:48 PM, said:

I'm having an issue with the SavingsAccount object in the second program.

Which means ???
Was This Post Helpful? 0
  • +
  • -

#3 Kinzer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 12

Re: SavingsAccount and SavingsAccountTest

Posted 18 November 2012 - 08:03 PM

Well I'm not exactly sure what the problem is, but when I try to run the program I get to the point where I input the interest rate, and then I get this error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class SavingsAccount
location: class SavingsAccountTest
at SavingsAccountTest.main(SavingsAccountTest.java:35)
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: SavingsAccount and SavingsAccountTest

Posted 18 November 2012 - 08:09 PM

It simply means that you shouldn't try to run a Java program that has compilation errors.
For sure it will eventually fails.

Fix the compilation error(s) you have before trying to run your program,
Was This Post Helpful? 0
  • +
  • -

#5 Kinzer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 12

Re: SavingsAccount and SavingsAccountTest

Posted 18 November 2012 - 08:37 PM

I understand that, I guess the issue is that it's supposed to test the SavingsAccount class and it isn't, and I'm just not sure how to make it do that. Could the problem be that I didn't save the first program correctly and that's why it's not "finding" it?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: SavingsAccount and SavingsAccountTest

Posted 19 November 2012 - 12:55 AM

Are the two source files saved in the same folder? Are the files named properly, ClassName.java?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1