Best and complete programs covering all 4 parts will receive 3000 points as i ha
ID: 3568367 • Letter: B
Question
Best and complete programs covering all 4 parts will receive 3000 points as i have asked the question twice here is the link to second posting
http://www.chegg.com/homework-help/questions-and-answers/best-complete-programs-covering-4-parts-receive-3000-points-asked-question-twice-link-seco-q6232093
A Bank Account Class
Part I Method
1. File Account.java contains a partial definition for a class representing a bank account. Save it to your directory and study it to see what methods it contains. Then complete the Account class as described below. Note that you won't be able to test your methods until you write ManageAccounts in question #2.
a. Fill in the code for method toString, which should return a string containing the name, account number, and balance for the account.
b. Fill in the code for method chargeFee, which should deduct a service fee from the account.
c. Modify chargeFee so that instead of returning void, it returns the new balance. Note that you will have to make changes in two places.
d. Fill in the code for method changeName which takes a string as a parameter and changes the name on the account to be that string.
2. File ManageAccounts.java contains a shell program that uses the Account class above. Save it to your directory, and complete it as indicated by the comments.
3. Modify ManageAccounts so that it prints the balance after the calls to chargeFees. Instead of using the getBalance method like you did after the deposit and withdrawal, use the balance that is returned from the chargeFees method. You can either store it in a variable and then print the value of the variable, or embed the method call in a println statement.
//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary 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;
}
//----------------------------------------------
// 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()
{
}
//----------------------------------------------
// Deducts $10 service fee
//----------------------------------------------
public void chargeFee()
{
}
//----------------------------------------------
// Changes the name on the account
//----------------------------------------------
public void changeName(String newName)
{
}
}
// ****************************************************************
// ManageAccounts.java
//
// Use Account class to create and manage Sally and Joe's
// bank accounts
// ****************************************************************
public class ManageAccounts
{
public static void main(String[] args)
{
Account acct1, acct2;
//create account1 for Sally with $1000
acct1 = new Account(1000, "Sally", 1111);
//create account2 for Joe with $500
//deposit $100 to Joe's account
//print Joe's new balance (use getBalance())
//withdraw $50 from Sally's account
//print Sally's new balance (use getBalance())
//charge fees to both accounts
//change the name on Joe's account to Joseph
//print summary for both accounts (use toString() implicitly)
}
}
Part II Static Variable and Static Method
Suppose the bank wants to keep track of how many accounts exist.
Part III Method Overloading
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.
Part IV
Suppose the bank wants to offer the service of consolidating two accounts (meaning two accounts belong to the same person)
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.
error in testconsolidation.java please help
----jGRASP exec: javac -g TestConsolidation.java
TestConsolidation.java:3: error: cannot access Account
import account.Account;
^
bad class file: .ccountAccount.class
class file contains wrong class: Account
Please remove or make sure it appears in the correct subdirectory of the classpath.
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Explanation / Answer
Here you go :)
Please comment if you have any doubts
//I've posted the solutions for the first two parts in the other link.
//Account class after part 3
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++;
}
public Account (double balance, String name)
{
this.balance=balance;
this.name=name;
acctNum=(int)Math.random()*100;
}
public Account (String name)
{
this.name=name;
balance=0;
acctNum=(int)Math.random()*100;
}
public double withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
return balance;
}
public double withdraw(double amount,double fee)
{
if (balance >= amount+fee)
balance -= amount+fee;
else
System.out.println("Insufficient funds");
return balance;
}
public void deposit(double amount)
{
balance += amount;
}
public double getBalance()
{
return balance;
}
public String toString()
{
return "Name: "+name+" Balance: "+balance+" Acc.number: "+acctNum;
}
public double chargeFee()
{
return withdraw(10);
}
public void changeName(String newName)
{
name=newName;
}
public static int getNumAccounts()
{
return numAccounts;
}
}
//Account class after Part 4
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++;
}
public Account (double balance, String name)
{
this.balance=balance;
this.name=name;
acctNum=(int)(Math.random()*100);
}
public Account (String name)
{
this.name=name;
balance=0;
acctNum=(int)(Math.random()*100);
}
public double withdraw(double amount)
{
if (balance >= amount)
balance -= amount;
else
System.out.println("Insufficient funds");
return balance;
}
public double withdraw(double amount,double fee)
{
if (balance >= amount+fee)
balance -= amount+fee;
else
System.out.println("Insufficient funds");
return balance;
}
public void deposit(double amount)
{
balance += amount;
}
public double getBalance()
{
return balance;
}
public String toString()
{
return "Name: "+name+" Balance: "+balance+" Acc.number: "+acctNum;
}
public double chargeFee()
{
return withdraw(10);
}
public void changeName(String newName)
{
name=newName;
}
public static int getNumAccounts()
{
return numAccounts;
}
public void close()
{
this.name+="CLOSED";
this.balance=0;
numAccounts--;
}
public static Account consolidate(Account acct1, Account acct2)
{
Account a=null;
if(acct1.name.equals(acct2.name))
{
if(acct1.acctNum!=acct2.acctNum)
{
a=new Account(acct1.balance+acct2.balance,acct2.name+"_"+acct2.name,(int)(Math.random()*100));
}else System.out.println("Both Accounts should have different account numbers merge!");
}
else System.out.println("Both Accounts should have same name to merge!");
return a;
}
}
//TestConsolidation class
import java.util.Scanner;
import account.Account;
public class TestConsolidation
{
public static void main(String[] args)
{
String name;
double balance;
long acctNum;
Account acct[]=new Account[3];
Scanner scan = new Scanner(System.in);
for(int i=0;i<3;i++)
{
System.out.println("Enter account holder's "+(i+1)+" name: ");
name = scan.next();
acct[i] = new Account(100,name);
}
System.out.println("-----------------------------------------");
System.out.println("Details of the 3 accounts: ");
for(int i=0;i<3;i++)
{
System.out.println(acct[i].toString());
}
acct[0].close();
System.out.println(Account.consolidate(acct[1], acct[2]));
System.out.println(acct[0].toString());
System.out.println(acct[1].toString());
System.out.println(acct[2].toString());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.