SlideShare a Scribd company logo
1 of 10
Download to read offline
Write a program that works with fractions. You are first to implement three methods, each to
perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of
these methods, you are supplied two fractions as arguments, each a two-element array (the
numerator is at index 0, the denominator is at index 1), and you are to return a resulting,
simplified fraction as a new two-element array (again, with the numerator at index 0, and
denominator at index 1). You have been provide an add method as an example. You must
compute the resulting fraction using fraction-based math (working with numerators and
denominators) – do not convert the fractions to double values (like 1.5), do the math, and convert
back to a fraction. You have been provided a method to simplify a fraction using the gcd method
from the previous lab.
Once the operation methods are complete and pass the JUnit tests, now focus your attention on
the main method. You first need to input the two fractions from the keyboard (numerator then
denominator for each; you can assume integers) as well as one of the four valid operations (+, -,
*, /). Then validate the inputs: make sure a valid operation was input, make sure neither of the
denominators are zero, and make sure that the numerator of the second fraction isn’t zero if the
operation is division (error messages have been provided for each of these situations). Finally,
compute the result of the operation and output the answer. Note that if the denominator of the
answer is 1, you should just output the numerator (this includes if the answer is 0).
Here is the outline code given:
public class LA5a {
/**
* Error to output if either denominator is zero
*/
static final String E_DEN_ZERO = "Denominator cannot be zero.";
/**
* Error to output if dividing by zero
*/
static final String E_DIV_ZERO = "Cannot divide by zero.";
/**
* Error to output if the operation is invalid
*/
static final String E_OP_INVALID = "Invalid operation.";
/**
* Returns the greatest common divisor (gcd) of two integers
*
* @param num1 integer 1
* @param num2 integer 2
* @return gcd of integers 1 and 2
*/
public static int gcd(int num1, int num2) {
int t;
while (num2 != 0) {
t = num2;
num2 = num1 % num2;
num1 = t;
}
return num1;
}
/**
* Returns the simplified form of a fraction
*
* @param f fraction (numerator=[0], denominator=[1])
* @return simplified fraction (numerator=[0], denominator=[1])
*/
public static int[] simplifyFraction(int[] f) {
final int gcd = gcd(f[0], f[1]);
int[] result = {f[0]/gcd, f[1]/gcd};
if ((result[0]<0 && result[1]<0) || (result[1]<0)) {
result[0] = -result[0];
result[1] = -result[1];
}
return result;
}
/**
* Adds two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of adding parameters (numerator=[0], denominator=[1])
*/
public static int[] addFractions(int[] f1, int[] f2) {
int[] result = new int[2];
result[0] = (f1[0] * f2[1]) + (f2[0] * f1[1]);
result[1] = f1[1] * f2[1];
return simplifyFraction(result);
}
/**
* Subtracts two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of subtracting parameter f2 from f1 (numerator=[0], denominator=[1])
*/
public static int[] subtractFractions(int[] f1, int[] f2) {
return new int[2];
}
/**
* Multiplies two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of multiplying parameters (numerator=[0], denominator=[1])
*/
public static int[] multiplyFractions(int[] f1, int[] f2) {
return new int[2];
}
/**
* Divides two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of dividing parameter f2 by f1 (numerator=[0], denominator=[1])
*/
public static int[] divideFractions(int[] f1, int[] f2) {
return new int[2];
}
public static void main(String[] args) {
}
}
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class LA5a {
/**
* Error to output if either denominator is zero
*/
static final String E_DEN_ZERO = "Denominator cannot be zero.";
/**
* Error to output if dividing by zero
*/
static final String E_DIV_ZERO = "Cannot divide by zero.";
/**
* Error to output if the operation is invalid
*/
static final String E_OP_INVALID = "Invalid operation.";
/**
* Returns the greatest common divisor (gcd) of two integers
*
* @param num1 integer 1
* @param num2 integer 2
* @return gcd of integers 1 and 2
*/
public static int gcd(int num1, int num2) {
int t;
while (num2 != 0) {
t = num2;
num2 = num1 % num2;
num1 = t;
}
return num1;
}
/**
* Returns the simplified form of a fraction
*
* @param f fraction (numerator=[0], denominator=[1])
* @return simplified fraction (numerator=[0], denominator=[1])
*/
public static int[] simplifyFraction(int[] f) {
final int gcd = gcd(f[0], f[1]);
int[] result = {f[0]/gcd, f[1]/gcd};
if ((result[0]<0 && result[1]<0) || (result[1]<0)) {
result[0] = -result[0];
result[1] = -result[1];
}
return result;
}
/**
* Adds two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of adding parameters (numerator=[0], denominator=[1])
*/
public static int[] addFractions(int[] f1, int[] f2) {
int[] result = new int[2];
result[0] = (f1[0] * f2[1]) + (f2[0] * f1[1]);
result[1] = f1[1] * f2[1];
return simplifyFraction(result);
}
/**
* Subtracts two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of subtracting parameter f2 from f1 (numerator=[0], denominator=[1])
*/
public static int[] subtractFractions(int[] f1, int[] f2) {
int[] result = new int[2];
result[0] = (f1[0] * f2[1]) - (f2[0] * f1[1]);
result[1] = f1[1] * f2[1];
return simplifyFraction(result);
}
/**
* Multiplies two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of multiplying parameters (numerator=[0], denominator=[1])
*/
public static int[] multiplyFractions(int[] f1, int[] f2) {
int[] result = new int[2];
result[0] = f1[0] * f2[0];
result[1] = f1[1] * f2[1];
return simplifyFraction(result);
}
/**
* Divides two fractions
*
* @param f1 first fraction (numerator=[0], denominator=[1])
* @param f2 second fraction (numerator=[0], denominator=[1])
* @return result of dividing parameter f2 by f1 (numerator=[0], denominator=[1])
*/
public static int[] divideFractions(int[] f1, int[] f2) {
int[] result = new int[2];
result[0] = f1[0] * f2[1];
result[1] = f1[1] * f2[0];
return simplifyFraction(result);
}
public static void inputFraction1(Scanner sc, int[] f1){
System.out.println("Enter fraction: ");
System.out.print("Numerator: ");
f1[0] = sc.nextInt();
System.out.print("Denominator: ");
f1[1] = sc.nextInt();
while(f1[1] == 0){
System.out.print("Denominator (not zero): ");
f1[1] = sc.nextInt();
}
}
public static void inputFraction2(Scanner sc, int[] f2){
System.out.println("Enter fraction: ");
System.out.print("Numerator: ");
f2[0] = sc.nextInt();
while(f2[0] == 0){
System.out.print("Numerator (not zero): ");
f2[0] = sc.nextInt();
}
System.out.print("Denominator: ");
f2[1] = sc.nextInt();
while(f2[1] == 0){
System.out.print("Denominator (not zero): ");
f2[1] = sc.nextInt();
}
}
public static void print(int[] f){
System.out.println(f[0]+"/"+f[1]);
}
public static void main(String[] args) {
int f1[] = new int[2];
int f2[] = new int[2];
int f[];
Scanner sc = new Scanner(System.in);
while(true){
char op;
int num, denom;
System.out.println("Enter +. add -.subtract *.multiply /.divide e.Exit");
op = sc.next().charAt(0);
switch(op){
case '+' :
inputFraction1(sc, f1);
inputFraction1(sc, f2);
f = addFractions(f1, f2);
print(f);
break;
case '-' :
inputFraction1(sc, f1);
inputFraction1(sc, f2);
f = subtractFractions(f1, f2);
print(f);
break;
case '*' :
inputFraction1(sc, f1);
inputFraction1(sc, f2);
f = multiplyFractions(f1, f2);
print(f);
break;
case '/' :
inputFraction1(sc, f1);
inputFraction2(sc, f2);
f = divideFractions(f1, f2);
print(f);
break;
case 'e' :
break;
default:
System.out.println("Invalid opertor");
}
if(op == 'e')
break;
}
}
}
/*
Sample run:
Enter +. add -.subtract *.multiply /.divide e.Exit
+
Enter fraction:
Numerator: 4
Denominator: 0
Denominator (not zero): 5
Enter fraction:
Numerator: 1
Denominator: 2
13/10
Enter +. add -.subtract *.multiply /.divide e.Exit
/
Enter fraction:
Numerator: 5
Denominator: 4
Enter fraction:
Numerator: 0
Numerator (not zero): 7
Denominator: 0
Denominator (not zero): 8
10/7
Enter +. add -.subtract *.multiply /.divide e.Exit
e
*/

More Related Content

Similar to Write a program that works with fractions. You are first to implemen.pdf

AnswerNote Provided code shows several bugs, hence I implemented.pdf
AnswerNote Provided code shows several bugs, hence I implemented.pdfAnswerNote Provided code shows several bugs, hence I implemented.pdf
AnswerNote Provided code shows several bugs, hence I implemented.pdfanurag1231
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersSheila Sinclair
 
fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdfanjanadistribution
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfezonesolutions
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Nrtl activity coefficient for mixture1
Nrtl activity coefficient for mixture1Nrtl activity coefficient for mixture1
Nrtl activity coefficient for mixture1ESSID Abou Hligha
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfangelfragranc
 
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKEric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKGuardSquare
 
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...Istanbul Tech Talks
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfaioils
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
C++ code to determine tax on the given items When variable .pdf
 C++ code to determine tax on the given items When variable .pdf C++ code to determine tax on the given items When variable .pdf
C++ code to determine tax on the given items When variable .pdfANSAPPARELS
 
Design and Implementation of High Speed Area Efficient Double Precision Float...
Design and Implementation of High Speed Area Efficient Double Precision Float...Design and Implementation of High Speed Area Efficient Double Precision Float...
Design and Implementation of High Speed Area Efficient Double Precision Float...IOSR Journals
 
Hello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdfHello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdfFashionColZone
 
CalculatorProject.pdf
CalculatorProject.pdfCalculatorProject.pdf
CalculatorProject.pdfblueline4
 
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docxLiterary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docxjeremylockett77
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2SSE_AndyLi
 

Similar to Write a program that works with fractions. You are first to implemen.pdf (20)

AnswerNote Provided code shows several bugs, hence I implemented.pdf
AnswerNote Provided code shows several bugs, hence I implemented.pdfAnswerNote Provided code shows several bugs, hence I implemented.pdf
AnswerNote Provided code shows several bugs, hence I implemented.pdf
 
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And EngineersAnswers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
Answers To Selected Exercises For Fortran 90 95 For Scientists And Engineers
 
fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdf
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Nrtl activity coefficient for mixture1
Nrtl activity coefficient for mixture1Nrtl activity coefficient for mixture1
Nrtl activity coefficient for mixture1
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdf
 
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDKEric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
Eric Lafortune - ProGuard: Optimizer and obfuscator in the Android SDK
 
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
ITT 2014 - Eric Lafortune - ProGuard, Optimizer and Obfuscator in the Android...
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Slides13.pdf
Slides13.pdfSlides13.pdf
Slides13.pdf
 
C++ code to determine tax on the given items When variable .pdf
 C++ code to determine tax on the given items When variable .pdf C++ code to determine tax on the given items When variable .pdf
C++ code to determine tax on the given items When variable .pdf
 
Design and Implementation of High Speed Area Efficient Double Precision Float...
Design and Implementation of High Speed Area Efficient Double Precision Float...Design and Implementation of High Speed Area Efficient Double Precision Float...
Design and Implementation of High Speed Area Efficient Double Precision Float...
 
H010114954
H010114954H010114954
H010114954
 
Hello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdfHello, I need some assistance in writing a java program THAT MUST US.pdf
Hello, I need some assistance in writing a java program THAT MUST US.pdf
 
Functions
FunctionsFunctions
Functions
 
CalculatorProject.pdf
CalculatorProject.pdfCalculatorProject.pdf
CalculatorProject.pdf
 
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docxLiterary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
 
3 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart23 chapter2 algorithm_analysispart2
3 chapter2 algorithm_analysispart2
 

More from leventhalbrad49439

Which of the following are true when comparing TCPIP to the OSI Ref.pdf
Which of the following are true when comparing TCPIP to the OSI Ref.pdfWhich of the following are true when comparing TCPIP to the OSI Ref.pdf
Which of the following are true when comparing TCPIP to the OSI Ref.pdfleventhalbrad49439
 
What is the reason for having most of the blood volume distributed w.pdf
What is the reason for having most of the blood volume distributed w.pdfWhat is the reason for having most of the blood volume distributed w.pdf
What is the reason for having most of the blood volume distributed w.pdfleventhalbrad49439
 
What is eutectic reaction What is eutectoid reaction What is perife.pdf
What is eutectic reaction What is eutectoid reaction What is perife.pdfWhat is eutectic reaction What is eutectoid reaction What is perife.pdf
What is eutectic reaction What is eutectoid reaction What is perife.pdfleventhalbrad49439
 
What are the possible types of interfund transactions What are.pdf
What are the possible types of interfund transactions What are.pdfWhat are the possible types of interfund transactions What are.pdf
What are the possible types of interfund transactions What are.pdfleventhalbrad49439
 
What are Sociocultural issues when it come to body imageSolutio.pdf
What are Sociocultural issues when it come to body imageSolutio.pdfWhat are Sociocultural issues when it come to body imageSolutio.pdf
What are Sociocultural issues when it come to body imageSolutio.pdfleventhalbrad49439
 
Water can absorb and store a large amount of heat while increasing on.pdf
Water can absorb and store a large amount of heat while increasing on.pdfWater can absorb and store a large amount of heat while increasing on.pdf
Water can absorb and store a large amount of heat while increasing on.pdfleventhalbrad49439
 
True or false1 Anabolism results in increased production of biomas.pdf
True or false1 Anabolism results in increased production of biomas.pdfTrue or false1 Anabolism results in increased production of biomas.pdf
True or false1 Anabolism results in increased production of biomas.pdfleventhalbrad49439
 
The diploid number of the hypothetical animal Geneticus introductus .pdf
The diploid number of the hypothetical animal Geneticus introductus .pdfThe diploid number of the hypothetical animal Geneticus introductus .pdf
The diploid number of the hypothetical animal Geneticus introductus .pdfleventhalbrad49439
 
The Antoine equation often is used to describe vapor pressures lnP .pdf
The Antoine equation often is used to describe vapor pressures lnP .pdfThe Antoine equation often is used to describe vapor pressures lnP .pdf
The Antoine equation often is used to describe vapor pressures lnP .pdfleventhalbrad49439
 
The converson of the English to Christianity began a rich period of .pdf
The converson of the English to Christianity began a rich period of .pdfThe converson of the English to Christianity began a rich period of .pdf
The converson of the English to Christianity began a rich period of .pdfleventhalbrad49439
 
Table 19. Highest Level of Type of organization germ layers .pdf
Table 19. Highest Level of Type of organization germ layers .pdfTable 19. Highest Level of Type of organization germ layers .pdf
Table 19. Highest Level of Type of organization germ layers .pdfleventhalbrad49439
 
Question 12 (5 points)The reason that mitosis can result in the pr.pdf
Question 12 (5 points)The reason that mitosis can result in the pr.pdfQuestion 12 (5 points)The reason that mitosis can result in the pr.pdf
Question 12 (5 points)The reason that mitosis can result in the pr.pdfleventhalbrad49439
 
peripheral circuit functions built-in to the HC12 microcontroller, t.pdf
peripheral circuit functions built-in to the HC12 microcontroller, t.pdfperipheral circuit functions built-in to the HC12 microcontroller, t.pdf
peripheral circuit functions built-in to the HC12 microcontroller, t.pdfleventhalbrad49439
 
Mendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdf
Mendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdfMendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdf
Mendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdfleventhalbrad49439
 
If the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdf
If the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdfIf the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdf
If the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdfleventhalbrad49439
 
How does a wave formation influence the resultant waveSolution.pdf
How does a wave formation influence the resultant waveSolution.pdfHow does a wave formation influence the resultant waveSolution.pdf
How does a wave formation influence the resultant waveSolution.pdfleventhalbrad49439
 
How are health services paid for Provide a definition for the term .pdf
How are health services paid for Provide a definition for the term .pdfHow are health services paid for Provide a definition for the term .pdf
How are health services paid for Provide a definition for the term .pdfleventhalbrad49439
 
Given the information regarding mitosis and meiosis, clearly and con.pdf
Given the information regarding mitosis and meiosis, clearly and con.pdfGiven the information regarding mitosis and meiosis, clearly and con.pdf
Given the information regarding mitosis and meiosis, clearly and con.pdfleventhalbrad49439
 
First, look to determine what is the earliest era that fossils have .pdf
First, look to determine what is the earliest era that fossils have .pdfFirst, look to determine what is the earliest era that fossils have .pdf
First, look to determine what is the earliest era that fossils have .pdfleventhalbrad49439
 
Figure out and write down a definition of a translation that does no.pdf
Figure out and write down a definition of a translation that does no.pdfFigure out and write down a definition of a translation that does no.pdf
Figure out and write down a definition of a translation that does no.pdfleventhalbrad49439
 

More from leventhalbrad49439 (20)

Which of the following are true when comparing TCPIP to the OSI Ref.pdf
Which of the following are true when comparing TCPIP to the OSI Ref.pdfWhich of the following are true when comparing TCPIP to the OSI Ref.pdf
Which of the following are true when comparing TCPIP to the OSI Ref.pdf
 
What is the reason for having most of the blood volume distributed w.pdf
What is the reason for having most of the blood volume distributed w.pdfWhat is the reason for having most of the blood volume distributed w.pdf
What is the reason for having most of the blood volume distributed w.pdf
 
What is eutectic reaction What is eutectoid reaction What is perife.pdf
What is eutectic reaction What is eutectoid reaction What is perife.pdfWhat is eutectic reaction What is eutectoid reaction What is perife.pdf
What is eutectic reaction What is eutectoid reaction What is perife.pdf
 
What are the possible types of interfund transactions What are.pdf
What are the possible types of interfund transactions What are.pdfWhat are the possible types of interfund transactions What are.pdf
What are the possible types of interfund transactions What are.pdf
 
What are Sociocultural issues when it come to body imageSolutio.pdf
What are Sociocultural issues when it come to body imageSolutio.pdfWhat are Sociocultural issues when it come to body imageSolutio.pdf
What are Sociocultural issues when it come to body imageSolutio.pdf
 
Water can absorb and store a large amount of heat while increasing on.pdf
Water can absorb and store a large amount of heat while increasing on.pdfWater can absorb and store a large amount of heat while increasing on.pdf
Water can absorb and store a large amount of heat while increasing on.pdf
 
True or false1 Anabolism results in increased production of biomas.pdf
True or false1 Anabolism results in increased production of biomas.pdfTrue or false1 Anabolism results in increased production of biomas.pdf
True or false1 Anabolism results in increased production of biomas.pdf
 
The diploid number of the hypothetical animal Geneticus introductus .pdf
The diploid number of the hypothetical animal Geneticus introductus .pdfThe diploid number of the hypothetical animal Geneticus introductus .pdf
The diploid number of the hypothetical animal Geneticus introductus .pdf
 
The Antoine equation often is used to describe vapor pressures lnP .pdf
The Antoine equation often is used to describe vapor pressures lnP .pdfThe Antoine equation often is used to describe vapor pressures lnP .pdf
The Antoine equation often is used to describe vapor pressures lnP .pdf
 
The converson of the English to Christianity began a rich period of .pdf
The converson of the English to Christianity began a rich period of .pdfThe converson of the English to Christianity began a rich period of .pdf
The converson of the English to Christianity began a rich period of .pdf
 
Table 19. Highest Level of Type of organization germ layers .pdf
Table 19. Highest Level of Type of organization germ layers .pdfTable 19. Highest Level of Type of organization germ layers .pdf
Table 19. Highest Level of Type of organization germ layers .pdf
 
Question 12 (5 points)The reason that mitosis can result in the pr.pdf
Question 12 (5 points)The reason that mitosis can result in the pr.pdfQuestion 12 (5 points)The reason that mitosis can result in the pr.pdf
Question 12 (5 points)The reason that mitosis can result in the pr.pdf
 
peripheral circuit functions built-in to the HC12 microcontroller, t.pdf
peripheral circuit functions built-in to the HC12 microcontroller, t.pdfperipheral circuit functions built-in to the HC12 microcontroller, t.pdf
peripheral circuit functions built-in to the HC12 microcontroller, t.pdf
 
Mendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdf
Mendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdfMendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdf
Mendez Corporation has 10,000 shares of its $100 par value, 7 percen.pdf
 
If the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdf
If the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdfIf the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdf
If the dpy-11 and unc-31 genes are linked on the same chromosome, how.pdf
 
How does a wave formation influence the resultant waveSolution.pdf
How does a wave formation influence the resultant waveSolution.pdfHow does a wave formation influence the resultant waveSolution.pdf
How does a wave formation influence the resultant waveSolution.pdf
 
How are health services paid for Provide a definition for the term .pdf
How are health services paid for Provide a definition for the term .pdfHow are health services paid for Provide a definition for the term .pdf
How are health services paid for Provide a definition for the term .pdf
 
Given the information regarding mitosis and meiosis, clearly and con.pdf
Given the information regarding mitosis and meiosis, clearly and con.pdfGiven the information regarding mitosis and meiosis, clearly and con.pdf
Given the information regarding mitosis and meiosis, clearly and con.pdf
 
First, look to determine what is the earliest era that fossils have .pdf
First, look to determine what is the earliest era that fossils have .pdfFirst, look to determine what is the earliest era that fossils have .pdf
First, look to determine what is the earliest era that fossils have .pdf
 
Figure out and write down a definition of a translation that does no.pdf
Figure out and write down a definition of a translation that does no.pdfFigure out and write down a definition of a translation that does no.pdf
Figure out and write down a definition of a translation that does no.pdf
 

Recently uploaded

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 

Recently uploaded (20)

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 

Write a program that works with fractions. You are first to implemen.pdf

  • 1. Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-element array (the numerator is at index 0, the denominator is at index 1), and you are to return a resulting, simplified fraction as a new two-element array (again, with the numerator at index 0, and denominator at index 1). You have been provide an add method as an example. You must compute the resulting fraction using fraction-based math (working with numerators and denominators) – do not convert the fractions to double values (like 1.5), do the math, and convert back to a fraction. You have been provided a method to simplify a fraction using the gcd method from the previous lab. Once the operation methods are complete and pass the JUnit tests, now focus your attention on the main method. You first need to input the two fractions from the keyboard (numerator then denominator for each; you can assume integers) as well as one of the four valid operations (+, -, *, /). Then validate the inputs: make sure a valid operation was input, make sure neither of the denominators are zero, and make sure that the numerator of the second fraction isn’t zero if the operation is division (error messages have been provided for each of these situations). Finally, compute the result of the operation and output the answer. Note that if the denominator of the answer is 1, you should just output the numerator (this includes if the answer is 0). Here is the outline code given: public class LA5a { /** * Error to output if either denominator is zero */ static final String E_DEN_ZERO = "Denominator cannot be zero."; /** * Error to output if dividing by zero */ static final String E_DIV_ZERO = "Cannot divide by zero."; /** * Error to output if the operation is invalid */ static final String E_OP_INVALID = "Invalid operation.";
  • 2. /** * Returns the greatest common divisor (gcd) of two integers * * @param num1 integer 1 * @param num2 integer 2 * @return gcd of integers 1 and 2 */ public static int gcd(int num1, int num2) { int t; while (num2 != 0) { t = num2; num2 = num1 % num2; num1 = t; } return num1; } /** * Returns the simplified form of a fraction * * @param f fraction (numerator=[0], denominator=[1]) * @return simplified fraction (numerator=[0], denominator=[1]) */ public static int[] simplifyFraction(int[] f) { final int gcd = gcd(f[0], f[1]); int[] result = {f[0]/gcd, f[1]/gcd}; if ((result[0]<0 && result[1]<0) || (result[1]<0)) { result[0] = -result[0]; result[1] = -result[1]; } return result; }
  • 3. /** * Adds two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of adding parameters (numerator=[0], denominator=[1]) */ public static int[] addFractions(int[] f1, int[] f2) { int[] result = new int[2]; result[0] = (f1[0] * f2[1]) + (f2[0] * f1[1]); result[1] = f1[1] * f2[1]; return simplifyFraction(result); } /** * Subtracts two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of subtracting parameter f2 from f1 (numerator=[0], denominator=[1]) */ public static int[] subtractFractions(int[] f1, int[] f2) { return new int[2]; } /** * Multiplies two fractions * * @param f1 first fraction (numerator=[0], denominator=[1])
  • 4. * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of multiplying parameters (numerator=[0], denominator=[1]) */ public static int[] multiplyFractions(int[] f1, int[] f2) { return new int[2]; } /** * Divides two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of dividing parameter f2 by f1 (numerator=[0], denominator=[1]) */ public static int[] divideFractions(int[] f1, int[] f2) { return new int[2]; } public static void main(String[] args) { } } Solution Hi, Please find my implementation.
  • 5. Please let me know in case of any issue. import java.util.Scanner; public class LA5a { /** * Error to output if either denominator is zero */ static final String E_DEN_ZERO = "Denominator cannot be zero."; /** * Error to output if dividing by zero */ static final String E_DIV_ZERO = "Cannot divide by zero."; /** * Error to output if the operation is invalid */ static final String E_OP_INVALID = "Invalid operation."; /** * Returns the greatest common divisor (gcd) of two integers * * @param num1 integer 1 * @param num2 integer 2 * @return gcd of integers 1 and 2 */ public static int gcd(int num1, int num2) { int t; while (num2 != 0) { t = num2; num2 = num1 % num2; num1 = t; } return num1; } /** * Returns the simplified form of a fraction * * @param f fraction (numerator=[0], denominator=[1]) * @return simplified fraction (numerator=[0], denominator=[1])
  • 6. */ public static int[] simplifyFraction(int[] f) { final int gcd = gcd(f[0], f[1]); int[] result = {f[0]/gcd, f[1]/gcd}; if ((result[0]<0 && result[1]<0) || (result[1]<0)) { result[0] = -result[0]; result[1] = -result[1]; } return result; } /** * Adds two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of adding parameters (numerator=[0], denominator=[1]) */ public static int[] addFractions(int[] f1, int[] f2) { int[] result = new int[2]; result[0] = (f1[0] * f2[1]) + (f2[0] * f1[1]); result[1] = f1[1] * f2[1]; return simplifyFraction(result); } /** * Subtracts two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of subtracting parameter f2 from f1 (numerator=[0], denominator=[1]) */ public static int[] subtractFractions(int[] f1, int[] f2) { int[] result = new int[2]; result[0] = (f1[0] * f2[1]) - (f2[0] * f1[1]); result[1] = f1[1] * f2[1]; return simplifyFraction(result); }
  • 7. /** * Multiplies two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of multiplying parameters (numerator=[0], denominator=[1]) */ public static int[] multiplyFractions(int[] f1, int[] f2) { int[] result = new int[2]; result[0] = f1[0] * f2[0]; result[1] = f1[1] * f2[1]; return simplifyFraction(result); } /** * Divides two fractions * * @param f1 first fraction (numerator=[0], denominator=[1]) * @param f2 second fraction (numerator=[0], denominator=[1]) * @return result of dividing parameter f2 by f1 (numerator=[0], denominator=[1]) */ public static int[] divideFractions(int[] f1, int[] f2) { int[] result = new int[2]; result[0] = f1[0] * f2[1]; result[1] = f1[1] * f2[0]; return simplifyFraction(result); } public static void inputFraction1(Scanner sc, int[] f1){ System.out.println("Enter fraction: "); System.out.print("Numerator: "); f1[0] = sc.nextInt(); System.out.print("Denominator: "); f1[1] = sc.nextInt(); while(f1[1] == 0){ System.out.print("Denominator (not zero): "); f1[1] = sc.nextInt();
  • 8. } } public static void inputFraction2(Scanner sc, int[] f2){ System.out.println("Enter fraction: "); System.out.print("Numerator: "); f2[0] = sc.nextInt(); while(f2[0] == 0){ System.out.print("Numerator (not zero): "); f2[0] = sc.nextInt(); } System.out.print("Denominator: "); f2[1] = sc.nextInt(); while(f2[1] == 0){ System.out.print("Denominator (not zero): "); f2[1] = sc.nextInt(); } } public static void print(int[] f){ System.out.println(f[0]+"/"+f[1]); } public static void main(String[] args) { int f1[] = new int[2]; int f2[] = new int[2]; int f[]; Scanner sc = new Scanner(System.in); while(true){ char op; int num, denom; System.out.println("Enter +. add -.subtract *.multiply /.divide e.Exit");
  • 9. op = sc.next().charAt(0); switch(op){ case '+' : inputFraction1(sc, f1); inputFraction1(sc, f2); f = addFractions(f1, f2); print(f); break; case '-' : inputFraction1(sc, f1); inputFraction1(sc, f2); f = subtractFractions(f1, f2); print(f); break; case '*' : inputFraction1(sc, f1); inputFraction1(sc, f2); f = multiplyFractions(f1, f2); print(f); break; case '/' : inputFraction1(sc, f1); inputFraction2(sc, f2); f = divideFractions(f1, f2); print(f); break; case 'e' : break; default: System.out.println("Invalid opertor");
  • 10. } if(op == 'e') break; } } } /* Sample run: Enter +. add -.subtract *.multiply /.divide e.Exit + Enter fraction: Numerator: 4 Denominator: 0 Denominator (not zero): 5 Enter fraction: Numerator: 1 Denominator: 2 13/10 Enter +. add -.subtract *.multiply /.divide e.Exit / Enter fraction: Numerator: 5 Denominator: 4 Enter fraction: Numerator: 0 Numerator (not zero): 7 Denominator: 0 Denominator (not zero): 8 10/7 Enter +. add -.subtract *.multiply /.divide e.Exit e */