Thread: Help Me with this homework problem Please!!

  1. #1
    Unregistered
    Guest

    Help Me with this homework problem Please!!

    //The program says to Create a Savings Class. Use a static data member to contain the
    //annual interestRat for each of the savers. Each member o the class contains a privcate data member savingsbalance indincating the amount the saver currently has on deposit.
    //Provide data member SavingsBalance indicating he amount the saver currently has on deposit.
    //Provide a calculateMonthlyInterest member function that calculates the monthly interest
    //by multiplying the balance by annual InterestRate divide by 12
    //This interest should be added to savings Balance.
    //Provide a static member function modifyInterestRate that sets the satatic annual
    //interestRate to a new value.
    //Write a driver Program to test class with balance of $200000 and 300000 respectively
    //set annualInterestRate to 3%
    //then calculate the monthly interest and print new balances for each savers
    //Then set teh annualInterest Rate to 4% and calculate the next months interest and print the new balances
    //for each of the savers

    class SavingsAccount {

    public:
    SavingsAccount();

    void calculateMonthlyInterest();
    //static void modifyInterestrate(int);
    void printbalance();

    private:
    static double savingsBalance;
    double balance;

    //int y;

    //static data member
    static double annualInterestRate;
    };

    double SavingsAccount::annualInterestRate = .3;
    double SavingsAccount::savingsBalance = 20000;
    //int SavingsAccount::modifyInterestrate = 4;


    SavingsAccount::SavingsAccount()
    {
    savingsBalance = 0;
    }


    void SavingsAccount::calculateMonthlyInterest()
    {
    savingsBalance = savingsBalance * annualInterestRate/12.;


    }

    //void SavingsAccount::modifyInterestRate()
    //{

    //}

    void SavingsAccount:rintbalance()
    {
    cout<<"You balance is "<<savingsBalance;
    }
    int main()
    {

    SavingsAccount saver1;

    //SavingsAccount saver2;
    //cout<<" The current deposit for saver 1 is";

    saver1.calculateMonthlyInterest;
    saver1.printbalance;

    return 0;

    }


    //Please help me...

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Well, not sure what the problem is (perhaps you could be more specific?) but if the problem is that you are getting the way wrong answer, try changing

    Code:
    void SavingsAccount::calculateMonthlyInterest() 
    { 
         savingsBalance = savingsBalance * annualInterestRate/12.; 
    
    }
    to

    Code:
    void SavingsAccount::calculateMonthlyInterest() 
    {
        savingsBalance += savingsBalance * annualInterestRate / 12.;
    }
    otherwise you are replacing the account balance with the interest instead of computing the interest and adding it. If this is not your problem please elaborate, and next time use code tags.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  3. #3
    Unregistered
    Guest
    //this is a problem in my text book..
    //I just need to know about how to get started on it...
    //Its not outputting anything

    //The problem says to Create a Savings Class. Use a static data member to contain the
    //annual interestRat for each of the savers. Each member o the class contains a private data member savingsbalance indincating the amount the saver currently has on deposit.
    //Provide data member SavingsBalance indicating he amount the saver currently has on deposit.
    //Provide a calculateMonthlyInterest member function that calculates the monthly interest
    //by multiplying the balance by annual InterestRate divide by 12
    //This interest should be added to savings Balance.
    //Provide a static member function modifyInterestRate that sets the satatic annual
    //interestRate to a new value.
    //Write a driver Program to test class with balance of $200000 and 300000 respectively
    //set annualInterestRate to 3%
    //then calculate the monthly interest and print new balances for each savers
    //Then set teh annualInterest Rate to 4% and calculate the next months interest and print the new balances
    //for each of the savers

  4. #4
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    It looks like you have a good start. If you keep going, you'll probably get to where you want to go.

    Do you have a more specific question than "Help me?" People are glad to help, but you need to ask specific questions. Also, there's no need to put double slashes // in // front // of //every //line.
    "Logic is the art of going wrong with confidence."
    Morris Kline

  5. #5
    Unregistered
    Guest

    OK thanx

    Well for one the program wont run...
    copy and paste for me and run...

  6. #6
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    OK, lets look at you SavingsAccount class.
    Use a static data member to contain the
    //annual interestRat for each of the savers. Each member o the class contains a privcate data member savingsbalance indincating the amount the saver currently has on deposit.
    A static variable is a variable that is accessible by all instances of a class. That means that every SavingsAccount that you create will share the static variables and if any of the SavingsAccount classes changes a static variable, then that variable changes in every SavingsAccount. In your problem, you want every SavingsAccount to have the same annualInterestRate, so you declared annualInterestRate as static. However, not every savings account has the same balance. So savingsBalance shouldn't be a static variable.
    //Provide a static member function modifyInterestRate that sets the satatic annual
    //interestRate to a new value
    You've declared static double annualInterestRate as a private variable, which is good. As a general rule, class variables should be private and you should provide functions that allow the user of a class to modify the variable. In this case, you want to create a static function called SavingsAccount::modifyInterestRate that takes an interest rate as an input. All the function will do is set annualInterestRate to the new interest rate. Pretty simple.
    //Provide a calculateMonthlyInterest member function that calculates the monthly interest
    //by multiplying the balance by annual InterestRate divide by 12
    //This interest should be added to savings Balance.
    Looks like you've got it. Just make the change suggested by geophyzzer.
    //Provide data member SavingsBalance indicating he amount the saver currently has on deposit.
    I assume that this is referring to a function, not a variable. Looks like you've got it with your function SavingsAccount::PrintBalance()

    You need to be able to make deposits to your savings account. So you need to create a class function to add a deposited amount to your savingsBalance.

    This should move you on your way.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One More Homework Problem
    By joxerjen in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 04:39 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM