Question
Java: Modify class Account.java to provide An integer class instant variable accountNum to be the class number (Dont forget that you need to change the
Java: Modify class Account.java to provide
An integer class instant variable accountNum to be the class number (Dont forget that you need to change the constructor).
A method called debit that withdraws money from an Account. Ensure that the debit amount does not exceed the Accounts balance. If it does, the balance should be left unchanged and the method should print a message indicating Debit amount exceeded account balance.
In method getBalance(), add a line to print out the account number and account balance, then in the Test program, you just need to type, account1.getBalance(); instead of the printf statement.
AccountTest file has been modified already to test your new Account class, so no change on AccountTest.java. Only Account.java should be changed
Here is Account.java that needs to be modified:
public class Account { private double balance; // instance variable that stores the balance // *** To do 1. Add another instance variable that stores the account number // constructor (*** To do 2. Need to initialize account number) public Account( double initialBalance ) { // if initialBalance is not greater than 0.0, // balance is still initialized to 0.0 by default if ( initialBalance > 0.0 ) balance = initialBalance; } // end Account constructor // credits (adds) an amount to the account public void credit( double amount ) { balance = balance + amount; // add amount to balance } // end method credit // ***To do 3. debits (subtracts) an amount from the account // returns the account balance public double getBalance() // **** To do 4. add a print-out { return balance; // gives the value of balance to the calling method } // end method getBalance } // end class Account
Here is AccountTest.java that does not need to be changed at all:
import java.util.Scanner; public class AccountTest { // main method begins execution of Java application public static void main( String args[] ) { Account account1 = new Account( 1, 50.00 ); // create Account object Account account2 = new Account( 2, -7.53 ); // create Account object // display initial balance of each object account1.getBalance(); account2.getBalance(); // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); double depositAmount; // deposit amount read from user System.out.print( "Enter deposit amount for account2: " ); depositAmount = input.nextDouble(); // obtain user input System.out.printf( " adding %.2f from account2 balance ", depositAmount ); account2.credit( depositAmount ); // add amount to account2 // display balances account1.getBalance(); account2.getBalance(); // create Scanner to obtain input from command window input = new Scanner( System.in ); double withdrawalAmount; // withdrawal amount read from user System.out.print( "Enter withdrawal amount for account1: " ); withdrawalAmount = input.nextDouble(); // obtain user input System.out.printf( " subtracting %.2f from account1 balance ", withdrawalAmount ); account1.debit( withdrawalAmount ); // subtract amount from account1 // display balances account1.getBalance(); account2.getBalance(); } // end main } // end class AccountTest
In this picture is an example of how the output should look:
Thank you in advance!!
Account 1 balance: $50.00 Account 2 balance: $0.00 Enter deposit amount for account2: 25.53 adding 25.53 from account2 balance Account 1 balance: $50.00 Account 2 balance: $25.53 Enter withdrawal amount for accountl: 32.17 subtracting 32.17 from accountl balance Account 1 balance: 417.83 Account 2 balance: $25.53Step 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