Question

Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...

Cpp

Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part).

Details and Requirements

  1. Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A zero in the denominator of the fraction part constitutes an illegal number and should not be allowed. You should create appropriate member data in your class. All member data must be private.

  2. There should be two constructors. One constructor should take in three parameters, representing the integer part, the numerator, and the denominator (in that order), used to initialize the object. If the mixed number is to be a negative number, the negative should be passed on the first non-zero parameter, but on no others. If the data passed in is invalid (negatives not fitting the rule, or 0 denominator), then simply set the object to represent the value 0. Examples of declarations of objects:

     Mixed m1(3, 4, 5);    // sets object to 3 4/5
     Mixed m2(-4, 1, 2);   // sets object to -4 1/2
     Mixed m3(0, -3, 5);   // sets object to -3/5 (integer part is 0).
     Mixed m4(-1, -2, 4);  // bad parameter combination.  Set object to 0.
    

    The other constructor should expect a single int parameter with a default value of 0 (so that it also acts as a default constructor). This constructor allows an integer to be passed in and represented as a Mixed object. This means that there is no fractional part. Example declarations:

     Mixed m5(4);  // sets object to 4 (i.e. 4 and no fractional part).
     Mixed m6;     // sets object to 0 (default)
    

    Use member initialization list to initialize class fields. Note that this last constructor will act as a “conversion constructor”, allowing automatic type conversions from type int to type Mixed.

  3. The Mixed class should have public member function Print(). This function should output to the console the mixed number in the same format as above, with the following exceptions: If the object represents a 0, then just display a 0. Otherwise: If the integer part is 0, do not display it. If the fraction part equals 0, do not display it. For negative numbers, the minus sign is always displayed to the left.

    Examples: 0 , 2 , -5 , 3/4 , -6/7 , -2 4/5 , 7 2/3

Instructions

  • Create the file lab2.cpp, and copy below driver program into it.
  • Implement Mixed class following the above description.

Driver Program

This code provides minimal set of tests for your Mixed class. Note, this is not a comprehensive set of tests. It is just a some code to get you started, illustrating some sample calls.

#include <iostream>
using namespace std;

class Mixed {
    // write your solution here
}

int main(){
    Mixed m1(3, 4, 5);    // sets object to 3 4/5
    m1.Print(); // prints 3 4/5
    Mixed m2(-4, 1, 2);   // sets object to -4 1/2
    m2.Print(); // prints -4 1/2
    Mixed m3(0, -3, 5);   // sets object to -3/5 (integer part is 0).
    m3.Print(); // prints -3/5
    Mixed m4(-1, -2, 4);  // bad parameter combination.  Set object to 0.
    m4.Print(); // prints 0
    Mixed m4a(1, -2, -4); // bad parameter combination.  Set object to 0.
    m4a.Print(); // prints 0
    Mixed m5(4);
    m5.Print(); // prints: 4
    Mixed m6;
    m6.Print(); // prints 0
}

Homework Answers

Answer #1
Here numerous variables are used whose description are given in code ...


#include <bits/stdc++.h>

