C++ Object Oriented assignment
Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given.
INSTRUCTIONS
Create a fraction class and add your Name to the name fraction and use this as your class. For Example, if your name is Mark John, then your class name should be FractionMarkJ.
Create a constructor for your fraction class to handle the initialization appropriately.
There will be ONLY one constructor with two arguments.
The constructor should have code to confirm the call/use of itself through the printing (e.g. “Calling Fraction ( int, int )” ) and continue with the display of numerator and denominator after being initialized/built.
There will be a member function print ( ) that will print the current Fraction object.
There will be a member function extractUncommonDigit ( ) that will return an array of common digits between the numerator and denominator.
IMPORTANT REMAINDERS!
In your program, no GLOBAL DATA are allowed, and you must write all needed functions (no library functions are allowed – Except for cin and cout and their functions/manipulators)
Dynamic allocations must be released properly!
Test your output with data to be identified in class ( at least the same as being shown below as appropriate.
Attach the output at the end of your application program (as a comment).
Followed with a comment block about your logic, code issues, and any other comments that you may have – YOU MUST PROVIDE – this comment block even with a “ NO COMMENTS!” as the content of this block.
SOURCE CODE is attached below.
#include<iostream>
using namespace std;
class Fraction {
private:
int num;
int deno;
public:
Fraction() {
num = -1;
deno = -1;
}
Fraction(int n,int d) {
num = n;
deno = d;
}
void operator =(Fraction f)
{
num = f.num;
deno = f.deno;
}
void print() {
cout<<"\n\tThe current fraction obj has : ";
cout<<"\n\tNum = "<<num<<endl;
cout<<"\tDeno = "<<deno<<endl;
}
void update(int n,int d) {
if(n != -1)
num = n;
if(d != -1)
num = d;
}
bool isEmpty() {
if(num == -1 || deno == -1)
return true;
else
return false;
}
int foundInDeno(int i) {
int d = deno;
int res = 0;
while(d>0) {
int j = d%10;
if(j == i)
res++;
d = d/10;
}
return res;
}
int foundInNum(int i) {
int d = num;
int res = 0;
while(d>0) {
int j = d%10;
if(j == i)
res++;
d = d/10;
}
return res;
}
void extractUncommonDigit() {
cout<<"\n\tUsing result return from extractUncommonDigit()";
int n = num;
int d = deno;
cout<<"\n\tFrom num ..";
while(n>0) {
int i = n%10;
if(foundInDeno(i) <= 0) {
cout<<"\n\t"<<i<<" (occurred "<<foundInNum(i)<<" time(s))";
}
n = n/10;
}
cout<<"\n\n\tFrom deno ..";
while(d>0) {
int i = d%10;
if(foundInNum(i) <= 0) {
cout<<"\n\t"<<i<<" (occurred "<<foundInDeno(i)<<" time(s))";
}
d = d/10;
}
}
};
int main() {
int input;
Fraction f;
do {
cout<<"\n\t************************************"<<endl;
cout<<"\t* Menu HW#3 *"<<endl;
cout<<"\t* (1) Creating/Updating Fraction *"<<endl;
cout<<"\t* (2) Display the fraction *"<<endl;
cout<<"\t* (3) Display uncommon digit(s) *"<<endl;
cout<<"\t* (4) Quit *"<<endl;
cout<<"\t************************************"<<endl;
cout<<"\tEnter your option(1 through 4) : ";
cin>>input;
switch(input) {
case 1:
if(f.isEmpty()) {
int n,d;
cout<<"\tEnter an int to numerator : ";
cin>>n;
cout<<"\tEnter an int to denominator : ";
cin>>d;
Fraction fl(n,d);
f = fl;
cout<<"\n\tA call to Fraction(int n,int d) was build a fraction.";
f.print();
} else {
int in;
do{
cout<<"\n\t************************************"<<endl;
cout<<"\t* Update MENU *"<<endl;
cout<<"\t* (1) updating num *"<<endl;
cout<<"\t* (2) updating deno *"<<endl;
cout<<"\t* (3) updating num & deno *"<<endl;
cout<<"\t* (4) return *"<<endl;
cout<<"\t************************************"<<endl;
cout<<"\tEnter your option(1 through 4) : ";
cin>>in;
int n = -1 ,d = -1;
switch(in) {
case 1 :
cout<<"\n\tEnter an int to numerator : ";
cin>>n;
break;
case 2 :
cout<<"\n\tEnter an int to denominator : ";
cin>>d;
break;
case 3 :
cout<<"\tEnter an int to numerator : ";
cin>>n;
cout<<"\tEnter an int to denominator : ";
cin>>d;
break;
case 4 :
cout<<"\n\tReturning to previous menu.";
break;
default:
cout<<"\tInvalid input."<<endl;
}
if(in != 4) {
f.update(n,d);
cout<<"\n\tAfter updating : ";
f.print();
}
}while(in!=4);
}
break;
case 2:
if(f.isEmpty()) {
cout<<"\tNo Fraction Available."<<endl;
} else {
f.print();
}
break;
case 3:
if(f.isEmpty()) {
cout<<"\tNo Fraction Available."<<endl;
} else {
f.extractUncommonDigit();
}
break;
case 4:
cout<<"\tHaving fun!"<<endl;;
break;
default:
cout<<"\tInvalid input."<<endl;
}
} while(input!=4);
}
c++ code:
#include<iostream>
using namespace std;
//Fraction class
class Fraction {
private:
//private variable
int num;
int deno;
public:
//constructor to initialize the variable
Fraction() {
num = -1;
deno = -1;
}
//constructor with parameter
Fraction(int n,int d) {
num = n;
deno = d;
}
//update numerator value
void updateNum(int n)
{
num = n;
}
//update denominator value
void updateDenom(int d)
{
deno = d;
}
//set the numerator and denominator values of one fraction value to
other using operator
void operator =(Fraction f)
{
num = f.num;
deno = f.deno;
}
//print the fraction
void print() {
cout<<"\n\tThe current fraction obj has : ";
cout<<"\n\tNum = "<<num<<endl;
cout<<"\tDeno = "<<deno<<endl;
}
//update both numerator and denominator values
void update(int n,int d) {
//if(n != -1)
num = n;
//if(d != -1)
num = d;
}
//check the fraction is empty or not
bool isEmpty() {
if(num == -1 || deno == -1)
return true;
else
return false;
}
//check the common numbers in denominator
int foundInDeno(int i) {
int d = deno;
int res = 0;
while(d>0) {
int j = d%10;
if(j == i)
res++;
d = d/10;
}
return res;
}
//check and return the common numbers in numerator
int foundInNum(int i) {
int d = num;
int res = 0;
while(d>0) {
int j = d%10;
if(j == i)
res++;
d = d/10;
}
return res;
}
//check the common digits in fractions
void extractUncommonDigit() {
cout<<"\n\tUsing result return from extractUncommonDigit()";
int n = num;
int d = deno;
cout<<"\n\tFrom num ..";
while(n>0) {
int i = n%10;
if(foundInDeno(i) > 0) {
cout<<"\n\t"<<i<<" (occurred "<<foundInNum(i)<<" time(s))";
}
n = n/10;
}
cout<<"\n\n\tFrom deno ..";
while(d>0) {
int i = d%10;
if(foundInNum(i) > 0) {
cout<<"\n\t"<<i<<" (occurred "<<foundInDeno(i)<<" time(s))";
}
d = d/10;
}
}
};
int main() {
int input;
//object for fraction class
Fraction f;
//do loop
do {
//print menu
cout<<"\n\t************************************"<<endl;
cout<<"\t* Menu HW#3 *"<<endl;
cout<<"\t* (1) Creating/Updating Fraction *"<<endl;
cout<<"\t* (2) Display the fraction *"<<endl;
cout<<"\t* (3) Display uncommon digit(s) *"<<endl;
cout<<"\t* (4) Quit *"<<endl;
cout<<"\t************************************"<<endl;
cout<<"\tEnter your option(1 through 4) : ";
cin>>input; //get user options
switch(input) {
case 1:
//if fractions is empty , set the numerator and denominator
values
if(f.isEmpty()) {
int n,d;
cout<<"\tEnter an int to numerator : ";
cin>>n;
cout<<"\tEnter an int to denominator : ";
cin>>d;
Fraction fl(n,d); //set values
f = fl;
cout<<"\n\tA call to Fraction(int n,int d) was build a fraction.";
f.print(); //print the fraction
} else { //else update the values
int in;
do{
cout<<"\n\t************************************"<<endl;
cout<<"\t* Update MENU *"<<endl;
cout<<"\t* (1) updating num *"<<endl;
cout<<"\t* (2) updating deno *"<<endl;
cout<<"\t* (3) updating num & deno *"<<endl;
cout<<"\t* (4) return *"<<endl;
cout<<"\t************************************"<<endl;
cout<<"\tEnter your option(1 through 4) : ";
cin>>in;
int n = 0 ,d = 1;
switch(in) {
case 1 :
cout<<"\n\tEnter an int to numerator : ";
cin>>n;
f.updateNum(n); //update numerator alone
break;
case 2 :
cout<<"\n\tEnter an int to denominator : ";
cin>>d;
f.updateDenom(d); //update denominator alone
break;
case 3 :
cout<<"\tEnter an int to numerator : ";
cin>>n;
cout<<"\tEnter an int to denominator : ";
cin>>d;
f.update(n,d); //update numerator and denominator
break;
case 4 :
cout<<"\n\tReturning to previous menu.";
break;
default:
cout<<"\tInvalid input."<<endl;
}
if(in != 4) {
cout<<"\n\tAfter updating : ";
f.print(); //print after updating
}
}while(in!=4);
}
break;
case 2:
if(f.isEmpty()) { //if fraction is empty,print no fraction
cout<<"\tNo Fraction Available."<<endl;
} else {
f.print();
}
break;
case 3:
if(f.isEmpty()) {
cout<<"\tNo Fraction Available."<<endl;
} else {
f.extractUncommonDigit(); //print the common digits
}
break;
case 4:
cout<<"\tHaving fun!"<<endl;//quit
break;
default:
cout<<"\tInvalid input."<<endl;
}
} while(input!=4);
}
output:
//modification i have done in your code :
1.update numerator, and update denominator values -> i have update the numerator and denominator alone values
2.extract common digits, -> i have modified the function, now it will print the common digits in both num and deno
for any clarification, please do comments
C++ Object Oriented assignment Can you please check the program written below if it has appropriately...
Redo Programming Exercise 7 of Chapter 7 so that your program handles exceptions such as division by zero and invalid input. Your program should print Denominator must be nonzero and reprompt for a valid denominator when 0 is entered for a denominator. Please specify what should go in the divisionByZero.h file and the changes made to main.cpp Please provide output. Thank you in advance! main.cpp so far #include <iostream> using namespace std; void addFractions(int num1, int num2, int den1, int...
Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...
This question is about C++! Honestly, I'm not sure what I am doing right or wrong. Using just functions, nothing else, I am trying to create a program that uses nothing but functions to take in two fractions, add these fractions, divide these fractions, and then display the simplified fraction all from within a menu. I would very much appreciate any leads, aid, or description on what I've done right and wrong. Thanks! #include <iostream> using namespace std; //Description: Function...
C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
I need to do object oriented programming for c++. I had to make make a program where it would add,subtract,multiply,and divide fraction. I got that working, but it wont work for negative can anyone fix it and explain how they did it? Source.cpp code: #include "Fraction.h" #include <iostream> using namespace std; int main() { Fraction f1(-4, 6); Fraction f2(5, -9); Fraction sum = sum.add(f1, f2); sum.print(sum); Fraction diff = diff.subtract(f1, f2); diff.print(diff); ...
c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...
NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...