Google Answers Logo
View Question
 
Q: Simple Java Program ( No Answer,   6 Comments )
Question  
Subject: Simple Java Program
Category: Computers > Programming
Asked by: rejct-ga
List Price: $10.00
Posted: 17 Oct 2002 18:07 PDT
Expires: 22 Oct 2002 16:01 PDT
Question ID: 78026
Description: 

In this homework, you will essentially write a program that runs a
simple bank. Three classes will be used in this homework. You have to
implement these three classes. The names of these classes are
BankAccount, Bank, and HW03Bank. These classes should contain the
following instance methods. Make sure that all of your instance
variables are declared as private.


BankAccount class:

A BankAccount object keeps information about one particular bank
account at a bank. BankAccount class should contain following instance
variables (BankAccount class may contain static variables too).

	private int accountNum;  	// each bank account should have a unique
account number
  	private String customerName;	// the owner of the account
  	private double balance;		// the current balance of the account
 	private double[] transactions;	// all the transactions (deposits and
withdraws) on this account
	private int numOfTransactions;	// the number of the transactions on
the account

You have to include the following constructor and  instance methods in
your BankAccount class:

// Constructor: Initializes the new BankAccount object. customerName
and balance are initialized
// from the given values.You have to generate a unique account number
for the new
// BankAccount object.The opening balance will be the first
transaction (deposit) on this account.
public BankAccount(String cname, double openingBalance)

// Returns the account number of the BankAccount object the method is
called on.
public int getAccountNum()

// Returns a string containing the account number, the customer name
and the balance of
// the current bank account (the BankAccount object the method is
called on).
public String getAccountInfo()

// Returns a string containing the last N transactions of  the current
bank account
public String getTransactionInfo(int lastNTransactions)

// Withdraws the given amount from the current account. It should also
register this withdraw
// as a transaction. If the balance is not enough, it should print a
message.
public void withdraw(double amount)

// Deposits the given amount to the current account. It should also
register this deposit
// as a transaction. 
public void deposit(double amount)

Bank class:

A Bank object keeps information about the bank accounts at a bank.
Bank class should contain following instance variables (Bank class may
contain static variables too).

	private BankAccount[] accounts; 	// all the bank accounts at this
bank
	private int numOfAccounts;		// the number of bank accounts at this
bank

You have to include the following constructor and instance methods in
your Bank class:

// Constructor: Initializes the new Bank object. This new Bank object
initially doesn’t contain
// any account.
public Bank()

// Opens a new bank account and returns the account number of this new
account.
// Adds this account into the account list of the bank (the Bank
object the method is called on).
// It takes the customer name and the opening balance as parameters
public int openNewAccount(String customerName, double openingBalance)

// Withdraws the given amount from the account whose account number is
given.
// If the account is not available at the bank, it should print a
message.
public void withdrawFrom(int accountNum, double amount)

// Deposits the given amount to the account whose account number is
given.
// If the account is not available at the bank, it should print a
message.
public void depositTo(int accountNum, double amount)

// Prints the account number, the customer number and the balance of
the bank account whose
// account number is given.
// If the account is not available at the bank, it should print a
message.
public void printAccountInfo(int accountNum)

// Prints the account number, the customer number and the balance of
the bank account whose
// account number is given (together with last N transactions on that
account).
// If the account is not available at the bank, it should print a
message.
public void printAccountInfo(int accountNum, int lastNTransactions)





HW03Bank class:

You will create a console application that will "run" a bank. Your
HW03Bank class should contain only a main method. In this main method,
you will create a Bank object first, and you will perform certain
menu-driven operations using this Bank object. The main method of your
HW03Bank class should do the followings:

•	Create a new Bank object. You will use this Bank object for the
operations.

•	Prompt the user with the following menu choices:
1) open a new bank account
2) deposit to a bank account
3) withdraw from a bank account
4) print short account information
5) print the detailed account information including last transactions
6) quit

•	Execute the choice the user chooses and then continue till the user
quits.

•	What each menu item choice should do:
1.	Ask the user for the customer name and the opening balance of a new
bank account, and create this new bank account from that information
at the bank. Print the account number of this new account.
2.	Ask the user for the account number of an account, and a deposit
amount. Deposit the given amount to the bank account whose account
number is given.
3.	Ask the user for the account number of an account, and a withdraw
amount. Withdraw the given amount from the bank account whose account
number is given.
4.	Ask the user for the account number of an account, and print the
short information (account number, customer name, and balance)  about
this account.
5.	Ask the user for the account number of an account, and the number
of last N transactions. Print the detailed information (account
number, customer name, balance, and the last N transactions) about
this account.
6.	Quit.


This is what i have so far
import java.io.*;

public class HW03Bank { 
  
  
  public static void main (String[] args) throws IOException {
  
    // declare variables
    ...
        
    // create the BufferedReader object for input
    BufferedReader stdin = 
          new BufferedReader(new InputStreamReader(System.in));
    
    // create a bank
    abank = new Bank();

    // Give the menu to user until he/she quits
    ...
  } 
} 



//
// BankAccount Class
//
class BankAccount {

  // other static variables
	...
	
