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
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
Object Oriented Programming: 213COMP, 214COMP (Feb-2022) Assignment- I [10 marks] Academic honesty: O Only pdf file accepted & student ID, will be your upload file. O Student who submit copied work will obtain a mark of zero. O Late work or attach file by email message not allowed. Q1: Write the signature for a method that has one parameter of type String, and does not return a value. Q2: Write the signature for a method that has two parameters, both of type Student, and returns an int value. Q3: Write the constructor's headers of the followings? new Student (202101156, “Ahmed"); new Address(51, "jazan university","CS&IT" ); new Grade(true, 505235600, 4.5); Q4: a) Write a class Student that define the following information: name, stid , age. b) Explain a mutators (setters) and accessors(getters) methods for each attributes(fields). c) Add three constructors: • with empty constructor. one parameter constructor (name of student) two parameters constructor (name and stid) d) Create two…

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
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