Friday, July 3, 2015

// // 1 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 Ex03_12.java and Ex03_12_Test.java). 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
javac Ex03_12.java Ex03_12_Test.java

To run Ex03_12_Test, use the command
java Ex03_12_Test

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * 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 Ex03_12_Test.java to test Class Ex03_12.java

//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.in);
       
        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);
        account.credit(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);
        account.debit(debitAmount);
        
        System.out.printf ("Your Account Balance is: $%.2f\n\n", account.getBalance());

    }
}


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

1 comment:


  1. Siapa sih yang nggak tau dengan Bioskopkeren21, Bioskopkeren.at.ua yang di kenal dengan nama BioskopKeren atau Bioskop Keren, adalah salah satu website Nonton Film Online atau Nonton Movie Online nama lainnya Film Bioskop Online yang paling rame pengunjungnya, waduw.. rame macam pasar yah kaka... wkwkwk..... eits .. bukan lah... masa BioskopKeren.at.ua di bilang pasar... ampun dah nih kakak

    Bioskopkeren.at.ua Menyajikan berbagai Film Film yang BoxOffices ataupun Film yang lagi HITS banget...macam lagu aja pake HITS...wkwkwk, baik itu Film Barat , Film Anime, ataupun Film Indonesia, maupun Film Drama Korea yang bikin kita baper.. hahahaha... lagi curhat ni kk...wkwkwkkw.... karena Web BioskopKeren.at.ua menyediakan berbagai genre genre yang terlengkap, di BioskopKerenatua menyajikan berbagai film yang akan tayang di Bioskop 21 tanah air kita indonesia,

    Di Bioskop Keren sudah menyediakan link-link untuk anda yang ingin Download Film Bioskop atau pun Download Subtitel Indonesia , dan di lengkapi Susbtitel indonesia maupun substitel ingris, BioskopKeren.at.ua Menyediakan juga berbagai pilihan server jika salah satu server Streaming Movies sudah mati ataupun tidak jalan, di BioskopKeren tidak kalah juga dengan website Watch Movies Online yang lain nya loh..

    Nah buat kalian yang lagi sedang cari Film Bioskop kesukanaan kaka ataupun Film yang sudah tayang di Bioskop XXI ataupun lagi mencari film film yang segera tayang di Bioskop XX1, saran saya ada baiknya anda mengunjungi web BioskopKeren.at.ua

    ReplyDelete


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.

Thanks.