Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 9, Problem 2P

Explanation of Solution

Modified code:

“Account.java”:

//Class Account

public class Account

{

    //Private variable

    private double balance;

    //Define a constructor

    public Account()

    {

        //Initialize the amount value

        balance = 0;

    }

    //Parameterized constructor

    public Account(double initialDeposit)

    {

        //Assign the value

        balance = initialDeposit;

    }

    //Function to get balance

    public double getBalance()

    {

        //Return the amount

        return balance;

    }

    //Function to deposit the amount

public double deposit(double amount) throws NegativeAmountException

    {

        //Check if amount is greater than 0

        if (amount > 0)

            //Add the value to the balance

            balance += amount;

        //Else

        Else

            //return -1;

            //Throw an exception

            throw new NegativeAmountException();

        //Return the balance

        return balance;

    }

    //Function definition to withdraw the amount

public double withdraw(double amount) throws InsufficentBalanceException, NegativeAmountException

    {

        //Check if amount is greater than balance

        if (amount > balance)

            //throw an exception

            throw new InsufficentBalanceException();

        //Check if amount is less than 0

        else if (amount < 0)

            //Throw an exception

            throw new NegativeAmountException();

        //Else

        else

            //Calculate the balance

            balance -= amount;

        //Return the balance amount

        return balance;

    }

    //Main method

    public static void main(String[] args)

    {

        //Create an object for the class

        Account a = new Account(10);

        //Try block

        try

        {

            //Deposit the value

            a...

Blurred answer
Students have asked these similar questions
PROGRAMMING LANGUAGE: Java
The following class maintains an account balance and returns a special error code. public class Account { private double balance; // returns new balance or -1 if error public double deposit(double amount) { if (amount > 0) balance += amount; else return -1; // Code indicating error return balance; } } Rewrite the class so that it throws appropriate exception instead of returning -1 as an error code. Write test code that attempts to deposit
Value-returning methods must pass parameters. -True or -False

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning