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....
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...
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,...
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...
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...
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
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide...
Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide and clear. Test your calculator by writing a main program incorporating the test code below: Calculator mycalc; mycalc.clear(); mycalc.add(4.52); mycalc.add(3.789); mycalc.divide(2.6); mycalc.multiply(3.12); mycalc.subtract(2.678); cout << mycalc.display() << endl;       // prints out "7.2928" mycalc.clear(); mycalc.add(5.0); cout << mycalc.display() << endl;       // prints out "5" //advanced stuff #1: add a constructor Calculator calc1; cout << calc1.display() << endl;  //prints out 0 //advanced stuff #2: add a parameterized...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...