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;
}
�...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
- 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.arrow_forwardJava Foundations : Please provide code and explain. Thank you. Write the code segments in main( ) and in methodA( ) for the following scenario: main( ) passes methodA an int variable called a. methodA generates an exception, but does not deal with it, if a is not between 1 and 100.arrow_forwardUse java and use proper code indentation.arrow_forward
- please include comments for better understandingarrow_forwardplease solve this important, thanks.arrow_forwardWrite C# equivalent statements for the following: An “Account” class will be created having properties: accountName, accountNumber, accountType, totalBalance and methods: WithdrawAmount(), CheckBlance(), DepositAmount(). Derive child classes, that is, “SavingAccount” and “CurrentAccount” from the Account class and implement the following functionality. While taking input from user, check whether the account type is Saving or Current. If user enters “Saving”, an object of Saving Account will be instantiated and while depositing amount in the account an extra 3% of the amount will be deposited (as an extra profit) with the actual amount entered by user. If user enters “Current”, an object of Current Account will be instantiated and while depositing amount in the account, no extra amount will be deposited. Only the actual amount will be deposited. Also implement all the methods mentioned above.arrow_forward
- What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.arrow_forwardlanguage is c++ sample output included with user input in boldarrow_forwardSolve this program in C++ and attach the screenshot of output also. Solve this ASAParrow_forward
- Answer with True or False for the following. Explain your answers.-It is legal to write a method in a class which overloads another method declared in the same.-It is legal to write a method in a superclass which overrides a method declared in a sub-class.arrow_forwardfunction in Python called PassFail(), that returns the number of points required on the last exam in order to pass this class based on the first four exam points and the 55% exam standard mentioned in the syllabus. Note: there are a total of five exams. The first four exams are worth 100 points each. The fifth and last exam is worth 200 points.arrow_forwardObserve the following Java statement and the answer the question. Choose ONE that is true. System.out.println(circleArea = super.calArea()); a. This statement may exist in classes that are not related by inheritance b. The use of super will cause Java to raise exception. It should be replaced with an object. c. The returned value from the superclass method named calArea() is displayed d. None of the abovearrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning