Question
Using Java: Please correct the code below //Java How to Program (early objects) (10th Edition) Exercise 3.15 Removing Duplicated Code in Method Main import java.util.scanner;
Using Java: Please correct the code below
//Java How to Program (early objects) (10th Edition) Exercise 3.15 Removing Duplicated Code in Method Main
import java.util.scanner;
class account
{
private String name;
private double balance;
public Account(String name, double balance)
{
this.name=name;
if(balance>0.0)
this.balance=balance;
}
public void deposit(double depositAmount)
{
if(depositAmount>0.0)
balance=balance+depositAmount;
}
public void withdrawl(double withdrawlAmount)
{
if(balance>withdrawlAmount)
{
balance=balance-withdrawlAmount;
}
else
System.out.println("Withdrawl amount exceeded account balance");
}
public double getBalance()
{
return balance;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
} //ends class account
class AccountTest
{
public static void main(String[]args)
{
Account account1 = new Account("Emily Smith", 50.00);
Account account2 = new Account("John Doe", -7.53);
// Replace print statement with displayAccount method with account1 as an argument
// Calling displayAccount static method with Account Objects account1 and account2
displayAccount(account1);
displayAccount(account2);
Scanner input=new Scanner(System.in); //Creates a scanner to obtain input from the command window
double depositAmount=input.nextDouble();
System.out.printf("%nadding%.2f to account1 balance%n%n", depositAmount);
account1.deposit(depositAmount); //Adds to account1's balance
//Replace the print statement with displayAccount method with account1 as argument.//
//Calling displayAccount static method with Account Objects account2 and account 2.//
displayAccount(account1);
displayAccount(account2);
System.out.print("Enter deposit amount for account2: ");
depositAmount = input.nextDouble();
System.out.printf("%nadding %.2f to account2 balance%n%n", depositAmount);
account2.deposit(depositAmount); //add to account2 balance
//Replace the print statement with displayAccount method with account1 as an argument
//Calling displayAccount static method with Account Objects account1 and account2
displayAccount(account1);
displayAccount(account2);
} //End main
//The static method displayAccount accepts the account objects as its input parameter and calls the method getName and getBalance and print their values.
public static void displayAccount(Account accountToDisplay)
{
System.out.printf("%s balance: $%.2f%n",accountToDisplay.getName(),accountToDisplay.getBalance());
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started