Concept explainers
Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class Rational.
Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. Also include a default constructor that initializes an object to 0 (that is, to 0/1).
Overload the input and output operators >> and <<. Numbers are to be input and output in the form 1/2, 15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so −1/2, 15/32, and −300/−401 are also possible inputs. Overload all of the following operators so that they correctly apply to the type Rational: ==, <, <=, >, >=, +, −, *, and /. Also write a test program to test your class.
(Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b. You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/−8 would be represented the same as −1/2. You should also write a test program to test your class.)
Trending nowThis is a popular solution!
Chapter 11 Solutions
Problem Solving with C++ (10th Edition)
Additional Engineering Textbook Solutions
Concepts of Programming Languages (11th Edition)
Starting Out with C++: Early Objects
Introduction To Programming Using Visual Basic (11th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
- SOLVE IN "C#" Write c# equivalent statements for the following: There is a book library. Define classes respectively for a library. The library must contain a name and a list of books (use array). The books only contain author name as information. In the class, which describes the library, create methods to add a book to the library, to search for a book by a predefined author, and to delete a book from the library. Write a test class, which creates an object of type library, adds several books to it. Implement a test functionality, which finds all books authored by Stephen King and deletes them. Finally, display information for each of the remaining books.arrow_forwardHelp write the code for the follwing problem starting from scratch Write a complete C++ program in three files here that asks the user to enter a radius for a circle then calculates and prints its' area. The area should be calculated using the formula area = 3.14 * radius * radius. You must have a class called Circle that stores the radius as a double value and can calculate its' own area via a method called calcArea that takes no parameters and returns the area. The circle class may not print the area or radius! Hint: calcArea should take no parameters and return a double. For this version, make radius private and write setter (mutator) and getter (accessor) methods and/or constructors as needed to modify (set) and retrieve (get) the value of radius. Input Validation: If a negative radius is passed to an object of the Circle class, whether through a setter or a constructor, set the radius to 0 instead of using the negative value. This program MUST be split into three files: Header…arrow_forward3. A chess board is an 8x8 matrix of squares. Each square can be empty or occupied by a chess piece. Write a class for a chess board. In the class, write a function that puts a chess piece into the position given as a parameter. The position can be given as numbers 0..7, 0.7. Note: I need an answer on C++ code.. please do it fastarrow_forward
- Create a (C++) class that will store a list of names. Your class needs to include a function that will return the name that appears first aphabetically, and another function that should return the name that appears last alphabetically. You also need to include a function that will sort the list alphabetically. The class does not need to be case insensitive.arrow_forwardExercise 10 Automatic type conversion is called coercion. For example, if an int variable and a float variable are added in Java, the value of the int variable is coerced to float and a floating-point add is done. True or Falsearrow_forward11.arrow_forward
- i cant use static void Main(string[] args) for this assignment for C# this is what I have to work with using System; using static System.Console; class GuessingGame { static void Main() { // Write your main here } } This problem relies on the generation of a random number. You can create a random number that is at least min but less than max using the following statements: Random ranNumberGenerator = new Random();int randomNumber;randomNumber = ranNumberGenerator.Next(min, max); Write a program named GuessingGame that generates a random number between 1 and 10. (In other words, in the example above, min is 1 and max is 11.) Ask a user to guess the random number, then display the random number and a message indicating whether the user’s guess was too high, too low, or correct.arrow_forwardjavaarrow_forwardC++ programAnd please include .h and .cpparrow_forward
- by c++arrow_forwardNOTE: Please don't Copy Paste from another question similar to this. I looked through them and nothing works properly. Please write in Python and C++ language this given code.arrow_forwardPlease type this code in Java Language and please do not copy and paste it from other similar questions I will dislike the question and will write bad reviews. So please write your own code in Java Language and also post Output for the code too.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT