Friday, July 3, 2015

// // Leave a Comment

Program To Show How To Use Constructors In Java

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 3 Exercise 3.12.

Question: Modify class Account (Fig. 3.13) to provide a method called debit that withdraws money from an Account. Ensure that the debit 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 "Debit amount exceeded account balance." Modify class AccountTest (Fig. 3.14) to test method debit.

To run the application save both files with the same name as the class (because it is a public class) and with the .java file extension (in this case and Then compile both classes and run Ex03_12_Test. Ex03_12 will not run because it does not have a main method.

To compile both classes at the same time using the command prompt, use the command

To run Ex03_12_Test, use the command
java Ex03_12_Test

 * @Author: Aghatise Osazuwa
 * Website:
 * Exercise 3.12 - Modified Account Class
 * This Program Modifies Class Account (Fig. 3.13) In The Book To Provide
 * A Method Debit That Withdraws Money From An Account

 public class Ex03_12 {

    private double balance;

    public Ex03_12 (double initialBalance) {
        if (initialBalance > 0.0) 
            balance = initialBalance;
    public void credit (double amount) {
        balance = balance + amount;
    public double getBalance () {
        return balance;
    public void debit (double amount) {
        if (amount > balance)
            System.out.printf ("Debit amount exceeded account balance.\n");
        if (amount == balance)
            balance = balance - amount;
        if (amount < balance)
            balance = balance - amount;


Below is Class to test Class

//Exercise 3.12 - Modified Account Class Test
//This Program Tests Method Debit Of Ex03_12

import java.util.Scanner;

public class Ex03_12_Test {
    public static void main (String [] args) {

 Ex03_12 account = new Ex03_12 (0.0);

 Scanner input = new Scanner (;
        System.out.printf ("Your Account Balance is: $%.2f\n\n", account.getBalance());
        System.out.printf ("Enter Deposit Amount: ");
        double depositAmount = input.nextDouble();
        System.out.printf ("\nAdding %.2f to your account balance\n\n", depositAmount);;
        System.out.printf ("Your Account Balance is: $%.2f\n\n", account.getBalance());
        System.out.printf ("Enter Debit Amount: ");
        double debitAmount = input.nextDouble();
        System.out.printf ("\nDebiting %.2f from your account balance\n\n", debitAmount);
        System.out.printf ("Your Account Balance is: $%.2f\n\n", account.getBalance());


Click here to see other answers to Java How to Program.


Post a Comment

DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.