I dont understand what is wanted Pleas help me understand Wr

I don\'t understand what is wanted. Pleas help me understand.

Write a class named Fraction that will store fractional values as rational numbers, maintaining both a numerator and a denominator.

Use the following guidelines.

The class should internally maintain a numerator and a denominator in lowest terms, i.e. their GCD should always be 1.

The class should should support positive and negative rational numbers.

The class should have a default constructor initializing a Fraction to 0.

The class should have a two parameter constructor Fraction(int n, int d) to support initialization with arbitrary numerators and denominators

The class should implement the following methods

Fraction add(Fraction)

Fraction subtract(Fraction)

Fraction multiply(Fraction)

Fraction divide(Fraction) - throw new ArithmeticException(\"zerodivide\"); on zero divide.

The class should have a public String toString() method to return a String representation of the rational number.

The class should implement the following accessor methods

int getNumerator()

int getDenominator()

double getDecimal()

The template for the class is below. You must fill in the methods.


Copy and paste methods from the previous GCD assignment as needed, or use the GCD object, your choice.

Test the class with the following code, which you should put in a static public void main(String[]) method in a separate class named AssignFraction.

//AssignGCD

//GCD

Solution

import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.lang.Math;

public class AssignFraction
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
Fraction f0 = new Fraction();
Fraction f1 = new Fraction(2, 2);
Fraction f2 = new Fraction(2, 7);
Fraction f3 = new Fraction(1, -3);
System.out.println(f1 + \" + \" + f0 + \" = \" + f1.add(f0));
System.out.println(f1 + \" * \" + f2 + \" = \" + f1.multiply(f2));
System.out.println(f1 + \" / \" + f3 + \" = \" + f1.divide(f3));
System.out.println(f2 + \" + \" + f3 + \" = \" + f2.add(f3));
System.out.println(f2 + \" - \" + f3 + \" = \" + f2.subtract(f3));
System.out.println(f2 + \" * \" + f3 + \" = \" + f2.multiply(f3));
System.out.println(f2 + \" / \" + f3 + \" = \" + f2.divide(f3));
System.out.println(\"the numerator of \" + f3 + \" is \" + f3.getNumerator());
System.out.println(\"the denominator of \" + f3 + \" is \" + f3.getDenominator());
System.out.println(\"the decimal value of \" + f3 + \" is \" + f3.getDecimal());
}
}


public class Fraction
{
int numerator;
int denominator;
String message;
GCD obj;
  
Fraction()
{
// this method is the default constructor, invoked to initialize an instance when new Fraction() is executed
// 2 lines of code
numerator = 0;
denominator = 1;
}
  
  
Fraction(int n, int d)
{
// this method is the constructor invoked to initialize an instance when new Fraction(n, d) is executed
// it should maintain the numerator and denominator in lowest terms (divide out the GCD from each)
// and should handle the sign
//13-15 lines of code
// For ex: n = 25, d= 20 => GCD=5 so numerator = n/GCD; denominator=d/GCD
// numerator = 5 denominator = 4; Since now GCD=1, so this is lowest possible form of this numberator and denominator
// so we will store the final value as 5 and 4
  
// GCD obj = new GCD();
int var = 1; // initializing 1 to avoid garbage value //
var = obj.gcd(n,d);
while(var!=1)
{
numerator = n/var;
denominator = d/var;
var=obj.gcd(numerator, denominator);
}
DecimalFraction dfrac = new DecimalFraction(numerator, denominator);
message = dfrac.toString(true);
}
  
  
  
public int getNumerator()
{
return numerator; // 1 line of code
}
  
  
  
public int getDenominator()
{
return denominator; // 1 line of code
}
  
  
public double getDecimal()
{
return (double)numerator/denominator; // 1 line of code
}
  

public String toString()
{
return message; // 1 line of code
}
  
  
public Fraction add(Fraction opr)
{
// the object instance is the left operand of add; the passed object, opr, is the right operand of add() //
// we will get LCM of denominators like 1/3 and 2/4 -> LCM of denominators is 12
// GCD obj = new GCD();
int lcm = obj.lcm(denominator, opr.denominator);
numerator = numerator*(lcm/denominator); // 4
opr.numerator = opr.numerator*(lcm/opr.denominator); // 6
numerator = numerator+ (opr.numerator);
denominator = lcm;
}
  
  
public Fraction subtract(Fraction opr)
{
// addition is done using cross-multiplication
// return null; // 3-5 lines of code
// Do similar to addtion
}
  
public Fraction multiply(Fraction opr)
{
return new Fraction(numerator * opr.numerator, denominator * opr.denominator); // example for the others
}
  
public Fraction divide(Fraction opr)
{
// check for zero-divide using the code
if (0 == denominator)
throw new ArithmeticException(\"zerodivide\"); // this throws a Java Exception and immediately ends the method
return new Fraction(numerator/opr.numerator, denominator/opr.denominator); // example for the others
}
}


//GCD
public class GCD
{

// gcd method

public int gcd(int x, int y)

{

if(x<=1||y<=1)

return 1;

while(x!=y)
{

if(x<y)

y=y-x;

else

x=x-y;

}

return x;

}

// finding lcm method

public int lcm(int x, int y)

{

int greater;

greater = (x > y) ? x : y; // greater number

while(true)

{

if(greater % x == 0 && greater % y == 0)

return greater;

++greater;

}

}
}
// GCD class ending here

class DecimalFraction
{
int wholePart;
List<Integer> fractionDigits;
int repeatingAt;
  
DecimalFraction(int numerator, int denominator)
{
wholePart = numerator / denominator;
numerator = (numerator % denominator) * 10;
fractionDigits = new ArrayList<Integer>();
repeatingAt = -1;

List<Integer> remainders = new ArrayList<Integer>();
while (numerator > 0 && repeatingAt == -1)
{
remainders.add(numerator);
int whole = numerator / denominator;
numerator = (numerator % denominator) * 10;
fractionDigits.add(whole);
repeatingAt = remainders.indexOf(numerator);
}
}

String toString(boolean showRepeatingGroup)
{
StringBuilder result = new StringBuilder(wholePart + \".\");
for (int i = 0; i < fractionDigits.size(); i++) {
if (showRepeatingGroup && i == repeatingAt) {
result.append(\"{\");
}
result.append(fractionDigits.get(i));
}
if (showRepeatingGroup && repeatingAt >= 0) {
result.append(\"}\");
}
return result.toString();
}

I don\'t understand what is wanted. Pleas help me understand. Write a class named Fraction that will store fractional values as rational numbers, maintaining bo
I don\'t understand what is wanted. Pleas help me understand. Write a class named Fraction that will store fractional values as rational numbers, maintaining bo
I don\'t understand what is wanted. Pleas help me understand. Write a class named Fraction that will store fractional values as rational numbers, maintaining bo
I don\'t understand what is wanted. Pleas help me understand. Write a class named Fraction that will store fractional values as rational numbers, maintaining bo
I don\'t understand what is wanted. Pleas help me understand. Write a class named Fraction that will store fractional values as rational numbers, maintaining bo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site