Question

In: Computer Science

First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...

First lab: Create a Fraction class

  • Create member variables to store
    • numerator
    • denominator
    • no additional member variable are allowed
  • Create accessor and mutator functions to set/return
    • numerator
    • denominator
  • Create a function to set a fraction
  • Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4
    • use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string
  • Create a function to print the fraction by calling your ToString() function
  • Only positive values allowed; a fraction cannot be negative
  • Mutator functions do not ask the user for an input; in other words, no interactive input from a user
  • Call SetFraction(...) in SetDenominator and SetNumerator functions; this way all your error checking will be only in one function
  • Do not forget error checking in mutators functions; if values are invalid, set the fraction to 0/1
  • All function definitions must be outside of the class with the exception of functions with one line of code
  • Create a client program and test all accessor and mutator functions

Modify the Fraction class created in the first lab above as follows. Read the entire description before writing any code

  • Avoid redundant code
  • member variables
    • numerator & denominator only
    • do not add any member variables
  • Set functions
    • set numerator
    • set denominator
    • set fraction with one argument
    • set fraction with two arguments
    • set fractions with three arguments (whole, numerator, denominator)
    • to avoid redundancy and have error checking in one place only, call setFractions with three arguments in all other set functions
  • Create constructors
    • default
    • one argument
    • two argument
    • three argument
    • copy constructor
    • to avoid redundancy and have error checking in one place only, call setFractions with three arguments in all other set functions
  • Create a destructor
    • The destructor should set numerator and denominator to 0/1
  • Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4
    • use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string
  • Create a function to print the fraction to a console by calling your ToString() function
  • Add cout statements to the constructors and the destructor so you know when they are being executed
  • Add system ("pause") to the destructor so you know when it executes
  • Only positive values allowed
  • If the denominator is 0 set it to 1 and output an appropriate message
  • Do not allow implicit conversion of an int to a Fraction object
  • Main
    • your main should adequately test all the functions, constructors and the destructor.

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

main.cpp

#include<iostream>
#include<iomanip>

using namespace std;

class Fraction
{
private:

   int numerator, denominator;

public:

   Fraction()
   {
       numerator = 0;
       denominator = 1;
   }


   Fraction(int a, int b)
   {
       numerator = a;
       denominator = b;
       if(denominator==0){
           cout<<"Error!!! Denominator cannot be zero. Setting it to 1\n";
           denominator = 1;
       }
   }

   Fraction(int whole,int a, int b)
   {
       numerator = whole*b;
       numerator += a;
       denominator = b;
       if(denominator==0){
           cout<<"Error!!! Denominator cannot be zero. Setting it to 1\n";
           denominator = 1;
       }
   }

   void set(int a, int b)
   {
       numerator = a;
       denominator = b;
       if(denominator==0){
           cout<<"Error!!! Denominator cannot be zero. Setting it to 1\n";
           denominator = 1;
       }
   }

   void set(int whole,int a, int b)
   {
       numerator = whole*b;
       numerator += a;
       denominator = b;
       if(denominator==0){
           cout<<"Error!!! Denominator cannot be zero. Setting it to 1\n";
           denominator = 1;
       }
   }

   void setNumerator(int n){
       numerator = n;
   }
   void setDenominator(int d){
       denominator = d;
       if(denominator==0){
           cout<<"Error!!! Denominator cannot be zero. Setting it to 1\n";
           denominator = 1;
       }
   }

   int getNumerator(){
       return numerator;
   }
   int getDenominator(){
       return denominator;
   }
   string toString(){
       int whole = numerator/denominator;
       int num = numerator%denominator;
       string res="";
       if(whole!=0){
           res=to_string(whole)+" "+to_string(num)+"/"+to_string(denominator);
       }else{
           res=to_string(num)+"/"+to_string(denominator);
       }
       return res;
   }
};

int main()
{
   Fraction f(11,4);
   cout<<f.toString()<<endl;

   f.set(11,0);
   cout<<f.toString()<<endl;

   f.set(5,11,12);
   cout<<f.toString()<<endl;

   cout<<"Numerator : "<<f.getNumerator()<<endl;
   cout<<"DeNominator : "<<f.getDenominator()<<endl;


   return 0;

}

output


Related Solutions

The denominator of a fraction is 4 more than the numerator. If both the numerator and...
The denominator of a fraction is 4 more than the numerator. If both the numerator and the denominator of the fraction are increased by 3, the new fraction is 5/6 . Find the original fraction.
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2 can be expressed as the list [1,2]. (a) Write a function called factionAdd() that takes two fractions as lists and adds them. For example, fraction([1,2], [3,4]) returns [5,4] (b) Write a function fractionMult() that multiplies two fractions that are passed as lists. [HINT: You may use the following function gcd ( x ; y ) to help you calculate the Greatest Common Divisor, which...
1)  Create a UEmployee class that contains member variables for the university employee name and salary. The...
1)  Create a UEmployee class that contains member variables for the university employee name and salary. The UEmployee class should contain member methods for returning the employee name and salary. Create Faculty and Staff classes that inherit the UEmployee class. The Faculty class should include members for storing and returning the department name. The Staff class should include members for storing and returning the job title. Write a runner program that creates one instance of each class and prints all of...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
Your first lab is to create a simple class that emulates some mathematical functions in relation...
Your first lab is to create a simple class that emulates some mathematical functions in relation to complex numbers. A complex number is a number of the form a + bi, where a is a real number and bi is an imaginary number. Create a class Complex having two private data members real and imag of type double. The class has two constructors, one default (no parameters) and one whose parameters initialize the instance variables. It also need the following...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name...
in Java, Create a class called EMPLOYEE that includes three instance variables – a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app names EmployeeTest that demonstrates class EMLOYEE’s capabilities. Create two EMPLOYEE objects and display each object’s yearly...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
In C++ please Your class could have the following member functions and member variables. However, it's...
In C++ please Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented) bool empty(); //returns true if deck has no cards int cardIndex; //marks the index of the next card in the deck Card deck[52];// this is your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT