Question

In: Computer Science

Modify the Fraction class created previously as follows. Read the entire description (Code at the bottom...

Modify the Fraction class created previously as follows. Read the entire description (Code at the bottom also using C++ on visual studios)

  • 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

      Feel free to change the code if needed

      #include <iostream>
      #include <string>
      #include <iomanip>

      using namespace std;

      class Fraction {
      private:
      int numer;
      int denomin;

      public:
      int GetNumerator();
      int GetDenominator();
      Fraction();
      Fraction(int, int);
      void SetNumerator(int);
      void SetDenominator(int);
      void SetFraction(int, int);
      double GetFraction();
      string ToString();
      };


      Fraction::Fraction() {
      numer = 0;
      denomin = 1;
      }
      Fraction::Fraction(int numer, int denomin) {
      SetFraction(numer, denomin);
      }
      void Fraction::SetNumerator(int numer) {
      SetFraction(numer, this->denomin);
      }
      void Fraction::SetDenominator(int denomin) {
      SetFraction(this->numer, denomin);
      }
      int Fraction::GetNumerator() {
      return numer;
      }
      int Fraction::GetDenominator() {
      return denomin;
      }

      void Fraction::SetFraction(int numer, int denomin) {
      if (numer < 0 || denomin < 1) {
      this->numer = 0;
      this->denomin = 1;
      }
      else {
      this->numer = numer;
      this->denomin = denomin;
      }
      }

      double Fraction::GetFraction() {
      return (double)numer / (double)denomin;
      }

      string Fraction::ToString() {
      return to_string(numer) + "/" + to_string(denomin);
      }

      int main() {
      Fraction f(1, 2);
      cout << "Fraction: " << f.ToString() << "\n";
      cout << "Numerator: " << f.GetNumerator() << "\n";
      cout << "Denominator: " << f.GetDenominator() << "\n";
      cout << "Fraction value: " << f.GetFraction() << "\n";

      cout << "\n";

      f.SetNumerator(5);
      f.SetDenominator(3);

      cout << "Fraction: " << f.ToString() << "\n";
      cout << "Numerator: " << f.GetNumerator() << "\n";
      cout << "Denominator: " << f.GetDenominator() << "\n";
      cout << "Fraction value: " << f.GetFraction() << "\n";

      cout << "\n";

      f.SetFraction(4, 3);

      cout << "Fraction: " << f.ToString() << "\n";

      cout << "\n";

      f.SetNumerator(4);
      f.SetDenominator(3);

      cout << "Numerator: " << f.GetNumerator() << "\n";
      cout << "Denominator: " << f.GetDenominator() << "\n";
      cout << "Fraction value: " << f.GetFraction() << "\n";


      return 0;

      }

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>


using namespace std;

class Fraction {
private:
int numer;
int denomin;

public:
int GetNumerator();
int GetDenominator();

~Fraction();
Fraction();
Fraction(int);
Fraction(int, int);
Fraction(int, int, int);

// Copy constructor
Fraction(const Fraction &f) {
cout<<" copy constructor is calling\n";
numer = f.numer; denomin = f.denomin;
}


void SetNumerator(int);
void SetDenominator(int);

void SetFraction(int);
void SetFraction(int, int);
void SetFraction(int, int, int);

double GetFraction();

void PrintFraction();

string ToString();
};


Fraction::Fraction() {
numer = 0;
denomin = 1;

}

Fraction::Fraction(int number) {
cout<<"constructor is calling\n";
SetFraction(number);
}

Fraction::Fraction(int numer, int denomin) {
cout<<"constructor is calling\n";
SetFraction(numer, denomin);
}

Fraction::Fraction(int whole, int numer, int denomin) {
cout<<"constructor is calling\n";
SetFraction(whole, numer, denomin);
}

Fraction::~Fraction() {
cout<<"disctructor is calling\n";
numer = 0;
denomin = 1;
cout << "Pause! Press the Enter key to continue.\n";
system("pause");
//cin.get();
cout << "Resuming...\n";
}

void Fraction::SetNumerator(int numer) {
SetFraction(numer, this->denomin);
}
void Fraction::SetDenominator(int denomin) {
SetFraction(this->numer, denomin);
}
int Fraction::GetNumerator() {
return numer;
}
int Fraction::GetDenominator() {
return denomin;
}

void Fraction::SetFraction(int number) {

if(number < 0) {
cout<<"Only positive values allowed.\n";
this->numer = 0;
this->denomin = 1;
}
else {
this->numer = number;
this->denomin = number;
}
}
void Fraction::SetFraction(int numer, int denomin) {


if(denomin==0) {
cout<<"Denominator can be Zero in any fraction because division by zero is undefined.\n";
denomin = 1;
}

if(numer < 0 || denomin < 1) {
cout<<"Only positive values allowed.\n";
this->numer = 0;
this->denomin = 1;
}
else {
this->numer = numer;
this->denomin = denomin;
}
}
void Fraction::SetFraction(int whole, int numer, int denomin) {

if(denomin==0) {
cout<<"Denominator can be Zero in any fraction because division by zero is undefined.\n";
denomin = 1;
}

if(whole < 0 || numer < 0 || denomin < 1) {
cout<<"Only positive values allowed.\n";
this->numer = 0;
this->denomin = 1;
}else {

this->numer = whole*denomin + numer;
this->denomin = denomin;
}
}

double Fraction::GetFraction() {
return (double)numer / (double)denomin;
}

string Fraction::ToString() {
return to_string(numer) + "/" + to_string(denomin);
}

void Fraction::PrintFraction(){
cout<< "Fraction: " << ToString() <<"\n";
}

int main() {


Fraction f(1);
cout << "Fraction: " << f.ToString() << "\n";
cout << "Numerator: " << f.GetNumerator() << "\n";
cout << "Denominator: " << f.GetDenominator() << "\n";
cout << "Fraction value: " << f.GetFraction() << "\n";

cout << "\n";

Fraction f2(2,3,4);
cout << "Fraction: " << f2.ToString() << "\n";
cout << "Numerator: " << f2.GetNumerator() << "\n";
cout << "Denominator: " << f2.GetDenominator() << "\n";
cout << "Fraction value: " << f2.GetFraction() << "\n";
cout << "\n";

Fraction f3(f2);
f3.PrintFraction();
cout << "Numerator: " << f3.GetNumerator() << "\n";
cout << "Denominator: " << f3.GetDenominator() << "\n";
cout << "Fraction value: " << f3.GetFraction() << "\n";

cout << "\n";

f.SetNumerator(5);
f.SetDenominator(3);

cout << "Fraction: " << f.ToString() << "\n";
cout << "Numerator: " << f.GetNumerator() << "\n";
cout << "Denominator: " << f.GetDenominator() << "\n";
cout << "Fraction value: " << f.GetFraction() << "\n";

cout << "\n";

f.SetFraction(4, 3);

cout << "Fraction: " << f.ToString() << "\n";

cout << "\n";

f.SetNumerator(4);
f.SetDenominator(3);

cout << "Numerator: " << f.GetNumerator() << "\n";
cout << "Denominator: " << f.GetDenominator() << "\n";
cout << "Fraction value: " << f.GetFraction() << "\n";


return 0;

return 0;

}

Note: In linux we need use 'cin.get()' instead of system("pause") because it's works only in windows.


Related Solutions

c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire assignment. There are no more directions to it. Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make...
This is using Python, it is utilizing code from a Fraction class to create a Binary...
This is using Python, it is utilizing code from a Fraction class to create a Binary Class Please leave comments so I may be able to learn from this. Instruction for Binary Class: Exercise 6.18: Design an immutable class BinaryNumber, in the style of our Fraction class. Internally, your only instance variable should be a text string that represents the binary value, of the form '1110100'. Implement a constructor that takes a string parameter that specifies the original binary value....
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in. *NOTE*- I...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from the comments in the main routine that it wants an input argument of 3 to solve for a triangle and 4 to solve for a square or rectangle. MODIFY the code to add another method in the 'threeSides' and 'fourSides' classes to return the perimeter. ASSUME the following for TRIANGLES. ** for AREA (1/2 * base * height) dimension1 is base, dimension2 is height...
Read previously created binary file using ObjectInputStream Create an appropriate instance of HashMap from Java library...
Read previously created binary file using ObjectInputStream Create an appropriate instance of HashMap from Java library Populate the HashMap with data from binary file Display the information read in a user friendly format Problem: Given: Words.dat – a binary file consisting of all the words in War and Peace Goal: Read the words in from the binary file and figure out how many times each word appears in the file. Display the results to the user. Use a HashMap with...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of...
MATLAB ONLY please. Please put the entire code below. 1. you will read a list of internet logs from a notepad. 2. then you will extract the following. - a host (e.g., '146.204.224.152') - a user_name (e.g., 'feest6811' note: sometimes the username is missing! In this case, use '-' as the value for the username.) - the timme a request was made (e.g., '21/Jun/2019:15:45:24-0700') - the post request type (e.g., 'POST /incentivize HTTP/1.1' note: not everthing is a POST!) Your...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE,...
Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE Thank in Advance. Must do; The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor You must overload the assignment operator You must declare the overloaded output operator as a friend rather than part of...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT