Some reasons for point deductions % Reason Completely and correctly solved exerc
ID: 3757191 • Letter: S
Question
Some reasons for point deductions % Reason Completely and correctly solved exercises result in 100% points Exercise incorrectexercise missing Use a single file/directory for each exercise Keep backups of functioning versions 10% Function: example use missing 20% Function:contract missing 20% Function exercise description missing 80% Function function missing (incorrect 80% Function tests missing Be prepared to discuss (and defend) your solutions This exercise is mainly concerned with understanding how program flow works in object-oriented programs, i.e. object interaction based on messages. Each class must be separated into a header and source file. Tests and the main program must also be in their own source files. Use the format skeleton from exercise collection 1 as a template. Use the conat and constexpr keywords correctly. To emulate transfers between bank accounts the functions of an ATM have been analyzed and the following classes identified account, database, and interaction. An account should have attributes for account number (id_), a secret personal identifi- cation number (pin), and an account balance (balance) Exercise 2.1 Implement the methods account: :pay in and account: :pay out. Money values should be represented with a floating-point number (float). A strict separation between pay-in and pay-out is required, which means the argument for both functions cannot be negative. If a method fails, the object state shall be unchanged and the failure signaled by a return value of false. 4.5pt Exercise 2.2 Test account: :pay in and account: :pay.out by implementing a test program, ie. a sain function in a separate source file. 1.5pt Exercise 2.3 Implement a freestanding function transfer. Use the methods from exercise 2.1. The function declaration should be located near the class definition of account Hint: The arguments of the transfer function follow the source/sink semantic in the ordering from-to. 3pt Exercise 2.4 Implement a database that contains a list of accounts The number of accounts should be unlimited, in principle, but at the current time a simplified (prototype) solution is of more interest (see source code below). The account class must be extended to fulfill the requirement of default-constructible. Also extend account with a valid attribute because initialized accounts usually do not contain valid data. 3pt] A possible simplified version of database: class database f public: // .tars d.tor database C) size (O) public: // methodsExplanation / Answer
ANSWER :
// File Name: Rational.h
#ifndef Rational_H
#define Rational_H
#include <iostream>
using namespace std;
// Class Rational definition
class Rational
{
public:
// Data member for numerator and denominator
int numerator;
int denominator;
// Default constructor
Rational();
// Input redirection operator overloaded
friend ostream& operator<<(ostream &os, const Rational &f);
// Output redirection operator overloaded
friend istream& operator >> (istream &is, Rational &f);
// Overloading + operator
// Addition operator overloaded
Rational operator + (Rational f);
// Overloading - operator
// Subtraction operator overloaded
Rational operator - (Rational f);
// Overloading * operator
// Function to perform multiplication operation and return the result object
Rational operator * (Rational f);
// Overloading / operator
// Function to return the object after division
Rational operator / (Rational f);
// Overloading > operator
//Returns true if both numerator and denominator cross multiplication is greater
bool operator > (Rational f);
// Overloading < operator
//Returns true if both numerator and denominator cross multiplication is less
bool operator < (Rational f);
// Overloading = operator
// Returns the object after assignment
bool operator == (Rational f);
// Function to minimize
void reduce();
};// End of class
#endif
-----------------------------------------------------------------------------------------
#include "Rational.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
// Default constructor
Rational::Rational()
{
numerator = denominator = 1;
}// End of default constructor
// Input redirection operator overloaded
ostream& operator<<(ostream &os, const Rational &f)
{
// Stores numerator, operator symbol (/) and denominator in os stream
os<<f.numerator<<" / " <<f.denominator;
// Returns output stream
return os;
}// End of function
// Output redirection operator overloaded
istream& operator >> (istream &is, Rational &f)
{
// To store symbol
char ch;
// Stores numerator in is stream
is>>f.numerator;
// To discard fraction symbol
is>>ch;
// Stores denominator in is stream
is>>f.denominator;
// Returns input stream
return is;
}// End of function
// Overloading + operator
// Addition operator overloaded
Rational Rational::operator + (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Add both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator + f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function
// Overloading - operator
// Subtraction operator overloaded
Rational Rational::operator - (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Subtracts both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator - f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function
// Overloading * operator
// Function to perform multiplication operation and return the result object
Rational Rational::operator * (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Multiplies the implicit object numerator with parameter object denominator and stores it in temporary object numerator
t.numerator = numerator * f.numerator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function
// Overloading / operator
// Function to return the object after division
Rational Rational::operator / (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Checks for divide by zero error
if(f.denominator == 0 || denominator == 0)
{
cout<<" Cannot divide by zero";
exit(0);
}// End of if condition
// Otherwise denominator is not zero
else
{
// Multiply implicit object numerator with parameter object denominator and store it in the temporary object numerator
t.numerator = numerator * f.denominator;
// Multiply parameter object numerator with implicit object denominator and store it in the temporary object denominator
t.denominator = f.numerator * denominator;
// Return the result object
return t;
}// End of else
}// End of function
// Overloading > operator
// Returns true if both numerator and denominator cross multiplication is greater
bool Rational::operator > (Rational f)
{
return (numerator * f.denominator > denominator * f.numerator);
}// End of function
// Overloading < operator
// Returns true if both numerator and denominator cross multiplication is less
bool Rational::operator < (Rational f)
{
return (numerator * f.denominator < denominator * f.numerator);
}// End of function
// Overloading = operator
// Returns the object after assignment
bool Rational::operator == (Rational f)
{
if((numerator == f.numerator) && (denominator == f.denominator))
return true;
else
return false;
}// End of function
// Function to minimize
void Rational::reduce()
{
// Loops from 2 to denominator value
for(int x = 2; x < denominator; x++)
{
// Checks if x is divisible by both numerator and denominator
if((numerator % x == 0) && (denominator % x == 0))
{
// Divide numerator by x and store the quotient in numerator
numerator = numerator / x;
// Divide denominator by x and store the quotient in denominator
denominator = denominator / x;
}// End of if condition
}// End of for loop
// Displays the result
cout<<numerator<<"/"<<denominator;
}// End of function
// Displays the menu for Operator
void selectOperator()
{
cout<<" + for Addition - for Subtraction * for Multiplication / for Division
> - for greater than < for less than == for check equals $ - for exit.";
}// End of function
// Main method
int main()
{
// To store operator symbol
string operatorSymbol;
// Creates two Rational class object
Rational f1, f2;
// Creates an object to store result
Rational t;
// Loops till operator symbol is not "$"
do
{
// Displays the menu for operator symbol
selectOperator();
// Accepts the symbol
cout<<" Enter the expression symbol: ";
cin>>operatorSymbol;
// Checks if operator symbol is "$" exit the program
if(operatorSymbol == "$")
exit(0);
cout<<" Example First fraction 2/3 + second fraction 3/2 ";
// Accepts first fraction
cout<<"Enter an First Fraction: ";
cin>>f1;
// Accepts second fraction
cout<<" Enter an Second Fraction: ";
cin>>f2;
// Checks if the operator symbol is "+"
if(operatorSymbol == "+")
{
// Overloads the + operator to perform addition and store the result in t
t = f1 + f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is "-"
else if(operatorSymbol == "-")
{
// Overloads the - operator to perform subtraction and store the result in t
t = f1 - f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is "*"
else if(operatorSymbol == "*")
{
// Overloads the * operator to perform multiplication and store the result in t
t = f1 * f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is "/"
else if(operatorSymbol == "/")
{
// Overloads the / operator to perform division and store the result in t
t = f1 / f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is ">"
else if(operatorSymbol == ">")
{
// Overloads the > operator to check if object f1 is greater than f2
if(f1 > f2)
// If true display the result
cout<<" "<<f1<<" is greater than "<<f2;
// Otherwise f1 is not greater than f2
else
// If false display the result
cout<<" "<<f2<<" is greater than "<<f1;
}// End of if condition
// Otherwise checks if the operator symbol is "<"
else if(operatorSymbol == "<")
{
// Overloads the < operator to check if object f1 is less than f2
if(f1 < f2)
// If true display the result
cout<<" "<<f1<<" is smaller than "<<f2;
// Otherwise f1 is not less than f2
else
// If false display the result
cout<<" "<<f2<<" is smaller than "<<f1;
}// End of if condition
// Otherwise checks if the operator symbol is "=="
else if(operatorSymbol == "==")
{
// Overloads the == operator to check if object f1 is equals to f2
if(f1 == f2)
// If true display the result
cout<<f1<<" is equals to "<<f2;
// Otherwise f1 is not equals to f2
else
// If false display the result
cout<<f1<<" is not to "<<f2;
}// End of if condition
// Otherwise display invalid symbol
else
cout<<" Invalid symbol";
}while(1); // End of do while
}//End of main
Sample Output:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: +
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 12/2
Enter an Second Fraction: 10/3
12 / 2 + 10 / 3 = 56 / 6
Reduced Fraction -> 28/3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: -
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 18/3
Enter an Second Fraction: 4/8
18 / 3 - 4 / 8 = 132 / 24
Reduced Fraction -> 22/4
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: *
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 1/2
Enter an Second Fraction: 3/4
1 / 2 * 3 / 4 = 3 / 8
Reduced Fraction -> 3/8
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: /
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 18/2
Enter an Second Fraction: 6/3
18 / 2 / 6 / 3 = 54 / 12
Reduced Fraction -> 9/2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: >
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 8/2
Enter an Second Fraction: 6/1
6 / 1 is greater than 8 / 2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: <
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 12/3
Enter an Second Fraction: 2/7
2 / 7 is smaller than 12 / 3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: ==
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 1/2
Enter an Second Fraction: 1/2
1 / 2 is equals to 1 / 2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: ==
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 3/2
Enter an Second Fraction: 1/3
3 / 2 is not to 1 / 3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: $
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.