Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
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
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
PROGRAMMING LANGUAGE: C++ ALSO PUT SCREENSHOTS WITH EVERY TASK. TASK 1 : A class of ten students took a quiz .The grades (integers are in range 0-100) for this quiz are available to you .Determine class average on quiz . TASK 2: Write c++ code that print summery of exam result and decide either student should have makeup class or not .If more then 30% of class fails in exam it’s mean they need a makeup class otherwise they don’t need any makeup class. For class strength take input from user  (Hint: take two variables pass and fail) TASK 3: write a c++  that will determine whether a department-store customer has exceeded the credit limit on the charge account .For each customer , following facts are available : Account number (an integer) Balance at beginning of month Total of all items charged by this customer this month Total of all credit applied to this customer’s account this month. Allowed credit limit You are required to use a while structure to input each of these facts ,…
Java Questions - (Has 2 Parts). Based on each code, which answer out of the choices "A, B, C, D, E" is correct. Each question has one correct answer. Thank you. Part 1 - The following code will cause a(n) ___ exception. class Sample{public static void main(String[] args) {   int x = 0;   int y = 10;   int z = y/x; } } A. ArithmeticExceptionB. ArrayIndexOutOfBoundsExceptionC. StringIndexOutOfBoundsExceptionD. NullPointerExceptionE. ClassNotFoundException Part 2 - A programmer carelessly assigned a negative value as an index of an array. Which type of exception will be raised during execution? A. An IndexOutOfBoundsException is thrown.B. A NumberFormatException is thrown.C. The first slot of the array is used.D. This is an Error, so the program immediately terminates no matter what.E. The last slot of the array is used.

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (7th 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