  // Fields
	private int accountNum;
  private String customerName;
  private double balance;
  private double[] transactions;
	private int numOfTransactions;
  
  
  // Constructors
  public BankAccount(String cname, double openingBalance) {
	  ...
  }
	
  // getAccountNum
  public int getAccountNum() {
	  ...
  }
	
	// getAccountInfo
	public String getAccountInfo() {
    ...
	}
  
  // getTransactionInfo
  public String getTransactionInfo(int lastNTransactions) {
	  ...
  }
  
	// withdraw
  public void withdraw(double amount) {
		...
  }
  	
	
  // deposit
  public void deposit(double amount) {
		...
  }
  
  
} // End of BankAccount Class



class Bank {
	//Fields
	private BankAccount[] accounts;
	private int numOfAccounts;
	// other static constants
	...
	
	// Constructor
	public Bank() {
		...
	}
	
	public int openNewAccount(String cname, double openingBalance) {
		...
	}
	
	public void withdrawFrom(int accountNum, double amount) {
		...		
	}
	
	public void depositTo(int accountNum, double amount) {
		...	
	}
	
	public void printAccountInfo(int accountNum) {
		...	
	}
	
	public void printAccountInfo(int accountNum, int numOfTransactions) {
		...	
	}
	
	
}

Request for Question Clarification by rbnn-ga on 18 Oct 2002 08:13 PDT
BankAccount has a private double[] transactions variable to keep track
of the last N transactions, but there is no limit on N that I see.

Thus, if we create the array as double[] of, say length M, after M+1
transactions we run out of space in the array.

You are better off in cases where you want to store a possibly
unbounded information using an ArrayList, something like:

import java.util.*;
private ArrayList transactionsSoFar=new ArrayList();

To add a double to the ArrayList, do:

transactionsSoFar.add(new Double(d));

To get back a double from an integer, try:

((Double)transactionsSoFar.get(i)).doubleValue() ;

Is it OK to use an ArrayList instead of a double[] for the list of
past transactions?


In general, do we have to use the private instance variables stated?
Usually a specification does not specify private instance variables; a
private instance variable is fairly invisible to everyone but the
implementor - it is a little like specifying local variable names in a
method - a bit unusual .
Answer  
There is no answer at this time.

Comments  
Subject: Re: Simple Java Program
From: secret901-ga on 17 Oct 2002 18:24 PDT
 
While Google does not prohibit asking homework questions, and some
researcher might be willing to do your homework for you, but please
note that all answers here are properties of Google Answers and can
not be copied verbatim without violating Google's copyright.
Subject: Re: Simple Java Program
From: rbnn-ga on 18 Oct 2002 00:14 PDT
 
This seems like a simple program, I agree with you, but there are
still a lot of special cases and there is a fair amount of time that
has to be spent understanding the directions.

Also, you might want to specify if you need javadoc comments here.
Based on the guidelines here:
https://answers.google.com/answers/pricing.html I suspect that you
might have better luck repricing to $40.00 or so.

(To give you an idea how much time it's likely to take, I personally
have won various timed programming contests using Java, e.g.
http://otn.oracle.com/topcoder/content.html , in which speed of
programming is paramount, and I can tell you I would be hard-pressed
to solve your problem in under an hour; and with the issues involved
in trying to upload multiple files and clarify the questions (e.g.
javadoc needed? thread-safety needed?) probably longer than that. )
Subject: Re: Simple Java Program
From: rbnn-ga on 18 Oct 2002 00:23 PDT
 
secret901-ga: Can you cite the source for your belief that google
answers cannot be copied verbatim for a non-public, non-commercial use
such as homework without violating the google copyright?

I do not see anything in to that effect in the terms of service
https://answers.google.com/answers/termsofservice.html and, indeed, it
would surprise me your statement were correct. On the other hand I am
not a lawyer.
Subject: Re: Simple Java Program
From: mvguy-ga on 18 Oct 2002 06:34 PDT
 
Rbnn -- It doesn't have anything to do with Google's terms of service;
it has to do with copyright law. (I'm speaking about U.S. law here; I
can't speak to the law of other countries.)  Basically, anything
anybody writes is automatically copyrighted, and you can't use it
without permission.  The main exception is whether copying or using
something someone else wrote is "fair use."  Short quotations with
attribution are usually fair use, and there are even situations where
you can copy an entire work and have it considered "fair use."  But
taking an entire work and submitting it under your own name as
homework?  I'd say not.
http://www.templetons.com/brad/copyright.html
http://www.templetons.com/brad/copymyths.html
Subject: Re: Simple Java Program
From: haversian-ga on 18 Oct 2002 06:49 PDT
 
You could argue that (s)he's buying non-exclusive rights to the
material the Researcher posts.  Except that it's looking like nobody's
interested for $7.50.  As I remember my intro programming classes, the
assignments took about 5 hours to do - if this kid's got a job, that's
at least $50 worth of time.  rejct, I doubt anyone will answer your
question for less than that.
Subject: Re: Simple Java Program
From: mvguy-ga on 18 Oct 2002 11:45 PDT
 
Yeah, that's a good argument about the right to nonexclusive use the
person who "commissioned" the research might have.  I see no problem
with that -- if the student puts "programming by Google Answers" on
the assignment when turning it in. I'd like to see how the prof would
respond to that!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy