Java code in refference to the book Java HOW TO PROGRAM 10th edition Late Object
ID: 3773730 • Letter: J
Question
Java code in refference to the book Java HOW TO PROGRAM 10th edition Late Objects version
7.10
(Modified Account Class) Modify class Account (Fig. 7.8) to provide a method called withdraw that withdraws money from an Account. Ensure that the withdrawal amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the method should print a message indicating "Withdrawal amount exceeded account balance." Modify class AccountTest (Fig. 7.9) to test method withdraw.
Explanation / Answer
public class Account
{
private double balance;
public Account(double balance) // Constructor
{
if(balance >0.0)
this.balance =balance;
}
public void withdraw(double amount)
{
if(amount>getBalance())
{
System.out.println(“withdrawal amount exceeded account balance”);
balance = balance;
}
else
{
balance = balance – amount;
}
} // end of the withdraw method
public double getBalance()
{
return balance;
} // end of getBalance method
} // end of class Account
import java.uti.Scanner;
public class AccountTest
{
public static void main(String [] args)
{
Account a1= new Account(150.50);
Scanner s= new Scanner(System.in);
System.out.println(“enter the amount you want to withdraw”);
amount= s.nextDouble();
a1.withdraw(amount);
System.out.println(“The balance after transaction is: %.2f%”, a1.getBalance());
} // end of main function
} // end of test class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.