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
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
A PMC (private military contractor) has contacted you to help write code to control the Security Checkpoints for their company.  You must create (in C++) a Checkpoint class with a hidden attributes (Authorized, Guest, Unauthorized), two constructors (a default that sets all access levels to false and an overloaded that sets Authorized and Unauthorized to false and Guest to true for testing purposes) and a method that changes the access level to the next in the sequence (Authorized – Guest – Unauthorized).
Use C++ Create a class called Fruit. Add one member variables name. Add only one getter function for the name. The get function returns name. Implement a constructor that expects name value and assigns it to the member variable. This class doesn't have an empty constructor. Create a subclass of Fruit named CitrusFruit. The CitrusFruit class has two member variables weight and color.  Add a constructor which expects weight and  name and color.Pass name to the base constructor and set weight and color to it's member variable. Write a program that has an array of  3 Fruits, and an array of 3 CitrusFruits. Feel free to give the name , weight and color to the object as your choice. Use loops to print out the array properties to the terminal screen.
Write a C++ class and choose any name for it. The class should have two privatemembers and two public members. Write a constructor to initialize these membersto zero. Write another constructor to initialize these members to non-zero values.Write a method to return the average of the class members. Write a C++ programand initialize the members of the class with [2,5,8,3] respectively and call its methodto calculate the average.

Chapter 9 Solutions

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

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning