Question

In: Computer Science

A text editor window should pop up with the following source code (except with your actual name):

 

  1. A text editor window should pop up with the following source code (except with your actual name):
    1. csci1011.lab8;

      /**
      *
      * @author Your Name
      */
      public class Account {

      }
  1. Implement the Account class.
    1. Add a public enum called ACCOUNT_TYPE with two values CHECKING, SAVING
    2. Add the following private instance variables to the Account class:
      • An instance variable called aType of type Enum ACCOUNT_TYPE.
      • An instance variable called accountOwner of type String.
      • An instance variable called interestRate of type double.
      • An instance variable called balance of type double
    3. Add getters and setters for the four instance variables of item (b) to the Account class:
    4. Add the following methods to the Account class:
      • A void method called initialize that takes a parameter of type ACCOUNT_TYPE, a String, two doubles and uses those arguments to set the values of the accountType, accountOwner, interestRate, and balance instance variables.
      • A void method called display that displays the account information.
      • A void method called deposit with one parameter of double that adds the given parameter to the balance instance variable.
      • A void method called withdraw with one parameter of double that deduct the given parameter from the balance instance variable. This method prints an error message when the given parameter is greater than the balance. In this case no money should be withdrawn from the account.
  • In the main method of your main class, create two Account objects and perform the following:
    • Initialize the first account objects with SAVING as the accountType and other parameters of your own choice.
    • Initialize the first account objects with CHECKING as the accountType and other parameters of your own choice.
    • Display both accounts.
    • Deposit $200 from the first account
    • Withdraw $500 from the second account
  • Display both accounts.
  • Run the main program to see if the tests were successful.
    • Here is a sample output of the program.

Account Type: SAVING

Account Owner: Customer B

Interest Rate: 1.1

Balance: 500.0

 

Account Type: CHECKING

Account Owner: Customer A

Interest Rate: 1.2

Balance: 200.0

 

Cannot withdraw more than account balance

 

Account Type: SAVING

Account Owner: Customer B

Interest Rate: 1.1

Balance: 700.0

 

Account Type: CHECKING

Account Owner: Customer A

Interest Rate: 1.2

Balance: 200.0

 

solution provided:


package csci1011.csci1011.lab8;

import java.text.NumberFormat;
import java.text.DecimalFormat;

class Account {
enum ACCOUNT_TYPE
{
CHECKING,SAVINGS;
}
//create five variable
private ACCOUNT_TYPE aType;
private String accountOwner;
private double interestRate;
private double Balance;

//setter and getter methods
private ACCOUNT_TYPE getaType() {
return aType;
}

private void setaType(ACCOUNT_TYPE aType) {
this.aType = aType;
}

private String getAccountOwner() {
return accountOwner;
}

private void setAccountOwner(String accountOwner) {
this.accountOwner = accountOwner;
}

private double getInterestRate() {
return interestRate;
}

private void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}

private double getBalance() {
return Balance;
}

private void setBalance(double Balance) {
this.Balance = Balance;
}
void initialize(ACCOUNT_TYPE atype,String name,double interestRate,double balance)
{
setaType(atype);
setAccountOwner(name);
setInterestRate(interestRate);
setBalance(balance);
  
}
void display()
{
System.out.println(" Account Type : "+aType.name());
System.out.println("Account Owner : "+getAccountOwner());
System.out.println("Interest Rate : "+getInterestRate());
System.out.println("Balance : "+getBalance());
}
//deposit to the account
void deposit(double amount)
{

double bal=getBalance();
setBalance(bal+amount);
System.out.println("Successfull Deposit ! ");
}
//withdraw from account
void withdraw(double amount)
{
//if balance is 0 or amount is greater than balance
if(getBalance()<=0 || amount>getBalance())
{
System.out.println("You don't have enough money to withdraw");
}
else
{
double bal=getBalance();
setBalance(bal-amount);
System.out.println("Successfully withdrawn ");
}

}
public static void main(String args[])
{
//two objects
Account ac=new Account();
Account ac1=new Account();
  
//initialize with Savings
ac.initialize(ACCOUNT_TYPE.SAVINGS, "Customer B ", 1.1, 500);
//initialize with Checking
ac1.initialize(ACCOUNT_TYPE.CHECKING, "Customer A ", 1.2, 200);
//display
ac.display();
ac1.display();
//deposit into account
ac.deposit(20);
//withdraw from account
ac.withdraw(60);
//display
ac.display();
  
  
  
}
}

