Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In Java, please help!!! 3.11. (Modified Account Class) Modify class Account (Fig. 3.8) to provide a method called with- draw that withdraws money from an
In Java, please help!!!
3.11. (Modified Account Class) Modify class Account (Fig. 3.8) to provide a method called with- draw 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. 3.9) to test method withdraw. 1 2 3 // Fig. 3.8: Account.java // Account class with a double instance variable balance and a constructor // and deposit method that perform validation. public class Account { private String name; // instance variable private double balance; // instance variable // Account constructor that receives two parameters public Account(String name, double balance) { this.name = name; // assign name to instance variable name // validate that the balance is greater than 0.0; if it's not, // instance variable balance keeps its default initial value of 0.0 if (balance > 0.0) { // if the balance is valid this.balance = balance; // assign it to instance variable balance // method that deposits (adds) only a valid amount to the balance public void deposit(double depositAmount) { if (depositAmount > 0.0) { // if the depositAmount is valid balance = balance + depositAmount; // add it to the balance 1 2 3 // Fig. 3.9: AccountTest.java // Inputting and outputting floating-point numbers with Account objects. import java.util.Scanner; public class Account Test { public static void main(String[] args) { Account accountl = new Account("Jane Green", 50.00); Account account2 = new Account("John Blue", -7.53); // display initial balance of each object System.out.printf("%s balance: $%.2f%n", account1.getName(), account.getBalance()); System.out.printf("%s balance: $%.2f%n%n", account.getName(), account2.getBalance()); // create a Scanner to obtain input from the command window Scanner input = new Scanner(System.in); System.out.print("Enter deposit amount for accounti: "); // prompt double depositAmount = input.nextDouble(); // obtain user input System.out.printf("%nadding %.2f to accounti balance%n%n", depositAmount); accounti.deposit(depositAmount); // add to accounti's balance // display balances System.out.printf("%s balance: $%.2f%n", accountl.getName(), accounti.getBalance()); System.out.printf("%s balance: $%.2f%n%n", account2.getName(), account2.getBalance()); System.out.print("Enter deposit amount for account2: "); // prompt depositAmount = input.nextDouble(); // obtain user input System.out.printf("%nadding %.2f to account balance%n%n", depositAmount); account2.deposit(depositAmount); // add to account balance // display balances System.out.printf("%s balance: $%.2f%n", account.getName(), accounti.getBalance()); System.out.printf("%s balance: $%.2f%n%n", account2.getName(), account.getBalance()); 43 }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