Results 1 to 20 of 20
  1. #1
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default calculating balance on savings account

    I did my coding but it doesn't calculate right the interest in part2.
    I think I have something wrong in my code in the class SpecialSavings in the method CalculateMonthlyInterest.
    Could somebody give me advice about my codding

    Java Code:
    package p1;
    
    import corejava.*;
    
    public class driverProgram 
    {
    	public static void main ( String [ ] arg )
    	{
    	//Part1
    		SavingsAccount saver1 = new SavingsAccount( 2000.00 );
    		SavingsAccount saver2 = new SavingsAccount( 3000.00 );
    		System.out.printf( "Savings balance at the beginning \n");
    		printBalancePart1( saver1, saver2 );
    		
    		SavingsAccount.setInterestRate( 0.04 );
    		System.out.printf( "Savings balance after 1st month with the annual interest rate 4%% \n");
    		printBalanceWithInterestPart1( saver1, saver2 );
    		
    		SavingsAccount.modifyInterestRate( 0.05 );
    		System.out.printf( "Savings balance after 2nd month with the annual interest rate 5%% \n");
    		printBalanceWithInterestPart1( saver1, saver2 );
    	//Part2
    		System.out.printf( "\n\nPart2\n********************************************************\n\n");
    		
    		SpecialSavings saverNumber1 = new SpecialSavings( 2000.00 );
    		SpecialSavings saverNumber2 = new SpecialSavings( 3000.00 );
    		System.out.printf( "Savings balance at the beginning \n");
    		printBalancePart2( saverNumber1, saverNumber2 );
    		
    		String answer;
    		boolean continueProcess = true;
    		int countWithdraws = 0, countDeposits = 0;
    		
    		do 
    		{
    			answer = Console.readLine("Do you want to withdraw or deposit some money? Y/N: ");
    			if( answer.equalsIgnoreCase("Y") )
    			{
    				String choice;
    				do 
    				{
    					choice = Console.readLine( "Enter 'withdraw' or 'deposit': " );
    					if( choice.equalsIgnoreCase( "withdraw" ) )
    					{
    						boolean continueWithdraw = true;
    						do
    						{
    							choice = Console.readLine( "Enter first/second/both account(s)?: " );
    							if( choice.equalsIgnoreCase( "first" ) )
    							{
    								saverNumber1.withdrawMoney();
    								continueWithdraw = false;
    							}
    							else if ( choice.equalsIgnoreCase( "second" ) )
    							{
    								saverNumber2.withdrawMoney();
    								continueWithdraw = false;
    							}
    							else if ( choice.equalsIgnoreCase( "both" ) )
    							{
    								System.out.printf( "First account: " );
    								saverNumber1.withdrawMoney(); 
    								System.out.printf( "Second account: " );
    								saverNumber2.withdrawMoney();
    								continueWithdraw = false;
    							}
    							else 
    							{
    								continueWithdraw = printError();
    							}
    						} while ( continueWithdraw );
    						countWithdraws++;
    						continueProcess = false;
    					}
    					else if ( choice.equalsIgnoreCase("deposit") )
    					{
    						boolean continueDeposit = true;
    						do
    						{
    							choice = Console.readLine("Enter first/second/both account(s)?: ");
    							if( choice.equalsIgnoreCase("first") )
    							{
    								saverNumber1.depositMoney();
    								continueDeposit = false;
    							}	
    							else if ( choice.equalsIgnoreCase("second") )
    							{
    								saverNumber2.depositMoney();
    								continueDeposit = false;
    							}
    							else if ( choice.equalsIgnoreCase("both") )
    							{
    								System.out.printf("First account: ");
    								saverNumber1.depositMoney();
    								System.out.printf("Second account: ");
    								saverNumber2.depositMoney();
    								continueDeposit = false;
    							}
    							else 
    							{
    								continueDeposit = printError();
    							}
    							
    						}while(continueDeposit);
    						countDeposits++;
    						continueProcess = false;
    					}
    					else 
    					{
    						continueProcess = printError();
    					}
    				
    				}while(continueProcess);
    				continueProcess = true;
    			}
    			else if( answer.equalsIgnoreCase("N") )
    			{   
    				continueProcess = false;
    			}
    			else
    			{   
    				continueProcess = printError();
    			}
    		} while( continueProcess );
    	
    		if( (countWithdraws > 0 ) || (countDeposits > 0) )
    		{
    			System.out.printf( "\nSavings balance after " + countWithdraws + " withdraws and " + countDeposits + " deposits \n");
    			printBalancePart2(saverNumber1, saverNumber2);
    		}
    		
    		System.out.printf( "Savings balance after adding monthly interest \n");
    		printBalanceWithInterestPart2( saverNumber1, saverNumber2 );	
    	}
    	
    	public static void printBalancePart1( SavingsAccount saver1, SavingsAccount saver2 )
    	{
    		System.out.printf( "saver1: %.2f\n", saver1.getSavingsBalance() );
    		System.out.printf( "saver2: %.2f\n\n", saver2.getSavingsBalance() );
    	}
    	
    	public static void printBalanceWithInterestPart1( SavingsAccount saver1, SavingsAccount saver2 )
    	{
    		System.out.printf( "saver1\tInterest: %.2f\tSavings Balance: %.2f\n", saver1.CalculateMonthlyInterest(), saver1.getSavingsBalance() );
    		System.out.printf( "saver2\tInterest: %.2f\tSavings Balance: %.2f\n\n", saver2.CalculateMonthlyInterest(), saver2.getSavingsBalance() );	
    	}
    	
    	public static void printBalancePart2( SpecialSavings saverNumber1, SpecialSavings saverNumber2 )
    	{
    		System.out.printf( "saverNumber1: %.2f\n", saverNumber1.getSavingsBalance() );
    		System.out.printf( "saverNumber2: %.2f\n\n", saverNumber2.getSavingsBalance() );
    	}
    	
    	public static void printBalanceWithInterestPart2( SpecialSavings saverNumber1, SpecialSavings saverNumber2 )
    	{
    		System.out.printf( "saverNumber1\tInterest: %.2f\tSavings Balance: %.2f\n", saverNumber1.CalculateMonthlyInterest(), saverNumber1.getSavingsBalance() + saverNumber1.CalculateMonthlyInterest() );
    		System.out.printf( "saverNumber2\tInterest: %.2f\t\tSavings Balance: %.2f\n", saverNumber2.CalculateMonthlyInterest(), saverNumber2.getSavingsBalance() + saverNumber2.CalculateMonthlyInterest() );
    	}
    	
    	public static boolean printError( )
    	{
    		boolean continueProcess;
    		String choice; 
    		
    		do
    		{
    			choice = Console.readLine("Error. Enter 'Y' to continue");
    			continueProcess = true;
    		} while( !choice.equalsIgnoreCase("Y") );
    		
    		return continueProcess;
    	}
    }
    
    
    package p1;
    
    import corejava.*;
    
    public class SavingsAccount 
    {
    	static double annualInterestRate; 
    	private double savingsBalance; 
    	
    	SavingsAccount() { }
    	
    	SavingsAccount( double savingsBalance ) 
    	{ 
    	setSavingsBalance(savingsBalance);
    	}
    	
    	protected void setSavingsBalance( double savingsBalance ) 
    	{ 
    		this.savingsBalance = savingsBalance;
    	}
    	
    	protected double getSavingsBalance() 
    	{ 
    		return savingsBalance; 
    	}
    	
    	protected static void setInterestRate( double annualInterestRate ) 
    	{ 
    		SavingsAccount.annualInterestRate = annualInterestRate ;
    	}
    	
    	protected double CalculateMonthlyInterest()
    	{
    		double monthlyInterest = this.savingsBalance * ( annualInterestRate  / 12 ) ;  
    		this.savingsBalance += monthlyInterest;
    	
    		return monthlyInterest;
    	}
    	
    	protected static void modifyInterestRate( double annualInterestRate )
    	{
    		setInterestRate( annualInterestRate ); 
    	}
    	
    	protected void withdrawMoney()
    	{	
    		double withdraw;
    		do
    		{
    			withdraw = Console.readDouble("How much money do you want to withdraw? ");
    			if ( withdraw <= this.savingsBalance)
    			{
    				this.savingsBalance -= withdraw; 
    			}
    			else 
    			{
    				System.out.printf( "Error. You have less money on your account than you want to withdraw.\n" +
    						           "Account balace: " + this.savingsBalance + "\n" );
    			}
    		} while( withdraw > this.savingsBalance );
    	}
    	
    	protected void depositMoney()
    	{
    		double deposit = Console.readDouble("How much money do you want to deposit? ");
    		this.savingsBalance += deposit; 
    	}
    
    }
    
    
    
    package p1;
    
    public class SpecialSavings extends SavingsAccount 
    {
    	SpecialSavings() { }
    	
    	SpecialSavings( double savingsBalance )
    	{
    		super(savingsBalance);
    	}
    
    	protected double CalculateMonthlyInterest()
    	{
    		if( getSavingsBalance() >= 10000.00 )
    		{
    			setInterestRate( 0.1 );
    		}
    		else
    		{
    			setInterestRate( 0.04 );
    		}
    
    		SavingsAccount count = new SavingsAccount();
    		double monthlyInterest = count.CalculateMonthlyInterest();
    
    		return monthlyInterest;
    	} 
    }
    Last edited by Norm; 02-21-2012 at 09:51 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    it doesn't calculate right the interest in part2.
    Do you have to correct formula for computing the interest?
    give me advice about my coding
    Add some printlns to the code to print out the values of the variables and the results of equations to see what the code is doing while it is executing. If you know what the code is supposed to do, then when you see what it does, you should be able to figure out what the problem is.

    Post the output from the program and add comments to the output that shows where the values are wrong and show what the values should be.

  3. #3
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    Hi,

    Norm thank you for respond. I think the equation to calculate
    monthly interest is good. I checked it.

    This is the output. I made some comments almost at the end what cal
    wrong and how it should look.

    ************************************************** *******
    Savings balance at the beginning
    saver1: 2000.00
    saver2: 3000.00

    Savings balance after 1st month with the annual interest rate 4%
    saver1 Interest: 6.67 Savings Balance: 2006.67
    saver2 Interest: 10.00 Savings Balance: 3010.00

    Savings balance after 2nd month with the annual interest rate 5%
    saver1 Interest: 8.36 Savings Balance: 2015.03
    saver2 Interest: 12.54 Savings Balance: 3022.54



    Part2
    ************************************************** ******

    Savings balance at the beginning
    saverNumber1: 2000.00
    saverNumber2: 3000.00

    Do you want to withdraw or deposit some money? Y/N: Y
    Enter 'withdraw' or 'deposit': deposit
    Enter first/second/both account(s)?: both
    First account: How much money do you want to deposit? 10000
    Second account: How much money do you want to deposit? 0
    Do you want to withdraw or deposit some money? Y/N: N

    Savings balance after 0 withdraws and 1 deposits
    saverNumber1: 12000.00
    saverNumber2: 3000.00

    Savings balance after adding monthly interest
    saverNumber1 Interest: 0.00 Savings Balance: 12000.00
    saverNumber2 Interest: 0.00 Savings Balance: 3000.00

    //In 2 lines above it didn't calc Interest which should be added to saving balance.
    The equation to cal interest is:
    monthlyInterest = savingsBalance * ( annualInterestRate / 12 ) ;

    saverNumber1 Interest: 12000 *(0.1 / 12 ) = 100.00
    saverNumber2 Interest: 3000 *(0.04 / 12) = 10.00

    The output should look like should look like:

    saverNumber1 Interest: 100.00 Savings Balance: 12100.00
    saverNumber2 Interest: 10.00 Savings Balance: 3010.00

    Thank you again for every little help :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    saverNumber1 Interest: 0.00 Savings Balance: 12000.00
    I assume the error here is the 0.00 value.
    Where does that value come from? How is it computed? Add some printlns to the code where the interest is computed that prints out the values of ALL of the variables that are used to compute the interest. The print out should show you where the program is not working like you want.

  5. #5
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    Norm thank you so much for so quick respond

    the error is: Interest: 0.00 yes

    If you notice my program in part one calculates everything correctly. The values are set up in the program.
    In part two special savings are also set up but then their value changes depending how many withdraw and deposits you do.
    Then the Annual interest rate is chosen depending form the amount of money on your account.
    In the class SpecialSavings there is method which sets the interest and I displayed it below.
    Also I added few printing statement and highlited them because i wanted to see what values are going to be printed

    protected double CalculateMonthlyInterest()
    {
    if( getSavingsBalance() >= 10000.00 )
    {
    setInterestRate( 0.1 );
    }
    else
    {
    setInterestRate( 0.04 );
    }
    System.out.println( " Annual Interest Rate: " + annualInterestRate );

    SavingsAccount count = new SavingsAccount();
    double monthlyInterest = count.CalculateMonthlyInterest();
    System.out.println( "Monthly interest rate:" + monthlyInterest );
    return monthlyInterest;
    }

    It sets well annual Interest Rate. I can see it from print statement but savings balance
    isn't set well. from printing statement it looks like is 0.
    Special savings is set up in the method from class SavingsAccount. I also made some printing here

    protected double CalculateMonthlyInterest()
    {
    System.out.println( " Annual Interest Rate: " + annualInterestRate );
    System.out.println( " Savings Balance: " + this.savingsBalance );

    double monthlyInterest = this.savingsBalance * ( annualInterestRate / 12 ) ;
    this.savingsBalance += monthlyInterest;
    System.out.println( " Savings Balance: " + this.savingsBalance );
    return monthlyInterest;
    }

    This is the output of all program:

    Savings balance at the beginning
    saver1: 2000.00
    saver2: 3000.00

    Savings balance after 1st month with the annual interest rate 4%
    Annual Interest Rate: 0.04
    Savings Balance: 2000.0
    Savings Balance: 2006.6666666666667
    saver1 Interest: 6.67 Savings Balance: 2006.67
    Annual Interest Rate: 0.04
    Savings Balance: 3000.0
    Savings Balance: 3010.0
    saver2 Interest: 10.00 Savings Balance: 3010.00

    Savings balance after 2nd month with the annual interest rate 5%
    Annual Interest Rate: 0.05
    Savings Balance: 2006.6666666666667
    Savings Balance: 2015.0277777777778
    saver1 Interest: 8.36 Savings Balance: 2015.03
    Annual Interest Rate: 0.05
    Savings Balance: 3010.0
    Savings Balance: 3022.5416666666665
    saver2 Interest: 12.54 Savings Balance: 3022.54



    Part2
    ************************************************** ******

    Savings balance at the beginning
    saverNumber1: 2000.00
    saverNumber2: 3000.00

    Do you want to withdraw or deposit some money? Y/N: Y
    Enter 'withdraw' or 'deposit': deposit
    Enter first/second/both account(s)?: both
    First account: How much money do you want to deposit? 10000
    Second account: How much money do you want to deposit? 0
    Do you want to withdraw or deposit some money? Y/N: N

    Savings balance after 0 withdraws and 1 deposits
    saverNumber1: 12000.00
    saverNumber2: 3000.00

    Savings balance after adding monthly interest
    Annual Interest Rate: 0.1
    Annual Interest Rate: 0.1 // set up well
    Savings Balance: 0.0 // here is problem
    Savings Balance: 0.0
    Monthly interest rate:0.0
    Annual Interest Rate: 0.1
    Annual Interest Rate: 0.1
    Savings Balance: 0.0 // here is problem
    Savings Balance: 0.0
    Monthly interest rate:0.0 // here is problem
    saverNumber1 Interest: 0.00 Savings Balance: 12000.00
    Annual Interest Rate: 0.04
    Annual Interest Rate: 0.04
    Savings Balance: 0.0
    Savings Balance: 0.0
    Monthly interest rate:0.0
    Annual Interest Rate: 0.04
    Annual Interest Rate: 0.04
    Savings Balance: 0.0
    Savings Balance: 0.0
    Monthly interest rate:0.0
    saverNumber2 Interest: 0.00 Savings Balance: 3000.00



    Also maybe highlighted line should like this because SavingsAccount(getSavingsBallance()) overriten constructor has savingsBalance variable in the parenthesis


    protected double CalculateMonthlyInterest()
    {
    if( getSavingsBalance() >= 10000.00 )
    {
    setInterestRate( 0.1 );
    }
    else
    {
    setInterestRate( 0.04 );
    }
    System.out.println( " Annual Interest Rate: " + annualInterestRate );

    SavingsAccount count = new SavingsAccount(getSavingsBallance());
    double monthlyInterest = count.CalculateMonthlyInterest();
    System.out.println( "Monthly interest rate:" + monthlyInterest );
    return monthlyInterest;
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    Savings Balance: 0.0 // here is problem
    Why is the value 0? Where does the program set the value so it is not zero?

    The program is very hard to test because it requires the user to enter many numbers as data.
    For testing it would be better if all of the questions were answered in the program so the program could be executed without any user input.

  7. #7
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    Norm I did all the things which you possibly could advice me
    1. Wrote my code without any input data.
    2. Described in the code important things.
    3. Highlighted things which are also important.
    4. Printed out test cases and see the problem.
    5. I notice my problem is logic error.
    If you see part1 is everything calculated correctly. Why? The reason is that I am using only SavingAccount. cl ass and main. There everything is correct.
    If you see part2 is calculating wrong the interest and special savings accounts balance.
    I have to use here all the classes. When in the method CalculateMonthlyInterest in SpecialSavings. class
    i call the overriten method CalculateMonthlyInterest in SavingsAccount.class here is created logic error.
    That what I think. How to fix it? I posted execution results at the end of this post and I highlighted things which are
    calculated wrong. Also I wrote from where come from this calculations.


    Java Code:
    package p4;
    import corejava.*;
    
    public class driverMain
    {
    	public static void main ( String [ ] arg )
    	{
    	//Part1 
    
    		    System.out.printf( "\n\nPart1\n********************************************************\n\n");
    		
    			SavingsAccount saver1 = new SavingsAccount( 2000.00 );
    			SavingsAccount saver2 = new SavingsAccount( 3000.00 );
    			System.out.printf( "Savings balance at the beginning \n");
    			System.out.printf( "saver1: %.2f\n", saver1.getSavingsBalance() );
    			System.out.printf( "saver2: %.2f\n\n", saver2.getSavingsBalance() );
    			
    			
    			SavingsAccount.setInterestRate( 0.04 );
    			System.out.printf( "Savings balance after 1st month with the annual interest rate 4%% \n");
    			System.out.printf( "saver1\tInterest: %.2f\tSavings Balance: %.2f\n", saver1.CalculateMonthlyInterest(), 
                                                       saver1.getSavingsBalance() );
    			System.out.printf( "saver2\tInterest: %.2f\tSavings Balance: %.2f\n\n", saver2.CalculateMonthlyInterest(), 
                                                       saver2.getSavingsBalance() );	
    		
    			
    			SavingsAccount.modifyInterestRate( 0.05 );
    			System.out.printf( "Savings balance after 2nd month with the annual interest rate 5%% \n");
    			System.out.printf( "saver1\tInterest: %.2f\tSavings Balance: %.2f\n", saver1.CalculateMonthlyInterest(), 
                                                       saver1.getSavingsBalance() );
    			System.out.printf( "saver2\tInterest: %.2f\tSavings Balance: %.2f\n\n", saver2.CalculateMonthlyInterest(), 
                                                       saver2.getSavingsBalance() );	
    		
    	//Part2
    			System.out.printf( "\n\nPart2\n********************************************************\n\n");
    			
    			SpecialSavings saverNumber1 = new SpecialSavings( 2000.00 );
    			SpecialSavings saverNumber2 = new SpecialSavings( 3000.00 );
    
    			System.out.printf( "Savings balance at the beginning \n");
    			System.out.printf( "saverNumber1: %.2f\n", saverNumber1.getSavingsBalance() );
    			System.out.printf( "saverNumber2: %.2f\n\n", saverNumber2.getSavingsBalance() );
    
    			saverNumber1.withdrawMoney(200.00);
    			saverNumber2.withdrawMoney(300);				
    			saverNumber1.depositMoney(10000);
    			saverNumber2.depositMoney(0);
    			
    			System.out.printf( "Savings balance after withdraws and deposits \n");
    			System.out.printf( "saverNumber1: %.2f\n", saverNumber1.getSavingsBalance() );
    			System.out.printf( "saverNumber2: %.2f\n\n", saverNumber2.getSavingsBalance() );
    			
    			System.out.printf( "Savings balance after adding monthly interest \n");
    			System.out.printf( "saverNumber1\tInterest: %.2f\tSavings Balance: %.2f\n", saverNumber1.CalculateMonthlyInterest(),                                                   saverNumber1.getSavingsBalance()  );
    			System.out.printf( "saverNumber2\tInterest: %.2f\t\tSavings Balance: %.2f\n", saverNumber2.CalculateMonthlyInterest()                                                  , saverNumber2.getSavingsBalance() );						
    			
    	}
    }
    
    
    package p4;
    
    import corejava.*;
    
    public class SavingsAccount 
    {
    	static double annualInterestRate; 
    	private double savingsBalance; 
    	
    	SavingsAccount() { }
    	
    	SavingsAccount( double savingsBalance ) 
    	{ 
    	setSavingsBalance(savingsBalance);
    	}
    	
    	protected void setSavingsBalance( double savingsBalance ) 
    	{ 
    		this.savingsBalance = savingsBalance;
    	}
    	
    	protected double getSavingsBalance() 
    	{ 
    		return savingsBalance; 
    	}
    	
    	protected static void setInterestRate( double annualInterestRate ) 
    	{ 
    		SavingsAccount.annualInterestRate = annualInterestRate ;
    	}
    	
    	protected double CalculateMonthlyInterest()
    	{
    		System.out.println( "Annual Interest Rate: " + annualInterestRate + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method, 
                                                class SavingsAccount");
    		System.out.printf( "Savings Balance: %.2f \t\t\t\t\t\t// print in CalculateMonthlyInterest method, class SavingsAccount\n", 
                                               this.savingsBalance );
    				
    		double monthlyInterest = this.savingsBalance * ( annualInterestRate  / 12 ) ;  
    		this.savingsBalance += monthlyInterest;
    		
    		System.out.printf( "Savings Balance after after adding monthly interest: %.2f \t\t//print in CalculateMonthlyInterest method, 
                                                class SavingsAccount\n", this.savingsBalance );
    		
    		return monthlyInterest;
    	}
    	
    	protected static void modifyInterestRate( double annualInterestRate )
    	{
    		setInterestRate( annualInterestRate ); 
    	}
    	
    	protected void withdrawMoney(double withdraw)
    	{	
    
    		do
    		{
    			if ( withdraw <= this.savingsBalance)
    			{
    				this.savingsBalance -= withdraw; 
    			}
    			else 
    			{
    				System.out.printf( "Error. You have less money on your account than you want to withdraw.\n" +
    						           "Account balace: " + this.savingsBalance + "\n" );
    			}
    		} while( withdraw > this.savingsBalance );
    	}
    	
    	protected void depositMoney(double deposit)
    	{
    		this.savingsBalance += deposit; 
    	}
    
    }
    
    
    
    
    
    package p4;
    
    
    public class SpecialSavings  extends SavingsAccount 
    {
    	SpecialSavings() { }
    	
    	SpecialSavings( double savingsBalance )
    	{
    		super(savingsBalance);
    	}
    
    	protected double CalculateMonthlyInterest()
    	{
    		if( getSavingsBalance() >= 10000.00 )
    		{
    			setInterestRate( 0.1 );
    		}
    		else
    		{
    			setInterestRate( 0.04 );
    		}
    		
    		System.out.println( "Annual Interest Rate: " + annualInterestRate + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method, 
                                               class SpeciaSavings" );
    
    		SavingsAccount count = new SavingsAccount();
    		double monthlyInterest = count.CalculateMonthlyInterest();
    		System.out.println( "Monthly interest rate:"  + monthlyInterest + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method,
                                                class SpeciaSavings" );
    		
    		return monthlyInterest;
    	} 
    }

    /* Execution Results

    Part1
    ************************************************** ******

    Savings balance at the beginning
    saver1: 2000.00
    saver2: 3000.00

    Savings balance after 1st month with the annual interest rate 4%
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 2000.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after adding monthly interest: 2006.67//print in CalculateMonthlyInterest method, class SavingsAccount
    saver1 Interest: 6.67 Savings Balance: 2006.67
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 3000.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after adding monthly interest: 3010.00//print in CalculateMonthlyInterest method, class SavingsAccount
    saver2 Interest: 10.00 Savings Balance: 3010.00

    Savings balance after 2nd month with the annual interest rate 5%
    Annual Interest Rate: 0.05 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 2006.67 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after adding monthly interest: 2015.03//print in CalculateMonthlyInterest method, class SavingsAccount
    saver1 Interest: 8.36 Savings Balance: 2015.03
    Annual Interest Rate: 0.05 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 3010.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after adding monthly interest: 3022.54//print in CalculateMonthlyInterest method, class SavingsAccount
    saver2 Interest: 12.54 Savings Balance: 3022.54


    Part2
    ************************************************** ******

    Savings balance at the beginning
    saverNumber1: 2000.00
    saverNumber2: 3000.00

    Savings balance after withdraws and deposits
    saverNumber1: 11800.00
    saverNumber2: 2700.00

    Savings balance after adding monthly interest
    Annual Interest Rate: 0.1 // print in CalculateMonthlyInterest method, class SpeciaSavings
    Annual Interest Rate: 0.1 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 0.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after adding monthly interest: 0.00 //print in CalculateMonthlyInterest method, class SavingsAccount
    Monthly interest rate:0.0 // print in CalculateMonthlyInterest method, class SpeciaSavings
    saverNumber1 Interest: 0.00 Savings Balance: 11800.00
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SpeciaSavings
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 0.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after adding monthly interest: 0.00 //print in CalculateMonthlyInterest method, class SavingsAccount
    Monthly interest rate:0.0 // print in CalculateMonthlyInterest method, class SpeciaSavings
    saverNumber2 Interest: 0.00 Savings Balance: 2700.00


    */
    Last edited by Bonia; 02-28-2012 at 07:13 AM.

  8. #8
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: calculating balance on savings account

    what is the purpose of line 167? It seems you are creating a new account with a 0 balance and then doing the calculations against that. Then you return the value from that calculation.

  9. #9
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    Thank you yellowledbet for respond

    I wrote line 167 because this is only one way which I know to access to the
    method CalculateMonthlyInterest in class SavingsAccount. In part2
    I am trying to calculate monthly interest for already created two special
    accounts saverNumber1 and saverNumber2 in driverMain.class
    If you notice from my Execution Results in part2 at the and of my previous post you
    will see that two account have savings balance but monthly interest is not calculated.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    You must remove this statement and get the data you need from where it is being stored.
    Java Code:
       SavingsAccount count = new SavingsAccount();

  11. #11
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    You must remove this statement and get the data you need from where it is being stored.
    Java Code:
    1
    SavingsAccount count = new SavingsAccount();

    Norm I removed that line.
    So now I have compiling error because program doesn't know which CalculateMonthlyInterestMethod to call.
    So how do I need to code this part of the that it works?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    Where is the data that you need to access?
    Is it already in an object somewhere?
    How can you get to that object and its data?

  13. #13
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    Norm
    Where is the data that you need to access?
    I need to acces to the method CalculateMonthlyInterestRate in
    SavingAccount.class from CalculateMonthlyInterestRate method
    in SpecialSavings.class. I described all that in my previous post.

    Is it already in an object somewhere?
    object for SavingsAccount. class was created in main.
    I also created before the second object count in SpecialSavings.class
    to access to the SavingsAccount.class. It is the one you suggested
    me to remove. You see I don't know the other way to access to that class.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    way to access to that class.
    What is the relationship between the class where the data is and the class where you are trying to get access to the data?
    Does the current class extend the class where the data is?
    There is a way to get to the super class of a derived class.
    Look at lines 261 and 152

  15. #15
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    I don't the way which are telling me.

    There is relationship between classes which I am talking about.
    public class SpecialSavings extends SavingsAccount


    CalculateMonthlyInterestRate method in SpecialSavings.class
    calls method CalculateMonthlyInterest in class SavingsAccount
    because I need to calculate monthly interest. :)
    So method CalculateMonthlyInterest in class SavingsAccount
    after calulations adds monthly interest to special savings
    account balance. That why is so important to call this method.

    You see I am very new to Java and my knowledge is very small
    about this language.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    Did you look at the lines I told you to look at?
    CalculateMonthlyInterestRate method in class SpecialSavings
    calls method CalculateMonthlyInterest in class SavingsAccount
    Yes that is what you need to do.
    Your code does something like that in lines 261 and 152.
    Both those locations use "super" to call a method in the class that is extended.

  17. #17
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    I don't see connection between those two lines :(
    152. }
    261. if( getSavingsBalance() >= 10000.00 )

    How to use super in this moment?

    Do you sugest me to use super in the highlighted code

    public class SpecialSavings extends SavingsAccount
    {
    SpecialSavings() { }

    SpecialSavings( double savingsBalance )
    {
    super(savingsBalance);
    }

    protected double CalculateMonthlyInterest()
    {
    if( getSavingsBalance() >= 10000.00 )
    {
    setInterestRate( 0.1 );
    }
    else
    {
    setInterestRate( 0.04 );
    }

    System.out.println( "Annual Interest Rate: " + annualInterestRate + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method,
    class SpeciaSavings" );


    double monthlyInterest = CalculateMonthlyInterest();
    System.out.println( "Monthly interest rate:" + monthlyInterest + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method,
    class SpeciaSavings" );

    return monthlyInterest;
    }
    }
    Last edited by Bonia; 02-29-2012 at 01:27 AM.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    In the CalculateMonthlyInterest method in class SpecialSavings you need to call the method CalculateMonthlyInterest in class SavingsAccount.
    Here you call the constructor:
    super(savingsBalance);

  19. #19
    Bonia is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: calculating balance on savings account

    Hi Norm. I used super and it worked. I highlighted what I changed and the result is correct. :)
    Thanks for helping me.

    protected double CalculateMonthlyInterest()
    {
    if( getSavingsBalance() >= 10000.00 )
    {
    setInterestRate( 0.1 );
    }
    else
    {
    setInterestRate( 0.04 );
    }

    System.out.println( "Annual Interest Rate: " + annualInterestRate + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method, class SpeciaSavings" );
    double monthlyInterest = super.CalculateMonthlyInterest();
    System.out.println( "Monthly interest rate:" + monthlyInterest + "\t\t\t\t\t\t// print in CalculateMonthlyInterest method, class SpeciaSavings" );

    return monthlyInterest;
    }


    This is output




    Part1
    ************************************************** ******

    Savings balance at the beginning
    saver1: 2000.00
    saver2: 3000.00

    Savings balance after 1st month with the annual interest rate 4%
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 2000.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after after adding monthly interest: 2006.67 //print in CalculateMonthlyInterest method, class SavingsAccount
    saver1 Interest: 6.67 Savings Balance: 2006.67
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 3000.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after after adding monthly interest: 3010.00 //print in CalculateMonthlyInterest method, class SavingsAccount
    saver2 Interest: 10.00 Savings Balance: 3010.00

    Savings balance after 2nd month with the annual interest rate 5%
    Annual Interest Rate: 0.05 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 2006.67 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after after adding monthly interest: 2015.03 //print in CalculateMonthlyInterest method, class SavingsAccount
    saver1 Interest: 8.36 Savings Balance: 2015.03
    Annual Interest Rate: 0.05 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 3010.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after after adding monthly interest: 3022.54 //print in CalculateMonthlyInterest method, class SavingsAccount
    saver2 Interest: 12.54 Savings Balance: 3022.54



    Part2
    ************************************************** ******

    Savings balance at the beginning
    saverNumber1: 2000.00
    saverNumber2: 3000.00

    Savings balance after withdraws and deposits
    saverNumber1: 11800.00
    saverNumber2: 2700.00

    Savings balance after adding monthly interest
    Annual Interest Rate: 0.1 // print in CalculateMonthlyInterest method, class SpeciaSavings
    Annual Interest Rate: 0.1 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 11800.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after after adding monthly interest: 11898.33 //print in CalculateMonthlyInterest method, class SavingsAccount
    Monthly interest rate:98.33333333333333 // print in CalculateMonthlyInterest method, class SpeciaSavings
    saverNumber1 Interest: 98.33 Savings Balance: 11898.33
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SpeciaSavings
    Annual Interest Rate: 0.04 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance: 2700.00 // print in CalculateMonthlyInterest method, class SavingsAccount
    Savings Balance after after adding monthly interest: 2709.00 //print in CalculateMonthlyInterest method, class SavingsAccount
    Monthly interest rate:9.0 // print in CalculateMonthlyInterest method, class SpeciaSavings
    saverNumber2 Interest: 9.00 Savings Balance: 2709.00

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,410
    Rep Power
    25

    Default Re: calculating balance on savings account

    glad you got it working like you wanted.

Similar Threads

  1. Savings Account - Need to list months
    By Moka in forum New To Java
    Replies: 6
    Last Post: 04-21-2011, 04:28 AM
  2. Savings account balance comparison code.
    By Rhyssa6 in forum New To Java
    Replies: 2
    Last Post: 04-10-2011, 11:16 AM
  3. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  4. Savings Account Comparison
    By shivam kush in forum Networking
    Replies: 0
    Last Post: 10-19-2010, 10:03 AM
  5. Color Balance
    By THEAniKan in forum Java 2D
    Replies: 2
    Last Post: 09-14-2009, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •