Question

In: Computer Science

Project 1 Fractional Number Class Part-1 for Five groups of member functions: Constructors (inc. string constructor)...

Project 1 Fractional Number Class

Part-1 for Five groups of member functions:

  • Constructors (inc. string constructor)
  • Getter/setters
  • Math
  • Type cast
  • Friend << and >>

Part-2 for the explanation for the behavior observed by running the provided test pattern:

six (6) operations in one statement vs. six (6) operations in six (6) statements.

PROJECT 1 Frac Class

The following information, located on Github m03, are provided as a starter.

  1. Frac.h, a complete Frac Class Declaration ( the definition is to be complete by you.)
  2. testFrac_starter.cpp, a working program with partial Frac members defined to get you started.

Please follow the TODO List below to finish all Class definitions, and to expand the starter to exercise and verify all the Frac member methods.

What is the number Type?

In our daily life we use the Quantitative Numbers to represent the physical world, such as:

  • Time or Angle in HMS (Hour-Minute-Second),
  • Length in Feet-Inches,
  • Floating Point Number in Mantissa and Exponent

For example, a floating number

123.45 = 1.2345 × 10+2 (for 10-based)

  • Mantissa: 1.2345 (normalized significant part of the value)
  • Exponent: -2

The floating point number construction and operations are pretty complicated but using the floating point number type in C++ is pretty straight forward. Since all the commonly used floating number operators are defined (overloaded) to have the same look and feel like an integer data type.

What is a Fractional number?

The fractional numbers are figured out by two whole numbers (the fraction terms) that are separated by a horizontal line (the fraction line). The number above the line (the numerator) can be every whole number and the number below the line (the denominator) should be different from zero.

Here are some examples of some fractions: 3/4, 5/4,...

Project Requirement

The form and format the Fractional number to be used

In this project, We are only going to

(1) use two numbers: numerator over denominator to represent a fraction, no mixed numerals.

(2) allow the usage of the proper and improper fraction:

  • Proper Fraction: the number is inferior to the denominator, for instance 3/4 ;
  • Improper fraction: the numerator is superior to the denominator, for instance 9/2 ;

We are not going to use Mixed Fraction and Equivalent Fractions:

  • Mixed Fraction or Mixed Numeral: it is composed of a whole part and a fractional one, for instance 2 1/3 ;
  • Equivalent Fractions: fractions that keep on the same proportion of another fraction, for instance: 5/2 =10/4; All Fraction shall be the minimum number representation.

TODO List

To complete the definition of all Five (5) Required Frac class method groups:

1. Constructors: * you may combine the first 3 contructors below into one.

  • default: numerator default as 0, denominator default as 1
  • 1 argument: (int numerator) numerator only, denominator default as 1
  • 2 arguments: (int numerator, int denominator)
  • string constructor: (string s); where s in the format of "3/4"

2. Necessary getter and setters

3. Support the following operators:

  • basic math: +, -, *. /
  • pre and postfix ++ and --
  • comparators: <, >, <=, >=, !=, ==

4. Type conversion operators, for Frac numbers to work with integers and fractional numbers:

int a = 1;
float b = 2.2; 
Frac f, f2;
f = b;
f2 = f-a;

5. overload friend iostream insertion <<, and extraction >> operator

After successfully completed the definitions of the above 5 groups of methods:

  1. include the Special ++/-- Test Pattern (inside the testFrac_starter.cpp) as part of your final test program.

After successfully implemented and executed your program:

  1. Final Check List
    • To complete all method's definition based on the following member method declaration of the Frac Class for Project 1. (the declaration only Frac.h in Github m03 folder)
    • Complete the testFrac.cpp program (testFrac_starter.cpp is provided in Github). Please expand the tester to make sure all the Frac member methods are verified with your own tester.
    • Make sure no error or any unverified member methods (point deduction)
    • Provide the reason/explanation on why the increment/decrement test sequence (one statement vs 6 statements) produce different outcome.

The GitHub m03 is below:

Frac.h (header)

