CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 1999
    Location
    AZ, USA
    Posts
    11

    Somewhat confused, objects and methods

    Writing a program using multiple source files.

    Problem: Assume you have a stack of bills and are sitting down to write monthly checks. You don't know beforehand how many bills you can pay with your limited funds. but you do know you can't pay them all. Your bills are arranged in order of the payment due date. You simply pay bills until you money runs out.

    Analysis: You will pay each bill in order and stop writing checks when you run out of money. In other words, you should continue writing checks as long as your account balance is positive(greater than zero). The state at which to stop is when the "account balance is equal to zero or there is a negative balance." The problem inputs are you intial account balance and the amount of each bill. Since the bills are already in order. you do not need to worry about the due date. The problem outputs are the amount paid on each bill and the final account balance.

    Data requirements:
    Problem Inputs:
    initial account balance
    each bill amount

    Problem Output:
    amount paid on each bill
    final account balance

    Revelant formulas:
    account balance = account balance - amount paid

    Design: There are really two class in this problem. One is the class Bill and the other is the class Chekcing Account. Class checking account has a data field for the account balance and methods to read the inital balance, pay all bills, and update account balance.

    Specifications for Checking Account class

    Data fields: double balance - account balance

    Methods:
    readBalance - reads the intial account balance
    payAllbills - pays as many bills as possible
    displayBalance - displays the account balance

    Relevant formulas: balance = balance - amount paid

    Specification for Bill class

    Data fields: doulbe amount - amount of current bill

    Mehtods:
    readBill- reads the bill

    payBill pays the bill in full or part and updates the balance.

    Algorithm for payBill class (class Checking Account)

    1. Create a Bill object
    2. While the account balance is positive
    3. Get the next bill.
    4. Pay the next bill and update account balance.

    Algorithm for payBill(class Bill)

    1. if the account balance >= bill amount
    2. Pay the full amount of the bill and deduct it from the account balance.
    3. Else
    4. Pay the amount of the account balance and set balance to zero.

    Algorithm for main (class BillPayer)

    1. Read the intial account balance.
    2. Pay all bills showing the amount paid for each bill.
    3. Display the final balance.

    Sample run

    Starting balance $
    30
    Pay bill in full. new balance si $70.0
    Bill amount $
    20
    Pay bill in full. new balance is $50.0
    Bill amount $
    3
    Pay bill in full. new balance is $20.0

    Bill amount $
    Pay bill in full. new balance is $0.0
    Balance is $0.0





  2. #2
    Join Date
    Jun 1999
    Location
    Atlanta, GA
    Posts
    57

    Re: Somewhat confused, objects and methods

    You have to build some kind of intellegence to your application rather than a direct or blind deductions.
    Means, capture all the inputs and cache them some where ( in memory or secondary storage ) and your program should simulate the transactions and analyze ( you are building some intellegence here to your application ), what are all the bills I can pay with my existing resources before the actual deductions.

    Hope this gave some light on what you want to do.

    Meher

  3. #3
    Join Date
    Sep 1999
    Location
    AZ, USA
    Posts
    11

    Re: Somewhat confused, objects and methods

    Not really but thanks. The deal is, I have three files that each do a different function. Checking Account handles the balance, Bill pays the bills and Billpayer does somehting else. I'm having some error problems with these files.
    Class or declaration expected and else without if messages.
    //************************************************


    //*********************
    //Reads the initial balance.
    //Pays all the bills showing the amount paid for each bill.
    //Displays the final balance.
    //*********************************************************************

    import java.io.*;

    class Bill{

    public void paybill(){

    if (accountbalance >= billamount);{
    accountbalance = accountbalance - billamount;

    }else{
    System.out.println ("You made a partial payment of" + billamount);

    accountbalance = 0;}
    }

    } //method main

    } // class Bill






    //*********************************************************************
    //Reads the initial balance.
    //Pays all the bills showing the amount paid for each bill.
    //Displays the final balance.
    //*********************************************************************

    import java.io.*;

    public class BillPayer {

    public static void main (String[] args) throws IOExecption

    checking Account mychecks = new checking Account();

    mychecks.readBalance();

    mybill.paybill();

    mychecks.displaybalance();


    }// method main

    }//class BillPayer





    //***********************************************************************************************
    //Creates a bill object.
    //While the account balance is positive, it gets the next bill.
    //Then pays the next bill and updates the account balance.
    //***********************************************************************************************

    import java.io.*;

    public class CheckingAccount {

    double balance;

    public void readBalance() throws IOException {

    BufferedReader stdin = new BufferedReader
    (new InputStreamReader(System.in));

    System.out.println ("Starting balance $");

    balance = Double.parseDouble (stdin.readLine());
    }
    public void payAllBills() throws IOException {
    Bill nextbill = new Bill();
    }
    //while (balance > 0)

    //Bill.readbill();
    //Bill.paybill();


    //System.out.println ("Balance is zero.");


    public void displayBalance() throws IOException {

    System.out.println (balance);

    }

    } // class CheckingAccount






  4. #4
    Guest

    Re: Somewhat confused, objects and methods

    sorry I could not get the actual picture of your problem.
    The error you are talking about is a compilation error?
    If so why don't you zip your code and send it to me - [email protected]
    So that I can help you some thing - if I can.



  5. #5
    Join Date
    Aug 1999
    Location
    Australia, Melbourne
    Posts
    10

    Re: Somewhat confused, objects and methods

    Two errors with your code are seen from a glance...

    The firt is that you have included a semi colon after the if statement and before the {

    An if should read:
    if (...)
    {
    }

    not if (...);
    {
    }

    This will cause an error.

    the second problem is a call to the checking account needs to b on word not two...

    I hope this helps if you don't already know.

    Chris


  6. #6
    Join Date
    Sep 1999
    Location
    AZ, USA
    Posts
    11

    Re: Somewhat confused, objects and methods

    Thanks for all the replies. I got the problem done with the help of Teaching Assistants at ASU. Apparently this list is "off limits" for purposes of my class, so I'll have to wait till next semester to enjoy all the support.


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured