Best and complete programs covering all 4 parts will receive 3000 points as i ha
ID: 3568368 • 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-q6232091
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.
Explanation / Answer
you can either see the below code here or find it in the link that i gave below..
http://www.math.ucla.edu/~ronmiech/Pic20/Lec10pdf/Lec10.pdf
please rate me
Private Variables
Let us start with some code for a class:
public class Account
{
String name;
double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}// end Account
} // end class Account
The class we are building here will be a template for an account
at a bank. There are two "class" variables, name and balance. The
"constructor" for the class, which has the same name Account as the
class is
public Account(String n, double b)
{
name = n;
balance = b;
}// end Account
It has a parameter list Account(String n, double b) which will be used
in the creation of an instance of this class.
The code for the class is kept in a separate file Account.java.
The next program, Bank.java, will eventually be a collection of
accounts. Right now it has only one. The file Bank.java is kept in the
same directory as "Account":
public class Bank
{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
System.out.println(" balance = "+ a.balance);
System.out.println(" ");
} //end main()
}// end class Bank
In this application we are constructing an account for "Ronald",
with an initial deposit of $3000. When the application is run its'
output is:2
In reviewing this program one sees that the variable balance can
be accessed by the program Bank. As a matter of fact it can be accesed
by any number of programs. The access can be restricted by declaring
the two class variables in Account to be private:
public class Account
{
private String name;
private double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}// end Account
} // end class Account
When Bank.java is compiled (after the above change has been made
and saved to the Account.java file) one gets a "variable balance .. not
accessible" error message:
It is not accessible because it has been declared to be private.
The variable balance can only be manipulated within the class Account".
So, how does the bank find out what is in each account? This is done by
adding a toPrint() method to the class Account:
public class Account
{
private String name;
private double balance;
public Account(String n, double b)
{
name = n;3
balance = b;
}
public String toPrint()
{
String output;
output = name +", balance="+ balance;
return output;
}
}
Then, Bank is modified by a call to the a.toPrint() method, which
prints out the name and the balance :
public class Bank
{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
System.out.println(" ");
System.out.println(a.toPrint());
System.out.println(" ");
} //end main()
}// end class Bank
The output is:
Deposits, Withdrawals
Next, we add two methods to to permit the bank to make deposits
and withdrawls;
public class Account
{
private String name;
private double balance;
public Account(String n, double b)
{
name = n;
balance = b;
}
public String toPrint()
{
String output;4
output = name +", balance="+ balance;
return output;
}
public double deposit(double d)
{
if ( d >= 0) balance = balance + d;
if (d < 0)
System.out.println("Your deposit has to be >= 0");
return balance;
}
public double withdrawl(double w)
{
if (w >= 0 && w <= balance) balance = balance -w;
if (w > balance)
System.out.println("You don't have sufficient funds!");
if (w < 0)
System.out.println("Withdrawl must be >= 0");
return balance;
}
}// end Account
We modify Bank to test deposit() and withdrawl() :
public class Bank
{
public static void main(String args[])
{
Account a = new Account("Ronald", 3000);
System.out.println("Started with $3000");
System.out.println(a.toPrint());
System.out.println(" Then deposited $450");
a.deposit(450);
System.out.println(a.toPrint());
System.out.println(" Then tried to deposit -200");
a.deposit(-200);
System.out.println(" Withdrew $650");
a.withdrawl(650);
System.out.println(a.toPrint());
System.out.println(" Tried to get $4000");
a.withdrawl(4000);
System.out.println(a.toPrint());
System.out.println(" ");
} //end main()
}// end class Bank
The output is:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.