// Function Prototypes for Overloaded Stream Operators
// Forward declaration needs to be filled
// if multifiles are used, make sure to place the inclusion guard.
#include <iostream>
class Frac;
ostream &operator << (ostream &, const Frac &);
istream &operator >> (istream &, Frac &);
class Frac {
private:
int num, den;
long gcd(long a, long b)
Frac lowterms(Frac &f);
public:
Frac();
Frac(string s);
Frac(int num_, int den_);
Frac(const Frac& rhs);
Frac operator=(const Frac& rhs);
// math + - * must be minimum term, i.e. no 2/8
Frac operator + (Frac &rhs);
Frac operator - (Frac &rhs);
Frac operator * (Frac &rhs);
Frac operator / (Frac &rhs);
// increment ++ decrement --
Frac operator++();
Frac operator++(int);
Frac operator--();
Frac operator--(int);
// comparators
bool operator == (Frac &f2);
bool operator != (Frac &f2);
bool operator < (Frac &f2);
bool operator > (Frac &rhs);
bool operator <= (Frac &f2);
bool operator >= (Frac &f2);
// overloading >> << stream operators
friend istream& operator>>(istream& strm, Frac& f);
friend ostream& operator<<(ostream& strm,const Frac& f) {
};

testFrac_starter.cpp (file)

/////////////////////////////////////////////////////////////
// This starter is a one-file all inclusive test appliation.
// This starter combines with the necessary Frac definition.
// Do not re-include the Frac.h
// If you prefer a multiple file approach, separte them cleanly.
/////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <sstream> // stringstream
using namespace std;
class Frac;
ostream &operator<< (ostream &stm, Frac const &rhs);
class Frac {
long num;
long den;
public:
// Frac() { num=0; den=1; }
Frac() : num(0), den(1) {}
Frac(long n, long d) {num=n; den=d;}
Frac(const Frac &obj) {*this = obj;}
void operator=(const Frac &rhs) {
num = rhs.num; den = rhs.den;
} // Frac a = b;
// string constructor is challenging, worth 2 pts, try your best!
Frac(string s);
// math operators
Frac operator+(const Frac &rhs) {
Frac temp;
temp.num = num*rhs.den + rhs.num*den;
temp.den = den*rhs.den;
// need to apply lower term by / gcd here
return temp;
}
// postfix increment operator ++ --
Frac operator++() {
num += den;
// lowterms(*this);
return *this; }
Frac operator--() {
num -= den;
// lowterms(*this);
return *this; }
// overload ostream insertion operator<<
friend ostream &operator<< (ostream &stm, Frac const &rhs) {
stm << rhs.num << "/" << rhs.den; }
};
int main() {
Frac x(3,4);
Frac y(1,2);
cout << "\nCreated Frac x(3,4) as " << x;
cout << "\nCreated Frac y(1,2) as " << y;
// Turn on this one when you completed the definition of string constructor
// Frac s("6/7"); // passing a Frac number as a string.
// cout << "\nString constructed: s: " << s;
Frac z(x);
cout << "\nCopy constructed z as x: " << z;
Frac v = x + y;
cout << "\nx + y is: " << v;
cout << "\nPlease observe the outputs of identical commands\n"
<< " executed in one statement v. separated statements. \n";
Frac f(5,6);
cout << f << " " // start
<< --f << " "
<< f << " "
<< ++f << " "
<< --f << " "
<< ++f << endl; // end of one statement
cout << f << " " ;
cout << --f << " " ;
cout << f << " " ;
cout << ++f << " " ;
cout << --f << " " ;
cout << ++f;
cout << "\nWhy the above output values are not the same?\n\n";
}

thank you

Solutions

Expert Solution

Note : I have done implementations for most of the functions.I have doubt regarding post and preincrementations of the fraction .I will browse and complete the remaining functions.Need some time.Thank u

______________________

// This starter is a one-file all inclusive test appliation.
// This starter combines with the necessary Frac definition.
// Do not re-include the Frac.h
// If you prefer a multiple file approach, separte them cleanly.
/////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <sstream> // stringstream
using namespace std;
#include "Frac.h"

long Frac::gcd(long a, long b)
{
  
}
Frac Frac::lowterms(Frac &f)
{
  
}
Frac::Frac()
{
this->num=0;
this->den=1;

}
Frac::Frac(string s)
{
  
}
Frac::Frac(int num_, int den_)
{
this->num=num_;
this->den=den_;
}
Frac::Frac(const Frac& rhs)
{
  
}
Frac::Frac operator=(const Frac& rhs)
{
  
}
// math + - * must be minimum term, i.e. no 2/8
Frac::Frac operator+(Frac &rhs)
{
  
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int sumnumer = (a * d + b * c);
int denom = (b * d);
  
Frac frac(sumnumer,denom);
return frac;

}
Frac::Frac operator-(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int sumnumer = (a * d - b * c);
int denom = (b * d);
  
Frac frac(sumnumer,denom);
return frac;
}
Frac::Frac operator*(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int mulnumer = (a * c);
int muldenom = (b * d);

Frac frac(mulnumer,muldenom);
return frac;
}
Frac::Frac operator/(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int divnumer = (a * d);
int divdenom = (c * b);
Frac frac(divnumer,divdenom);
return frac;
}
// increment ++ decrement --
Frac::Frac operator++()
{
  
}
Frac::Frac operator++(int)
{
  
}
Frac::Frac operator--()
{
  
}
Frac::Frac operator--(int)
{
  
}
// comparators
bool Frac::operator==(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;

}
bool Frac::operator!=(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;

double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;

}
bool Frac::operator<(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;

}
bool Frac::operator>(Frac &rhs)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;

if ((n1 / d1) > (n2 / d2))
return true;
else
return false;

}
bool Frac::operator<=(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;

}
bool Frac::operator>=(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;

}
// overloading >> << stream operators
istream& operator>>(istream& strm, Frac& f)
{
  
}
ostream& operator<<(ostream& strm,const Frac& f)
{
strm<< f.num << "/" << f.den;
return strm;
}

________________________


Related Solutions

The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method 2: badFriend - return string value, "I don't like you!" Main Method: - create new Friend object - call goodFriend and badFriend using Friend object /* class header */ { /* constructor method header */ { } public static void main (String [] args) { Friend f = /* new Friend object */ // call goodFriend using Friend object // call badFriend using Friend...
Create a student class that stores name, registration number and percentage, grade. Add member functions -...
Create a student class that stores name, registration number and percentage, grade. Add member functions - Read( string n, int r, float p): Read function that accepts parameter - Display(): To display student’s information - CalculateGrade()
Give a C++ class declaration called SavingsAccount with the following information: Operations (Member Functions) 1. Open...
Give a C++ class declaration called SavingsAccount with the following information: Operations (Member Functions) 1. Open account (with an initial deposit). This is called to put initial values in dollars and cents. 2. Make a deposit. A function that will add value to dollars and cents 3. Make a withdrawal. A function that will subtract values from dollars and cents. 4. Show current balance. A function that will print dollars and cents. Data (Member Data) 1. dollars 2. cents Operations...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List...
Create a new project ‘Clocky’ - Create a ‘ClockAlarm’ class Include 1 member – a List alarmTimes Create an accessor and mutator Create a method addAlarmTime which adds an alarm time to your list Create a method deleteAlarmTIme which removes an alarm time from your list Create a method – displayAlarmTimes which prints all alarm times in the list - Create a ‘Clock’ class - include at least 2 members - one member of Clock class needs to be private...
In class need to define two fields: 1. Integer part of the number - ulong; &...
In class need to define two fields: 1. Integer part of the number - ulong; & fractional part of the number - ushort in C# Using C#, create the following methods: 1. Method for adding two prices (parameters for function is 2 numbers, integer(main currency) and fractional parts(Fractional currency)) 2. Method for adding two prices(parameter is object of same type as your class) 3. Method for adding two prices(parameter is string)
ANOVA Part 1—A study compared five groups with six observations per group. An F statistic of...
ANOVA Part 1—A study compared five groups with six observations per group. An F statistic of 5.13 was reported. A.) Give the degrees of freedom for this statistic and be specific about which part you are referencing, numerator or denominator. B.) Find the P-value associated with this F-statistic. Show your calculator command in the space provided. Part 2—A study to compare four groups took random samples from each population of interest of sizes 38, 42, 21, and 45. The corresponding...
ACC-112 Plass Project ACC-112 Student Class Project Part 1 1. Record each of the following transactions...
ACC-112 Plass Project ACC-112 Student Class Project Part 1 1. Record each of the following transactions in the journal. Explanations are not required. Dec. 01, 2019 Dec. 02 Paid $1,000 cash for a four-month insurance policy. This policy begins December 1st. Dec. 03 Paid $600 cash for office supplies. Dec. 05 Performed delivery services for a customer and received $2,000 cash. Dec. 07 Completed a delivery job, billed the customer for $2,500 and received a promise to collect the $2,500...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT