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)
- Code in C++ Language As a cashier, you would always hate those times when customers choose to take back their order since you'd always summon your manager and yell, "Ma'am pa-void!" No matter how you hate it, you always have to do it since you have no other choice. Given (1) an array of prices of the items, (2) another array of the items the customer wishes to take back, and (3) the customer's payment, print the change of the customer. Input: The first number indicates the number of items "n" brought to the cashier. The next "n" numbers are the prices of the items. The next number indicates the number of items "m" that the customer wants to take back and not proceed. It is assured that 0 <= m <= n. The next "m" numbers are the items to be void that ranges from item 1 to item "n". The final number is the customer's payment. It is also assured that the given payment >= final price of items. INPUT: 5 103.65 650.95 10.25 1067.30 65.18 2 1 5 2000.00 Output: The change…arrow_forwardIn java Develop a function that takes two integers and returns the sum of the values from the first to the second (the two inputs may be in any order). Call the function at least twice with different arguments Develop a void function PrintLineOfChar that accepts a single character and an integer. The function prints a single line of the character repeated the integer number of times. Call the function at least twice with different arguments.arrow_forwardSOLVE 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_forward
- implement pass-by-value parameter passing method.USE C LANGUAGEarrow_forwardYou must use C++ as the programming language. Please do the following:Create a program that illustrates C++ classes. Your program should do the following:Write a class named Car that has the following member variables: year (An int that holds the car’s model year. The year must be between 1900-2030 (inclusive)). make (A string that holds the make of the car.) speed (An int that holds the car’s current speed. The speed must be 0 or greater. )In addition, the class should have the following member functions: Constructor - The constructor should accept the car’s year and make as arguments and assignthese values to the object’s year and make member variables. The constructor should initializethe speed member variable to 0. Notice that this constructor will have 2 parameters: one forthe year and one for the make of the Car. When you define a Car object, you will need to send the two arguments the constructor isexpecting:Car myCar(2002, “Jetta”); In the constructor you need to assign a…arrow_forwardWhat is the difference between static and dynamic type checking? Static type checking occurs at compile-time, while dynamic type checking occurs at runtime. ○ Static type checking is faster than dynamic type checking. Dynamic type checking ensures that the program will not crash. Static type checking allows types to change during execution.arrow_forward
- Help 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_forwardI need help writing a C++ code.arrow_forwardc programmingarrow_forward
- 3. 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_forwardCreate 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_forwardAgain I'm stuck give me answer please. In form.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT