Question

Must write in Java - ignore the Junit tests Write a program that works with fractions....

Must write in Java - ignore the Junit tests

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 are two example runs of the program:

Enter the numerator for the first fraction: 1

Enter the denominator for the first fraction: 2

Enter the numerator for the second fraction: -4

Enter the denominator for the second fraction: 8

Enter the operation (+, -, *, /): +

1/2 + -4/8 = 0

Enter the numerator for the first fraction: 7

Enter the denominator for the first fraction: 8

Enter the numerator for the second fraction: 1

Enter the denominator for the second fraction: 3

Enter the operation (+, -, *, /): -

7/8 - 1/3 = 13/24

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

import java.util.*;
class FractionAsArrays
{
public static int gcd(int num1, int num2)
{
if(num1 == 0)
return num2;
else if(num2 == 0)
return num1;
else
return gcd(num2, num1 % num2);
}
public static int[] reduce(int[] number)
{
int temp = gcd(number[0], number[1]);
number[0] /= temp;
number[1] /= temp;
return number;
}
public static int[] add(int[] first, int[] second)
{
int[] result = new int[2];
result[0] = first[0]*second[1] + second[0]*first[1];
result[1] = first[1]*second[1];
return reduce(result);
}
public static int[] sub(int[] first, int[] second)
{
int[] result = new int[2];
result[0] = first[0]*second[1] - second[0]*first[1];
result[1] = first[1]*second[1];
return reduce(result);
}
public static int[] mul(int[] first, int[] second)
{
int[] result = new int[2];
result[0] = first[0] * second[0];
result[1] = first[1] * second[1];
return reduce(result);
}
public static int[] div(int[] first, int[] second)
{
int[] result = new int[2];
result[0] = first[0] * second[1];
result[1] = first[1] * second[0];
return reduce(result);
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[] firstNumber = new int[2];
int[] secondNumber = new int[2];
int[] resultNumber = new int[2];
System.out.print("Enter the numerator for the first fraction: ");
firstNumber[0] = sc.nextInt();
System.out.print("Enter the denominator for the first fraction: ");
firstNumber[1] = sc.nextInt();
  
System.out.print("Enter the numerator for the second fraction: ");
secondNumber[0] = sc.nextInt();
System.out.print("Enter the denominator for the second fraction: ");
secondNumber[1] = sc.nextInt();
if(firstNumber[1] == 0 || secondNumber[1] == 0)
{
System.out.println("Denominators cannot be 0 for a fraction.");
return;
}
  
char operator;
System.out.print("Enter the operation (+, -, *, /): ");
operator = sc.next().charAt(0);
switch(operator)
{
case '+':   resultNumber = add(firstNumber, secondNumber); break;
case '-':   resultNumber = sub(firstNumber, secondNumber); break;
case '*':   resultNumber = mul(firstNumber, secondNumber); break;
case '/':   resultNumber = div(firstNumber, secondNumber); break;
default : return;
}
System.out.print(firstNumber[0]+"/"+firstNumber[1]+operator+secondNumber[0]+"/"+secondNumber[1]+" = "+resultNumber[0]);
if(resultNumber[1] != 1)
System.out.println("/"+resultNumber[1]);
else
System.out.println();
}
}

And the output screenshot is:

% (D < > 1 FractionAsArrays.java 《 Terminal Shell Edit View Window Help [ -))) 100% ES), Wed 16 Nov 00:35 ANANDA KUMAR THUMMA

Add a comment
Know the answer?
Add Answer to:
Must write in Java - ignore the Junit tests Write a program that works with fractions....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • IN JAVA PLEASE HELP! ALSO PLEASE ADD COMMENTS Summary Build two classes (Fraction and Fraction Counter)...

    IN JAVA PLEASE HELP! ALSO PLEASE ADD COMMENTS Summary Build two classes (Fraction and Fraction Counter) and a Driver for use in counting the number of unique fractions read from a text file. We'll use the ArrayList class to store our list of unique Fraction Counters. Rather than designing a monolithic chunk of code in main like we did in the previous homework, we'll practice distributing our code into containers (called classes) that you will design specifically to tackle this...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT