2 Replies - 2349 Views - Last Post: 07 February 2012 - 12:54 AM Rate Topic: -----

#1 teamsilvamma   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 153
  • Joined: 17-January 12

SavingsAccount Program

Posted 06 February 2012 - 12:26 PM

Hello,

I need some help with this program. The program runs except, I have it running my way, and the professor wants it her way.

This is what she wants us to use:

"In the SavingsAccount class, declare a class variable called annualInterestRate,
an instance constant called ACCOUNT_NUMBER, and an instance variable called
balance. Provide a two-parameter constructor to initialize the instance constant and
instance variable, and provide accessors for the instance constant and instance
variable. Provide an addMonthlyInterest method to update the balance, by adding
(balance * annualInterestRate / 12) to the current balance.
You are also required to provide a class method that sets the annual interest rate."

"In the YourNameProg4 Driver class, instantiate the saver1 and saver2 objects. Set
the annual interest rate at 0.05. Print the table heading lines. Use a for loop to
print the initial account numbers and balances and the account numbers and
balances each month after that month’s interest has been added. After the last
month’s printout, compute and display the total of both balances."

Output:

Monthly balances for one year with 0.05 annual interest:

Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2008.33 10003 3012.50
2 10002 2016.70 10003 3025.05
3 10002 2025.10 10003 3037.66
4 10002 2033.54 10003 3050.31
5 10002 2042.02 10003 3063.02
6 10002 2050.52 10003 3075.79
7 10002 2059.07 10003 3088.60
8 10002 2067.65 10003 3101.47
9 10002 2076.26 10003 3114.39
10 10002 2084.91 10003 3127.37
11 10002 2093.60 10003 3140.40
12 10002 2102.32 10003 3153.49
Final balance of both accounts combined: 5255.81

Is This A Good Question/Topic? 0
  • +

Replies To: SavingsAccount Program

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 473
  • View blog
  • Posts: 2,222
  • Joined: 07-April 08

Re: SavingsAccount Program

Posted 06 February 2012 - 12:48 PM

In order for us to help you, you need to do a few things:

1. Show us your code
2. Show us any errors you are receiving, unexpected outputs, or expected outputs that are not happening
3. Show us how you have attempted to fix the problems.
Was This Post Helpful? 0
  • +
  • -

#3 teamsilvamma   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Re: SavingsAccount Program

Posted 07 February 2012 - 12:54 AM

ooops, sorry ,my computer bugged out while in the middle of posting this and then I had to go to work..but here is the code for the Driver

package Assignments;

public class LuisSilvaProg4
{
	
	 public static void main( String args[] ){
		 
		 SavingsAccount saver1 = new SavingsAccount( 2000 );
		 SavingsAccount saver2 = new SavingsAccount( 3000 );
		 SavingsAccount.newInterestRate( 0.05 );
		 
		 System.out.println( "\nMonthly balances for one year with 0.05 annual interest:\n" );


		 	System.out.printf( "%10s%10s%10s%10s%10s\n", "Month", "Account#", "Balance", "Account#", "Balance" );
		 	System.out.printf( "%10s%10s%10s%10s%10s\n", "-----", "--------", "-------", "--------", "-------" );
		 	System.out.printf( "%10s%10s%10s%10s%10s\n", "0","10002", saver1.toString(), "10003", saver2.toString());
	 
		 	for ( int month = 1; month <= 12; month++ ){

		 		switch (month){
		 			case 1:System.out.printf( "\n");
		 			break;
		 			case 2:System.out.printf( "\n");
		 			break;
		 			case 3:System.out.printf( "\n");
		 			break;
		 			case 4:System.out.printf( "\n");
		 			break;
		 			case 5:System.out.printf( "\n");
		 			break;
		 			case 6:System.out.printf( "\n");
		 			break;
		 			case 7:System.out.printf( "\n");
		 			break;
		 			case 8:System.out.printf( "\n");
		 			break;
		 			case 9:System.out.printf( "\n");
		 			break;
		 			case 10:System.out.printf( "\n");
		 			break;
		 			case 11:System.out.printf( "\n");
			 		break;
		 			case 12:System.out.printf( "\n");
		 			break;
		 		}//end switch
	 
		 			String monthLabel = String.format( "%d", month ); 
		 				saver1.addMonthlyInterest(); saver2.addMonthlyInterest();
		 			
		 			System.out.printf( "%10s%10s%10s%10s%10s\n", monthLabel, "10002", 
		 				saver1.toString(), "10003", saver2.toString());
		 	}//end for

		 	System.out.printf( "%10s%10s\n", "\nFinal balance of both accounts combined:", "######");

		 	
	 }//end main
}//end LuisSilvaProg4



And here is the code for the class

package Assignments;

/***********************************************************
 * SavingsAccount.java
 * Luis Silva
 *
 *
 ***********************************************************/
public class SavingsAccount {

// interest rate for all accounts
	private static double annualInterestRate = 0;
	private double Balance;


 	public static void newInterestRate( double newRate ){
		annualInterestRate = (newRate >= 0 && newRate <= 1.0 ) ? newRate : 0.05;
	}//end newInterestRate

// provides account balances
	public SavingsAccount( double balance ){
		Balance = balance;
	}//end Constructor

// calculates the interest for each month
	public void addMonthlyInterest(){
		Balance += Balance * ( annualInterestRate / 12.0 );
	}//end addMonthlyInterest

// get string representation of SavingAccount
	public String toString(){
		return String.format( "$%.2f", Balance );
	}//end accesssor
	
}//end SavingsAccount



Thanks is advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1