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

Videos

Textbook Question
Book Icon
Chapter 9, Problem 8E

Create a class Rational that represents a rational number. It should have private attributes for

  • The numerator (an integer)
  • The denominator (an integer)

and the following methods:

  • Rational (numerator, denominator) — a constructor for a rational number.
  • Accessor methods getNumerator and getDenominator and mutator methods setNumerator and setDenominator for the numerator and the denominator.

You should use an exception to guarantee that the denominator is never zero.

Blurred answer
Students have asked these similar questions
Modify the attached code: Create a new class called CalculatorWithMod. This class should be a sub class of the base class Calculator. This class should also have an additional method for calculating the modulo. The modulo method should only be seen at the sub class, and not the base class! Include exception handling for instances when dividing by 0 or calculating the modulo with 0. You would need to use throw, try, and catch. The modulo (or "modulus" or "mod") is the remainder after dividing one number by another.Example: 20 mod 3 equals 2Because 20/3 = 6 with a remainder of 2 Refer also to the attached sample outputs:
Create a class MyTime which has the datamembers as follows: 1. hour: integer (1 to 12)2. minute: integer (0 to 59)3. second: integer (0 to 59)4. pm: bool variable. True means PM time and false means AM time. Implement a default constructor, a parameterized constructor and a copy constructor. Write set and get functions for all the four members of the class. Overload these operators: 1. Extraction operator >> : Prompt the user for hours, minutes, seconds and for am/pm and initialize the structure. Left operand istream object and right operand MyTime2. Assignment = : Assign the right object to the left object. Both operands are of type MyTime.3. Insertion << : Print the time in the format HH:MM SS PM. Left operand of type ostream and right operand of type MyTime. this in c++.
The following class maintains an account balance and returns aspecial error code.public class Account{private double balance;// returns new balance or -1 if errorpublic double deposit(double amount){if (amount > 0)balance += amount;elsereturn -1; // Code indicating errorreturn balance;}}Rewrite the class so that it throws appropriate exception instead ofreturning -1 as an error code. Write test code that attempts to depositinvalid amounts and catches the exceptions that are thrown

Chapter 9 Solutions

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

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Memory Management Tutorial in Java | Java Stack vs Heap | Java Training | Edureka; Author: edureka!;https://www.youtube.com/watch?v=fM8yj93X80s;License: Standard YouTube License, CC-BY