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 Java code provides a Class named Function in Function.java that has a static method named f that takes a double argument and returns the value of . It also has a static method named fprime that takes a double argument for x and returns the value of the slope of f(x). The slope of f(x) can be calculated as . Implement this method. public class Function { public static double f(double x) { return Math.pow(x,3) - 3 * x; }   public static double fprime(double x) { // your code goes here   } }
THIS NEEDS TO BE DONE IN JAVA Modify the Account class to provide a debit method 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 out a message indicating "Debit amount exceeded amount balance." Modify class AccountTest to test method debit.  Below are links to the Account and AccountTest programs. //This is for the account class public class Account {private String name; //instance variableprivate double balance; //instance variable//Account constructor that receives two parameterspublic Account(String name, double balance){this.name = name; //assing name to instance varialbe name//validate that the balance is greater than 0.0; if it's not,//instance varisble balance keeps its default initial value of 0.0if (balance > 0.0){ //if the balance is valaidthis.balance = balance; //assign it to instance variable balance}}//method that deposits (adds)…
ANSWER IN JAVA Standard Output: Enter·side·length·of·square:Side·length·must·be·greater·than·0.↵ Define the class InvalidSideException, which inherits from the Exception class. Also define a Square class, which has one method variable -- an int describing the side length. The constructor of the Square class should take one argument, an int meant to initialize the side length; however, if the argument is not greater than 0, the constructor should throw an InvalidSideError. The Square class should also have a method getArea(), which returns the area of the square.Create a Driver class with a main method to test your classes. Your program should prompt the user to enter a value for the side length, and then create a Square object with that side length. If the side length is valid, the program should print the area of the square. Otherwise, it should catch the InvalidExceptionError, print "Side length must be greater than 0.", and terminate the program.

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
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
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage