Implement examples of naming variables, functions, and classes using 3 different
ID: 3838758 • Letter: I
Question
Implement examples of naming variables, functions, and classes using 3 different languages.
Your program must include examples of:
1. global variable(s)
2. function(s) - show how local, nonlocal, and global variables can work together
3. class(es) -
a. some fields.
b. some methods
Your program must use all of the above examples.
You must write your program in 3 different languages.
One language must have a complete working and tested implementation,
while the other two should be as close to working as you can make
them.
You must choose one language from the following three groups:
Group 1:
1. Java
2. C++
3. C
Group 2:
1. python
2. javascript
Group 3:
any other language not in group 1 or group 2
You are encouraged, but not required, to choose
a functional programming language for your
third language.
3. Ruby
------------------------------------------------------------------------------------------------------------
What I so far:
This program in C++ should tell me the
amount of pie left (in fraction)
after a certain amount has been taken off.
Use a fraction that has numerator and denominator
DESIGN:
I will have a class called Fraction
contains integers numerator and numerator
that will call a function gcd
Another function called simplify that lowers
the fraction into simplest form by calling the
gcd to determine the gcd of the denominator.
a function called add fraction subtracts the amount
taken off and return the answer as the remainder of pie I have left
--------------------------------------------------------------------------
C++ code:
using namespace std;
// Required Class
class Fraction
{
private:
int numerator;
int denominator;
double pieTaken;
double remainPie;
public:
//public methods
Fraction();
double simplify();
int gcd(int num, int denom);
void setValue(int num, int denom);
};
// Constructor to assign default values to the data members
Fraction::Fraction(){
pieTaken = 0.0;
remainPie = 100.0;
}
// simplify() function to lower the fraction into simplest form
double Fraction:: simplify(){
double frac = 0.0;
int divisor = gcd(numerator, denominator);
numerator = numerator / divisor;
denominator = denominator / divisor;
frac = (double)numerator / (double)denominator;
return frac;
}
// gcd() function to calculate the greatest common divisor using EUCLIDEAN ALGORITHM
int Fraction::gcd(int num, int denom){
int greatDiv; // To store the greatest common divisor
int rem;
if (num == 0)
greatDiv = denom;
else if (denom == 0)
greatDiv = num;
else{
rem = num % denom;
greatDiv = gcd(denom, rem);
}
return greatDiv;
}
// setValue() function to assign user given values to numerator and denominator and calculate remainPie.
void Fraction:: setValue(int num, int denom){
numerator = num;
denominator = denom;
pieTaken = simplify();
cout << " Portion of the Pie taken out: " << pieTaken;
cout << endl;
if (pieTaken > remainPie){
cout << "You are trying to take our more than what is available!";
exit(0);
}
else{
remainPie = remainPie - pieTaken;
cout << "Remaining Pie: " << remainPie;
cout << endl;
}
}
int main() {
bool isValid = true;
int num, denom;
Fraction frac;
do{
// Get values for numerator and denominator
cout << " Enter the numerator value of fraction: ";
cin >> num;
cout << "Enter the denominator value of fraction: ";
cin >> denom;
if (num < 0 || denom < 0){ // If either value is negative
isValid = false;
cout << " The numerator and denominator must be some positive values. Please try again!";
cout << endl;
}
if (denom == 0){ // If denominator is 0
isValid = false;
cout << " Denominator cannot be 0! Please try again!";
cout << endl;
}
cout << endl;
}while(isValid);
frac.setValue(num, denom);
cout << endl;
return 0;
}
Please help review this and do it in Javascript and Ruby for me. Thanks
Explanation / Answer
Solution:
Run in JsFiddle
HTML:
<title>Fraction</title>
<script src="script.js"></script>
<body>
</body>
JAVASCRIPT
var fraction = new function() {
this.type = "macintosh";
this.color = "red";
this.numerator=12
this.denominator=4;
this. pieTaken=0;
this.remainPie=100;
this.getInfo = function () {
return this.color + ' ' + this.type + ' apple'+this.numerator;
};
this.gcd=function(num,denom){
var greatDiv;
if(num==0)
greatDiv=denom;
else if(denom==0)
greatDiv=num;
else
{
rem=num%denom;
greatDiv=arguments.callee(denom,rem);
}
return greatDiv;
};
this.simplify=function()
{
var frac=0.0;
var div=this.gcd(this.numerator,this.denominator);
this.numerator=this.numerator/div;
this.denominator=this.denominator/div;
frac=this.numerator/this.denominator;
return frac;
};
this.setValue=function(num,denom)
{
this.numerator = num;
this.denominator = denom;
this.pieTaken = this.simplify();
this.remainPie = this.remainPie -this. pieTaken;
res=this.remainPie.toString()
return res
};
}
alert(fraction.setValue(12,4));
C++ Code:
#include<iostream>
using namespace std;
// Required Class
class Fraction
{
private:
int numerator;
int denominator;
double pieTaken;
double remainPie;
public:
//public methods
Fraction();
double simplify();
int gcd(int num, int denom);
void setValue(int num, int denom);
};
// Constructor to assign default values to the data members
Fraction::Fraction(){
pieTaken = 0.0;
remainPie = 100.0;
}
// simplify() function to lower the fraction into simplest form
double Fraction:: simplify(){
double frac = 0.0;
int divisor = gcd(numerator, denominator);
numerator = numerator / divisor;
denominator = denominator / divisor;
frac = (double)numerator / (double)denominator;
return frac;
}
// gcd() function to calculate the greatest common divisor using EUCLIDEAN ALGORITHM
int Fraction::gcd(int num, int denom){
int greatDiv; // To store the greatest common divisor
int rem;
if (num == 0)
greatDiv = denom;
else if (denom == 0)
greatDiv = num;
else{
rem = num % denom;
greatDiv = gcd(denom, rem);
}
return greatDiv;
}
// setValue() function to assign user given values to numerator and denominator and calculate remainPie.
void Fraction:: setValue(int num, int denom){
numerator = num;
denominator = denom;
pieTaken = simplify();
cout << " Portion of the Pie taken out: " << pieTaken;
cout << endl;
if (pieTaken > remainPie){
cout << "You are trying to take our more than what is available!";
exit(0);
}
else{
remainPie = remainPie - pieTaken;
cout << "Remaining Pie: " << remainPie;
cout << endl;
}
}
int main() {
bool isValid = true;
int num, denom;
Fraction frac;
do{
isValid=true;
// Get values for numerator and denominator
cout << " Enter the numerator value of fraction: ";
cin >> num;
cout << "Enter the denominator value of fraction: ";
cin >> denom;
if (num < 0 || denom < 0){ // If either value is negative
isValid = false;
cout << " The numerator and denominator must be some positive values. Please try again!";
cout << endl;
}
if (denom == 0){ // If denominator is 0
isValid = false;
cout << " Denominator cannot be 0! Please try again!";
cout << endl;
}
cout << endl;
}while(!isValid);
frac.setValue(num, denom);
cout << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.