subject

Create a class called Fraction. Provide a constructor that takes 2 integers. Provide methods for:
toString
setFraction
equals
add
subtract
multiple
divide
reduce - make sure you get the previous methods done before worrying about reduce.

The following will be your starting point:

package fractions;
import java. util.*;
public class Fraction
{
private Scanner scan = new Scanner(System. in);
private int num=1;
private int denom=1;

public Fraction()
{
}

public Fraction(int n, int d)
{
// Fill in code (good to use setFraction)
}

public void setFraction(int n, int d)
{
//Fill in code ... don't forget to reduce
}

public Fraction add(Fraction op)
{
//Fill in code ... don't forget to reduce
// Algebra HINT: a/b + c/d = (a*d + b*c)/(b*d)
}

public Fraction subtract(Fraction op)
{
//Fill in code ... don't forget to reduce
// Algebra HINT: a/b - c/d = (a*d - b*c)/(b*d)
}

public Fraction multiply(Fraction op)
{
//Fill in code ... don't forget to reduce
// Algebra HINT: a/b * c/d = (a*c)/ (b*d)
}

public Fraction divide(Fraction op)
{
//Fill in code ... don't forget to reduce
// Algebra HINT: a/b / c/d = (a*d)/ (b*c)
}

private void reduce()
{
// Pseudo code:
// set smaller = minimum ( abs(num), abs(denom));
// Loop through the possible divisors: 2, 3, 4, ... smaller
// For each possible divisor:
// while (num and denom are evenly divisible by divisor)
// {
// num /= divisor;
// denom /= divisor;
// smaller /= divisor;
// }
}

public boolean equals(Fraction f)
{
// Assuming all fractions are reduced
// Fill in code
}

public String toString()
{
// Fill in code
}

public void readin(String label)
{
while (true) // Keep trying if bad input is received
{
System. out. print(label);
String temp = scan. next();
temp = temp. trim(); // get rid of white space at the beginning and end
int index = temp. indexOf('/');
if (index >= 0)
{
String numStr = temp. substring(0, index);
String denomStr = temp. substring(index+1);
int n = Integer. parseInt(numStr);
int d = Integer. parseInt(denomStr);
setFraction(n, d);
return;
}
else
System. out. println("Input Fraction missing / ");
}//Keep trying until you get it right
}

public static void main(String[] args)
{
Fraction f1= new Fraction();
Fraction f2= new Fraction();
Fraction f3=null;
Scanner scan = new Scanner(System. in);

while(true)
{
System. out. println(); // Add a blank line
System. out. print("Enter operation: + - * / q (q ==> quit) : ");
String input = scan. next();
if (input. charAt(0) == 'q')
break; // All done

f1.readin("Enter Fraction 1: ");
f2.readin("Enter Fraction 2: ");
System. out. println("f1 = " + f1);
System. out. println("f2 = " + f2);

if (f1.equals(f2))
System. out. println("f1 and f2 are equal");
else
System. out. println("f1 and f2 are not equal");

switch (input. charAt(0))
{
case '+':
f3 = f1.add(f2);
System. out. println("f1+f2=" + f3);
break;

case '-':
f3 = f1.subtract(f2);

System. out. println("f1-f2=" + f3);
break;

case '*':
f3 = f1.multiply(f2);
System. out. println("f1*f2="+f3);
break;

case '/':
f3 = f1.divide(f2);
System. out. println("f1/f2="+f3);
break;

default:
System. out. println("Illegal command: " + input );
break;

}
}// end of while loop
System. out. println("Bye");

} // end of main
}

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 07:30, alexandramendez0616
Jasper and samantha are in a robotics competition. the guidelines state that the robots should be able to move a 10-gram weight at least 2 meters and turn in a circle. jasper and samantha have already built the robot. which step of the design process should they follow next to decide whether their robot meets the minimum criteria for the competition?
Answers: 1
image
Computers and Technology, 22.06.2019 08:40, baue4431
What are the three parts to physical security standards for various types of army equipment and the risk level
Answers: 2
image
Computers and Technology, 23.06.2019 01:50, akornegay2
Write a program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: a. double the number. b. reverse the digits of the number. c. raise the number to the power of 2, 3, or 4. d. sum the digits of the number. e. if the number is a two-digit number, then raise the first digit to the power of the second digit. f. if the number is a three-digit number and the last digit is less than or equal to 4, then raise the first two digits to the power of the last digit. after performing an operation if the number is less than 10, add 10 to the number. also, after each operation determine if the number is prime. each successive operation should be performed on the number generated by the last operation. your program should not contain any global variables and each of these operations must be implemented by a separate function. also, your program should be menu driven. 7. (fraction calculator) write a program that
Answers: 1
image
Computers and Technology, 24.06.2019 06:50, emmv565628
What are the things you are considering before uploading photos on social media?
Answers: 1