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....
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,...
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
Coding in Java Create an Airplane class (not abstract) that uses the Vehicle interface in Q1....
Coding in Java 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 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. // code from Q1: interface Vehicle { void Start();...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
a/b x c/dy Let's design a class and explore more on object reference: A fraction with...
a/b x c/dy Let's design a class and explore more on object reference: A fraction with two private fields: a for representing numerator and b for representing denominator. Define a constructor Fraction() accept two integers to represent the numerator and denominator. Define a public instance method named printString() which returns a String in form of "a/b". Define a public instance method named multiplyBy() which accepts one argument of class type Fraction, calculates the multiplication between self and the receiving Fraction,...
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks the...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT