Please help in JAVA: This program should input numerator and denominator from a
ID: 3789631 • Letter: P
Question
Please help in JAVA:
This program should input numerator and denominator from a file, create a Fraction object (reducing the fraction if necessary) and then save the fraction to an ArrayList. This list will then be sorted, and output.
Make one method to input, create, and add to the ArrayList. Another method call to sort. And the third method to output the contents of the (sorted) ArrayList.
The input file will consist of an (unknown) quantity of ints representing numerator denominator pairs, which may be negative or zero.
You will need to think about your constructor - you will find it easier if you follow these rules
1. If both numerator and denominator are negative make them both positive
2. if the numerator is positive but the denominator is negative switch both so that the numerator is negative and the denominator positive
3. any other case leave as is.
thanks!
Explanation / Answer
Hi buddy, please find the below java program. Comments have been added for your better understanding.
----------------------------------------------------------
import java.util.*;
import java.lang.*;
import java.io.*;
class Fraction implements Comparable<Fraction>{
//This function calcuates the gcd of two numbers
int gcd(int a, int b){
if(b==0)return a;
return gcd(b,a%b);
}
int ind,num,den;
//This is the constructor
Fraction(int ind,int num, int den){
this.ind = ind;
//Calculating GCD
int g = gcd(Math.abs(num),Math.abs(den));
//Reducing the fractions
num = num/g;
den = den/g;
//Dealing with the negative sign
if((num<=0&&den<=0)||(den<0&&num>=0)){
num = -num;
den = -den;
}
this.num = num;
this.den = den;
}
//This method defines how the object should be compared with the current object
@Override
public int compareTo(Fraction f){
return f.den*this.num - f.num*this.den;
}
//This method converts the fraction to a string
@Override
public String toString(){
return ind+" : ("+num+" / "+den+")";
}
}
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// Initializing the bufferedReader for reading the input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Reading the input pairs and storing in the array
String pairs[] = br.readLine().trim().split(" ");
//Initializing the fractions arraylist
ArrayList<Fraction> fractions = new ArrayList();
for(int i=0;i<pairs.length;i+=2){
int n = Integer.parseInt(pairs[i]);
int d = Integer.parseInt(pairs[i+1]);
//Creating a new fraction object with the index, numerator and denominator
//Index is used for identifying the fraction
Fraction f = new Fraction(i/2 + 1,n,d);
//Adding the fraction object to the arraylist
fractions.add(f);
}
//Calling the sort method in the Collections class to sort the list
Collections.sort(fractions);
System.out.println(fractions);
}
}
--------------------------------------------------------------------------------------------------------
Input : 5 4 6 8 -3 9 30 68
Output : [3 : (-1 / 3), 4 : (15 / 34), 2 : (3 / 4), 1 : (5 / 4)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.