Java Format: Unbound (saleable)
Java Format: Unbound (saleable)
8th Edition
ISBN: 9780134448398
Author: SAVITCH, Walter
Publisher: Prentice Hall
Expert Solution & Answer
Book Icon
Chapter 9, Problem 9E

Explanation of Solution

Creating a class “RationalAssert.java”:

  • Import required package.
  • Define the class “RationalAssert”.
    • Declare the private variables “the_numerator”, and “the_denominator”.
    • Define parameterized constructor.
      • Assign “numerator” to “the_numerator”.
      • Assign “denominator” to “the_denominator”.
      • Create an assertion with the condition “assert the_denominator != 0”.
    • Give mutator methods to set numerator and denominator.
    • Give accessor methods to get the value of numerator and denominator.
    • Give function definition for “value ()”.
      • Return the value produced by “the_numerator / the_denominator”.
    • Given function “toString ()”.
      • Return the string “the_numerator / the_denominator”.
    • Define the “main ()” method.
      • Create an object for “Scanner” class.
      • Declare the variables “n”, and “d”.
      • Get the numerator and denominator from the user.
      • Call the constructor by passing “n”, and “d”.
      • Call the function “value”.
      • Call the function to get the numerator and denominator.
      • Print new line.

Program:

Rational.java:

import java.util.Scanner;

//Define a class

public class RationalAssert

{

    //Declare required variables

    private int the_numerator;

    private int the_denominator;

    //Parameterized constructor

    public RationalAssert(int numerator, int denominator)

    {

        //Assign the value of numerator

        the_numerator = numerator;

        //Assign the value of denominator

        the_denominator = denominator;

        //Declare an assertion

        assert the_denominator != 0;

    }

    //Function to set the value of numerator

    public void setNumerator(int numerator)

    {

        //Set the value

        the_numerator = numerator;

    }

    //Function to set the denominator

    public void setDenominator(int denominator)

    {

        //Declare an assertion

        assert the_denominator != 0;

        //Assign the value

        the_denominator = denominator;

    }

 �...

Blurred answer
Students have asked these similar questions
Answer both to get a like or don't attempt.Thanks Question: 3. _________is a correct annotation with respect to JUnits.4. Is it necessary to write the test class to test every class? __________
Hello, need help with this exercise in JAVA. We need to use the STATE DESIGN PATTERN.   GIVEN CODE: public class Pizza { public final static int COOKED = 0; public final static int BAKED = 1; public final static int DELIVERED = 2; int state = COOKED; public int getState() {return state; } public void setState(int state) {this.state = state; } public void bake() throws Exception { if(state == COOKED) { System.out.print("Baking the pizza..."); state = BAKED; } else if(state == BAKED) { throw new Exception("Can't bake a pizza already baked"); } else if(state == DELIVERED) { throw new Exception("Can't bake a pizza already delivered");} } public void deliver() throws Exception { if(state == COOKED) { throw new Exception("Can't deliver a pizza not baked yet"); } else if(state == BAKED) { System.out.print("Delivering the pizza..."); state = DELIVERED; } else if(state == DELIVERED) { throw new Exception("Can't deliver a pizza already delivered"); } } }   EXERCISE: Refactor the pizza class…
describe how exception-handling is using try/except similar to and different from handling exceptional cases using ordinary decision structures (variations on if)?

Chapter 9 Solutions

Java Format: Unbound (saleable)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education