using namespace std;
class Mixed{
   bool contain_integer=false;   //check whether given data contains integer or not
   bool is_negative=false; //check whether given data is positive or negative
   bool contain_fraction=false; //check whether given data contains fraction or not
   bool done=false; //check whether we have dealt with the data or not
   int Integer_part;//integer part of data
int Numerator; //number of fraction if exists
int Denominator; //denominator of fraction if exists
   public:
        Mixed(int integer_part,int numerator,int denominator)
       {
           if(denominator==0)   //if denominator is 0 it is invalid 
           {
               Integer_part=0;    // make the value 0
               contain_integer=true;
               done=true;
           }
           if(!done)  //is condition aboe doesnot match
           { 
               if(integer_part!=0)   //checking for integet part
               {
                   Integer_part=integer_part;  //assigning the value
                   if(numerator<0||denominator<0)  //this conditions checks for invalidity
                   {
                     Integer_part=0;
                      contain_integer=true;
                     done=true;

                   }
                   if(!done)  //check if above condition is false
                   {
                       if(integer_part<0)  //check whether integer_part is negative
                       {
                           is_negative=true; //set variable to true
                       }
                       if(numerator==0)  // if numerator is 0 then only integer part will be //there
                       {
                           Integer_part=integer_part; //setting the integer part
                         contain_integer=true;
                         done=true;
                       }
                       if(!done) //if above conditions does not match
                       {
                           Numerator=numerator;  //then simple data contains all three parts
                       Denominator=denominator;
                       contain_fraction=true;
                       contain_integer=true;
                       done=true;
                       }

                   }


               }
               else
               {
                 contain_integer=false; 
                if(numerator!=0)
                {

                    if(denominator<0)  //checks for invalidity
                    {
                          Integer_part=0;
                      contain_integer=true;
                     done=true;


                    }
                    else
                    {
                        if(numerator<0)  //contains only fraction --> if it is negative then //set variable to true
                        {
                            is_negative=true;


                        }
                        Numerator=numerator;  //assigning the values

                        Denominator=denominator;
                    contain_fraction=true;

                    }

                }
                else //if numerator is zerp then value is 0
                {
                    Integer_part=0;   
                      contain_integer=true;
                     done=true;


                }



               }




           }

       }
       Mixed(int integer_part=0)  //default argument 
       {
           contain_integer=true;   // contains only integer
           if(integer_part<0)
            is_negative=true;
           Integer_part=integer_part;


       }
       void Print()
       {
           if(is_negative==true)  //check whether data was negative or positive
            cout<<"-";
           if(contain_integer)   //check whether data contains integer part or not
           {
               cout<<abs(Integer_part)<<" ";

           }
           if(contain_fraction) //check whether data contains fraction
           {
               cout<<abs(Numerator)<<"/"<<abs(Denominator);
           }
      cout<<"\n";

       }





};
int main()
{
Mixed m1(3,4,5);
    m1.Print(); // prints 3 4/5
    Mixed m2(-4, 1, 2);   // sets object to -4 1/2
    m2.Print(); // prints -4 1/2
    Mixed m3(0, -3, 5);   // sets object to -3/5 (integer part is 0).
    m3.Print(); // prints -3/5
    Mixed m4(-1, -2, 4);  // bad parameter combination.  Set object to 0.
    m4.Print(); // prints 0
    Mixed m4a(1, -2, -4); // bad parameter combination.  Set object to 0.
    m4a.Print(); // prints 0
    Mixed m5(4);
    m5.Print(); // prints: 4
    Mixed m6;
    m6.Print(); // prints 0
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
Create a class hierarchy to be used in a university setting. The classes are as follows:...
Create a class hierarchy to be used in a university setting. The classes are as follows:  The base class is Person. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The class should also have a static data member called nextID which is used to assign an ID number to each object created (personID). All data members must be private. Create the following member functions: o Accessor functions to allow access to first name and last...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
For this assignment, you'll create a Player class that captures information about players on a sports...
For this assignment, you'll create a Player class that captures information about players on a sports team. We'll keep it generic for this assignment, but taking what you learn with this assignment, you could create a class tailored to your favorite sport. Then, imagine using such a class to track the stats during a game. Player Class Requirements Your class should bet set up as follows: Named Player Is public to allow access from other object and assemblies Has a...
Create a class called Invoice that a hardware store might use to represent an invoice for...
Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables – a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initialize the four instance variables. Provide a set and a get method for each...
Write in C++. Define a class called Text whose objects store lists of words. The class...
Write in C++. Define a class called Text whose objects store lists of words. The class Text will be just like the class StringVar except that the class Text will use a dy- namic array with base type StringVar rather than base type char and will mark the end of the array with a StringVar object consisting of a single blank, rather than using '\0' as the end marker. Intuitively, an object of the class Text represents some text consisting...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
3.6 Appointments PYTHON (3 pts) Create a new class called Appointment. An appointment should have a...
3.6 Appointments PYTHON (3 pts) Create a new class called Appointment. An appointment should have a description and a date. Your Appointment class should take the description and date from the constructor and assign them to instance variables. Your constructor should take parameters in the following order: description, year, month, day. Your instance variables should be called: description year month day (2 pts) Override the __str__(self) method to print a human-friendly version of the object. Your__str__ method should produce output...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for...
Create an Airplane class (not abstract) that uses the Vehicle interface in Q1. The code for all methods should print simple messages to the screen using System.out.println(). Add an integer speed variable that is changed using the ChangeSpeed method. ChangeSpeed adds 5 to the speed each time it is called. Create a default constructor that sets the initial speed to 0. Don't create other constructors or any setter/getter methods. PLEASE CODE THIS IN JAVA
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • 4. List and describe the THREE (3) necessary conditions for complete similarity between a model and...
    asked 9 minutes ago
  • In C++ Complete the template Integer Average program. // Calculate the average of several integers. #include...
    asked 14 minutes ago
  • A uniform rod is set up so that it can rotate about a perpendicular axis at...
    asked 16 minutes ago
  • To the TwoDArray, add a method called transpose() that generates the transpose of a 2D array...
    asked 37 minutes ago
  • How could your result from GC (retention time, percent area, etc.) be affected by these following...
    asked 47 minutes ago
  • QUESTION 17 What are the tasks in Logical Network Design phase? (Select five. ) Design a...
    asked 49 minutes ago
  • What is the temperature of N2 gas if the average speed (actually the root-mean-square speed) of...
    asked 58 minutes ago
  • Question One: Basic security concepts and terminology                         (2 marks) Computer security is the protection of...
    asked 1 hour ago
  • In program P83.cpp, make the above changes, save the program as ex83.cpp, compile and run the...
    asked 1 hour ago
  • the determination of aspirin in commercial preparations experment explain why the FeCl3-KCl-HCl solution was ased as...
    asked 1 hour ago
  • Describe important events and influences in the life of Wolfgang Amadeus Mozart. What styles, genres, and...
    asked 1 hour ago
  • 3.12 Grade Statistics Write a python module "school.py" that prints school information (first 3 lines of...
    asked 1 hour ago