Solutions

Expert Solution

Two classes added.1.Account 2.Main

ACCOUNT_TYPE of Account is accessed in Main class by creating the object of Account class .

some enhancesment mades in methods for understanding display section.

Java code and screenshort of output shown below

import java.text.NumberFormat;
import java.text.DecimalFormat;

class Account {
enum ACCOUNT_TYPE
{
CHECKING,SAVINGS;
}
//create five variable
private ACCOUNT_TYPE aType;
private String accountOwner;
private double interestRate;
private double Balance;

//setter and getter methods
private ACCOUNT_TYPE getaType() {
return aType;
}

private void setaType(ACCOUNT_TYPE aType) {
this.aType = aType;
}

private String getAccountOwner() {
return accountOwner;
}

private void setAccountOwner(String accountOwner) {
this.accountOwner = accountOwner;
}

private double getInterestRate() {
return interestRate;
}

private void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}

private double getBalance() {
return Balance;
}

private void setBalance(double Balance) {
this.Balance = Balance;
}
void initialize(ACCOUNT_TYPE atype,String name,double interestRate,double balance)
{
setaType(atype);
setAccountOwner(name);
setInterestRate(interestRate);
setBalance(balance);
  
}
void display()
{
System.out.println(" Account Type : "+aType.name());
System.out.println("Account Owner : "+getAccountOwner());
System.out.println("Interest Rate : "+getInterestRate());
System.out.println("Balance : "+getBalance());
}
//deposit to the account
void deposit(double amount)
{

double bal=getBalance();
setBalance(bal+amount);
System.out.println("$"+amount+" Successfull Deposit ! ");
}
//withdraw from account
void withdraw(double amount)
{
//if balance is 0 or amount is greater than balance
if(getBalance()<=0 || amount>getBalance())
{
System.out.println("You don't have enough money to withdraw");
}
else
{
double bal=getBalance();
setBalance(bal-amount);
System.out.println("Successfully withdrawn $"+amount);
}
}
}
class Main
{
public static void main(String args[])
{
//two objects
Account ac=new Account();
Account ac1=new Account();
//acess ACCOUNT_TYPE through object of class in another class
Account.ACCOUNT_TYPE type1=Account.ACCOUNT_TYPE.SAVINGS;
//initialize with Savings
ac.initialize(type1, "Customer A ", 1.1, 500);
Account.ACCOUNT_TYPE type2=Account.ACCOUNT_TYPE.CHECKING;
//initialize with Checking
ac1.initialize(type2, "Customer B ", 1.2, 200);
//display
System.out.println("First Account is:");
ac.display();
System.out.println("Second Account is:");
ac1.display();
//deposit into account
ac.deposit(200);
//withdraw from account
ac1.withdraw(500);
//display
System.out.println("First Account is:");
ac.display();
System.out.println("Second Account is:");
ac1.display();
  
  
}
}

 


Related Solutions

Sub HW4_1_1() 'a. Using the properties window in the VB editor assign ' the code name...
Sub HW4_1_1() 'a. Using the properties window in the VB editor assign ' the code name wsEx2 to the sheet with tab name Sheet1. 'b. Using VBA code and the code name for worksheet Sheet1, ' make this worksheet the active sheet. 'c. Declare a range variable and assign to this variable ' the range from A2 to A10 in the worksheet Sheet1. 'd. Write VBA code to create a range name and call it “Scores”. ' This range name...
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the...
Name your source code file sh.c Write your own simple shell. Your shell should prompt the user for a command, run it, then prompt the user for their next command. If the user types "exit", then the shell should terminate. The shell should ignore Ctrl-C. Inside the attached parse.h header file, there is a parse function that you can use to split up the command line into separate strings. Recall that execvp accepts two arguments: the name of the command,...
you can use either vim or nano as text editor Implement the following code in ARM...
you can use either vim or nano as text editor Implement the following code in ARM on Raspberry Pi, compile and run. g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note: answer should be calculated not hardcoded
carter inc is evaluating a security giving the information in the pop-up window probability return .15...
carter inc is evaluating a security giving the information in the pop-up window probability return .15 6% .50 8% .20 12% .15. 14% calculate the investment expected return and it's standard deviation The investment expected rate of return is The investment standard deviation is
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT