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...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
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...
Create a UEmployee class that contains member variables for the university employee name and salary. The...
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...
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...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the secret word char * secretWord; char *guessedWord; public: //please create related constructors, getters, setters,constructor() constructor() You will need to initialize all member variables including the two dynamic variables. destructor() Please deallocate/freeup(delete) the two dynamic arrays memories. guessALetter(char letter) 1.the function will check if the letter guessed is included in the word. 2. display the guessed word with related field(s) filled if the letter guessed...
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...
C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪...
C++ Program Create Semester class (.h and .cpp file) with the following three member variables: ▪ a semester name (Ex: Fall 2020) ▪ a Date instance for the start date of the semester ▪ a Date instance for the end date of the semester The Semester class should have the following member functions 1. Create a constructor that accepts three arguments: the semester name, the start Date, and the end Date. Use default values for all the parameters in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT