Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Account class with a constructor to // initialize instance variable balance. public class Account { private double balance; // instance variable that stores the

// Account class with a constructor to // initialize instance variable balance.

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

// Create and manipulate an Account object. 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

Modify class Account 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 you do not need to change it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions