SavingsAccount

Private variables and methods galore :/

Page 1 of 1

3 Replies - 5227 Views - Last Post: 25 October 2010 - 10:21 PM Rate Topic: -----

#1 Guest_hwutang*


Reputation:

SavingsAccount

Posted 25 October 2010 - 09:29 PM

THE PROGRAM:

Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a driver program to test the class SavingsAccount. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 5% and calculate the next months interest and print the new balances for each of the savers.

Write another class SpecialSavings that extends SavingsAccount to pay interest of 10% on accounts that have balances that exceed 10K. Also provided methods to deposit and take money out of savings account. Write a driver program to test the class SpecialSavings. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Make a few deposits and withdrawals and show balance and interest earned for each account.

Some Tips for helping you with Lab 4
For Lab 4 part 1 -
Pl. follow the instructions in the assignment and write the class definition. A driver program is the code you would write in main() to exercise the code of class definition.

You should declare private variable in your class definition. You should also write protected methods to set and get the value of private variables.

For Lab 4 part 2 -
You need to write deposit and withdrawal methods. Should you put this in parent class or child class? Try to answer this question so that you have most reusability in your class definition.

In this part - you have to learn how polymorphism works.
You will have accounts whose balance might be above or below 10K. By using methods in both classes try to change the interest earned to 10% if balance is above 10K or 4% if the interest is lower.

THE PROBLEM:

Because of certain requirements, the savingsBalance variable of the SavingsAccount class must be private but it cannot be static. Thus, we are having trouble with trying to call the variable savingsBalance in the second class, SpecialSavings, which extends SavingsAccount...but because the variable savingsBalance is not static, the inherited method modifyInterestRate (which has to be static) can't access it. Problem is in line 42 specifically. Any way of getting around this? Thanks in advance.

-Distressed Programmer

THE CODE:

public class SavingsAccount
{
    private static double annualInterestRate;
    private double savingsBalance;

    protected SavingsAccount()
    {
        savingsBalance = 0;
        annualInterestRate = 0;
    }

    protected SavingsAccount(double balance)
    {
        savingsBalance = balance;
        annualInterestRate = 0;
    }

    protected void calculateMonthlyInterest()
    {
        System.out.println("Current savings balance: " + savingsBalance);
        double monthlyInterest;
        monthlyInterest = (savingsBalance * annualInterestRate)/12;
        savingsBalance += monthlyInterest;
        System.out.println("New savings balance: " + savingsBalance);
    }

    protected double getBalance()
    {
        return savingsBalance;
    }

    protected static void modifyInterestRate(double newInterestRate)
    {
        annualInterestRate = newInterestRate;
    }    
}

class SpecialSavings extends SavingsAccount
{
    protected static void modifyInterestRate()
    {
        if(SavingsAccount.getBalance() > 10000)
        {
            modifyInterestRate(.1);
        }
    }
}

class Driver
{
    public static void main(String[] args)
    {
        SavingsAccount saver1 = new SavingsAccount(2000);
        SavingsAccount saver2 = new SavingsAccount(3000);

        saver1.modifyInterestRate(.04);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.04);
        saver2.calculateMonthlyInterest();

        saver1.modifyInterestRate(.05);
        saver1.calculateMonthlyInterest();
        saver2.modifyInterestRate(.05);
        saver2.calculateMonthlyInterest();
    }
}


Is This A Good Question/Topic? 0

Replies To: SavingsAccount

#2 Guest_hwutang*


Reputation:

Re: SavingsAccount

Posted 25 October 2010 - 10:06 PM

Nevermind...figured it out on my own. Thanks a lot to those who actually took the time in trying to read this whole thing x_x
Was This Post Helpful? 0

#3 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: SavingsAccount

Posted 25 October 2010 - 10:09 PM

This might not be the best way to do this, but assign the savingsBalance to a static holder variable. You can have static variables in a none-static method.
 private static double annualInterestRate;
    private double savingsBalance;
   static double holder = 0;


Only added a static holdervariable.

 protected double getBalance()
    {
        return savingsBalance;
    }
    protected void setHolder(){
        holder = savingsBalance;

    }
    protected static double retHolder(){
        return holder;
    }


getBalance() still returns savings balance but you add a static method retHolder() which returns your static variable.

class SpecialSavings extends SavingsAccount
{
    protected static void modifyInterestRate()
    {

        if(SavingsAccount.retHolder() > 10000)
        {
            modifyInterestRate(.1);
        }
    }
}


Then simply change your if to reflect the change of the return method.

edit:
day late dollar short

This post has been edited by b0ng01: 25 October 2010 - 10:11 PM

Was This Post Helpful? 1
  • +
  • -

#4 Guest_hwutang*


Reputation:

Re: SavingsAccount

Posted 25 October 2010 - 10:21 PM

Thanks very much for the help though! Much appreciated. It's nice to know there are people willing to help out there :D
Was This Post Helpful? 0

Page 1 of 1