In: Computer Science
Define a class Fraction3YourName as follows,
/**
* Program Name: Fraction3YourName.java
* Discussion: Fraction3Yourname class
* written By:
* Date: 2019/09/19
*/
public class Fraction3YourName {
private int sign;
private int num;
private int denom;
public Fraction3YourName() {
//sign = ;
//denom = ;
}
public Fraction3YourName(int n) {
//sign = ;
//num = n;
//denom = ;
}
public Fraction3YourName(int s, int n, int d) {
//sign = s;
//num = n;
//denom = d;
}
}
You are asked to
Complete the definitions for the given constructors; and
Provide additional method members to allow the performance of
four simple
arithmetic operations: (1) Addition, (2) Subtraction, (3)
Multiplication, and (4)
Division; and
Provide one method member print() to display the
Fraction3YourName object.
by java programming
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
public class Fraction3YourName {
private int sign;
private int num;
private int denom;
public Fraction3YourName() {
sign =1;
denom =1;
}
public Fraction3YourName(int n) {
sign =1;
num = n;
denom =1;
}
public Fraction3YourName(int s, int n, int d) {
sign = s;
num = n;
denom = d;
}
// add the fraction
public Fraction3YourName add(Fraction3YourName frac)
{
int denom = this.denom * frac.denom;
int num = (this.num * frac.denom) + (frac.num*this.denom);
int s=1;
if(num<0)
s=-1;
return new Fraction3YourName(s,num,denom);
}
// subtract the fraction
public Fraction3YourName subtract(Fraction3YourName frac)
{
int denom = this.denom * frac.denom;
int num = (this.num * frac.denom) - (frac.num*this.denom);
int s=1;
if(num<0)
s=-1;
return new Fraction3YourName(s,num,denom);
}
// Multiply the fraction
public Fraction3YourName multiply(Fraction3YourName frac)
{
int denom = this.denom * frac.denom;
int num = (this.num * frac.num);
int s=1;
if(num<0)
s=-1;
return new Fraction3YourName(s,num,denom);
}
// Divide the fraction
public Fraction3YourName divide(Fraction3YourName frac)
{
int denom = this.denom * frac.num;
int num = (this.num * frac.denom);
int s=1;
if(num<0)
s=-1;
return new Fraction3YourName(s,num,denom);
}
// print the fraction number
public void print()
{
System.out.println(num+"/"+denom);
}
public static void main(String[] args) {
// create a fraction class using constructor 3
Fraction3YourName f1=new Fraction3YourName(1,2,3);
// print the fraction
System.out.print("F1 is: ");
f1.print();
// create a fraction class using constructor 2
Fraction3YourName f2=new Fraction3YourName(3);
// print the fraction
System.out.print("F2 is: ");
f2.print();
// add the fractions and print the result
Fraction3YourName addedFraction= f1.add(f2);
System.out.print("F1+F2 is: ");
addedFraction.print();
// subtract the fractions and print the result
Fraction3YourName subtractedFraction= f1.subtract(f2);
System.out.print("F1-F2 is: ");
subtractedFraction.print();
// multiply the fractions and print the result
Fraction3YourName multipliedFraction= f1.multiply(f2);
System.out.print("F1*F2 is: ");
multipliedFraction.print();
// divide the fractions and print the result
Fraction3YourName dividedFraction= f1.divide(f2);
System.out.print("F1/F2 is: ");
dividedFraction.print();
}
}
==================
SCREENSHOTS:
OUTPUT:
======================
F1 is: 2/3
F2 is: 3/1
F1+F2 is: 11/3
F1-F2 is: -7/3
F1*F2 is: 6/3
F1/F2 is: 2/9
=======================