Java 102 Classes

User Generated

abgncvengr

Programming

Description

You can post solutions at the end of the document in the same file unless exclusively requested, no plagiarism or duplication is permitted, comments are not required for programs unless exclusively asked for.

Unformatted Attachment Preview

[IFT 102] Introduction to Java Technologies Lab 6 Score: 50 pts (10 + 40) I. Account Class File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then modify it as follows: 1. Overload the constructor as follows: ▪ ▪ ▪ public Account (double initBal, String owner, long number) - initializes the balance, owner, and account number as specified public Account (double initBal, String owner) - initializes the balance and owner as specified; randomly generates the account number. public Account (String owner) - initializes the owner as specified; sets the initial balance to 0 and randomly generates the account number. 2. Overload the withdraw method with one that also takes a fee and deducts that fee from the account. File TestAccount.java contains a simple program that exercises these methods. Save it to your directory, study it to see what it does, and use it to test your modified Account class. //************************************************************ // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, and get a String representation // of the account. //************************************************************ public class Account { private double balance; private String name; private long acctNum; //------------------------------------------------//Constructor -- initializes balance, owner, and account number //------------------------------------------------public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; } [IFT 102] Introduction to Java Technologies //------------------------------------------------// Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. //------------------------------------------------public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds"); } //------------------------------------------------// Adds deposit amount to balance. //------------------------------------------------public void deposit(double amount) { balance += amount; } //------------------------------------------------// Returns balance. //------------------------------------------------public double getBalance() { return balance; } //------------------------------------------------// Returns a string containing the name, account number, and balance. //------------------------------------------------public String toString() { return "Name:" + name + "\nAccount Number: " + acctNum + "\nBalance: " + balance; } } //************************************************************ // TestAccount.java // // A simple driver to test the overloaded methods of // the Account class. //************************************************************ import java.util.Scanner; public class TestAccount { public static void main(String[] args) { String name; double balance; long acctNum; [IFT 102] Introduction to Java Technologies Account acct; Scanner scan = new Scanner(System.in); System.out.println("Enter account holder's first name"); name = scan.next(); acct = new Account(name); System.out.println("Account for " + name + ":"); System.out.println(acct); System.out.println("\nEnter initial balance"); balance = scan.nextDouble(); acct = new Account(balance,name); System.out.println("Account for " + name + ":"); System.out.println(acct); System.out.println("\nEnter account number"); acctNum = scan.nextLong(); acct = new Account(balance,name,acctNum); System.out.println("Account for " + name + ":"); System.out.println(acct); System.out.print("\nDepositing 100 into account, balance is now "); acct.deposit(100); System.out.println(acct.getBalance()); System.out.print("\nWithdrawing $25, balance is now "); acct.withdraw(25); System.out.println(acct.getBalance()); System.out.print("\nWithdrawing $25 with $2 fee, balance is now "); acct.withdraw(25,2); System.out.println(acct.getBalance()); System.out.println("\nBye!"); } } II. Opening and Closing Accounts File Account.java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then write the following additional code: 1. Suppose the bank wants to keep track of how many accounts exist. a. Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will be initialized (to 0, since it’s an int) automatically. b. Add code to the constructor to increment this variable every time an account is created. c. Add a static method getNumAccounts that returns the total number of accounts. Think about why this method should be static - its information is not related to any particular account. d. File TestAccounts1.java contains a simple program that creates the specified number of bank accounts then uses the getNumAccounts method to find how many accounts were created. Save it to your directory, then use it to test your modified Account class. [IFT 102] Introduction to Java Technologies 2. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Also decrement the total number of accounts. 3. Add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned. Two important rules of consolidation: ▪ Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number. ▪ Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your money! Check these conditions before creating the new account. If either condition fails, do not create the new account or close the old ones; print a useful message and return null. 4. Write a test program that prompts for and reads in three names and creates an account with an initial balance of $ 100 for each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account. Now print the accounts again, including the consolidated one if it was created. //************************************************************ // TestAccounts1 // A simple program to test the numAccts method of the // Account class. //************************************************************ import java.util.Scanner; public class TestAccounts1 { public static void main(String[] args) { Account testAcct; Scanner scan = new Scanner(System.in); System.out.println("How many accounts would you like to create?"); int num = scan.nextInt(); for (int i=1; i
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

I did this.Please review at least some my work because if you'd correct all 6 simultaneously, it may be a tragedy:)

Class Number:
Lab Activity Number: 6
Date:
Instructor’s name:

I. Account Class.
//************************************************************
// Account.java
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//************************************************************
public class Account
{
private double balance;
private String name;
private long acctNum;
//------------------------------------------------//Constructor -- initializes balance, owner, and account number
//------------------------------------------------public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++; // ADDED
}
// ADDED start
static final long MAX_ACCOUNT_NUM = 10000;
// initializes the balance and owner as specified;
// randomly generates the account number.
public Account(double initBal, String owner)
{
balance = initBal;
name = owner;
acctNum = (long)(Math.random()*MAX_ACCOUNT_NUM);
numAccounts++;
}
private static i...


Anonymous
Super useful! Studypool never disappoints.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags