I have main class that asks the user to input fractions I ha
I have main class that asks the user to input fractions. I have Fraction class that takes the fractions from user and check i they are valid, and reduce. And I have a Quicksort class that sorts all the fractions.
User input can be: 1/2 1 3/4 7/6 0/5 ....
sort is a public function in Quicksort that takes an array of fractions as argumet: sort(Fraction[ ] frac_Array);
My question is: How can i save the fractions in an array to pass it to sort?
//Main
public static void main(String[] args) {
print_header(); // Print Header
Scanner scan = new Scanner(System.in);
// Accept fractions from user
System.out.print(\"\ Enter your fractions: \");
String string_input = scan.nextLine();
String[] stringArray = string_input.split(\" \");
for (int i = 0; i < stringArray.length; i++) {
int num, denom;
Fraction frac;
Fraction[] fracArray = {};
if (stringArray[i].contains(\"/\")) {
String[] numDenom = stringArray[i].split(\"/\");
num = Integer.parseInt(numDenom[0]);
denom = Integer.parseInt(numDenom[1]);
frac = new Fraction(num, denom);
} else {
num = Integer.parseInt(stringArray[i]);
denom = 1;
frac = new Fraction(num, denom);
}
fracArray[i] = frac; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
}
Solution
Please follow the code and comments for description :
CODE :
a) Fraction.java :
public class Fraction implements Comparable<Fraction> {
private int numerators, denominators;
// Constructors
public Fraction() { // default
numerators = 0;
denominators = 1;
}
public Fraction(int n, int d) { // with parameters
this.numerators = n; // this is the member variable
this.denominators = d;
}
// method for conversion to string
@Override
public String toString() {
return (\"Decimal Value of the Fraction (\" + numerators + \"/\" + denominators + \")=\" + (double) numerators / (double) denominators);
}
// this implements Comparable
@Override
public int compareTo(Fraction o) {
double x = (double) numerators / (double) denominators;
double y = (double) o.numerators / (double) o.denominators;
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
}
}
b) FractionArray.java :
import java.util.Arrays; // for sorting
import java.util.Scanner;
import java.util.StringTokenizer;
class FractionSort {
public static void main(String[] argv) {
Fraction[] fracArray = new Fraction[7];
Scanner sc = new Scanner(System.in); // scanner that takes input from the command line
String input = sc.nextLine();
StringTokenizer st = new StringTokenizer(input, \" \"); // tokenizer that splits based on the spaces
for (int i = 0; i < fracArray.length; i++) { // iterating over the length of the arrays
int count = 0; // required initialisations
if (count < st.countTokens()) { // checking for the tokens in the strin tokniser
int numerator = 0; // variable to store numerator on each iteration
int denominator = 0; // variable to store denominator on each iteration
Fraction frac;
String inToken;
inToken = st.nextToken();
if (inToken.contains(\"/\")) { // checking if is a fractionor not
String[] numDenom = inToken.split(\"/\"); // splitting based on the symbol
numerator = Integer.parseInt(numDenom[0]); // saving the numerator
denominator = Integer.parseInt(numDenom[1]); // saving the denominator
frac = new Fraction(numerator, denominator); // adding it to the fraction class
fracArray[i] = frac; // summing it to the array
} else {
numerator = Integer.parseInt(inToken);
denominator = 1;
frac = new Fraction(numerator, denominator);
fracArray[i] = frac;
}
}
count++; // incrementing the index count value
}
// printing the entered fractions
System.out.println(\"The Entered Fractions are : \");
for (int i = 0; i < fracArray.length; i++) {
System.out.println(fracArray[i]);
}
// sorting the fractions using Comparable interface
Arrays.sort(fracArray);
// printing the sorted fractions
System.out.println(\"\ The Sorted Fractions are : \");
for (int i = 0; i < fracArray.length; i++) {
System.out.println(fracArray[i]);
}
}
}
OUTPUT :
1/8 2 1/5 0/5 6/8 9/7 4/5
The Entered Fractions are :
Decimal Value of the Fraction (1/8)=0.125
Decimal Value of the Fraction (2/1)=2.0
Decimal Value of the Fraction (1/5)=0.2
Decimal Value of the Fraction (0/5)=0.0
Decimal Value of the Fraction (6/8)=0.75
Decimal Value of the Fraction (9/7)=1.2857142857142858
Decimal Value of the Fraction (4/5)=0.8
The Sorted Fractions are :
Decimal Value of the Fraction (0/5)=0.0
Decimal Value of the Fraction (1/8)=0.125
Decimal Value of the Fraction (1/5)=0.2
Decimal Value of the Fraction (6/8)=0.75
Decimal Value of the Fraction (4/5)=0.8
Decimal Value of the Fraction (9/7)=1.2857142857142858
Decimal Value of the Fraction (2/1)=2.0
Hope this is helpful.


