Question
HELP IN JAVA: Write a program that will input integers (numerator and denominator) from a file (lab20.in) , create fractions from these integers, and store
HELP IN JAVA: Write a program that will input integers (numerator and denominator) from a file (lab20.in) , create fractions from these integers, and store these fractions in an array. It will then ask the user for two integers (numerator and denominator), create a fraction from these, and search the array for this fraction (using binary search). I am giving you the file below but when coding you will not always know the amount of objects in a file.
lab20.in:
1 4 5 16 3 8 50 100 11 12 4 3 3 2 13 8 50 25 9 4 11 4 59 20 90 30
I need help finishing the method binarySearch and how to add the ints from the file into an array so the method can use it. An array and not an arraylist
What I have so far:
public class Lab20 { public static class Fraction implements Comparable
denominator = 1; } //Constructor public Fraction(int num, int denom) { this.numerator = num; this.denominator = denom; } //Getters and Setters public int getNumerator() {
return numerator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public int getDenominator() {
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
} //toString to print public String toString() {
return numerator + "/" + denominator;
} //compareTo public int compareTo(Fraction f) { if ((f.denominator*this.numerator)>(f.numerator*this.denominator))
return 1;
else if ((f.denominator*this.numerator)<(f.numerator*this.denominator))
return -1;
else
return 0; } public static Fraction binarySearch(Fraction[]myF, int high, int low, Fraction key) { int rangeSize = (high - low)+1; int mid = (low + high)/2; if (rangeSize == 1) { if (A[mid].compareTo(key)==1) } } } public static void main(String[] args) { //Initialize file File inFile = new File("lab20.in"); Scanner fileInput = null; //try and catch try {
fileInput = new Scanner(inFile);
}
catch (FileNotFoundException e) {
} Fraction[] myF = new Fraction[13]; int n, d; //Loop to get fraction input and adding it to the array while(fileInput.hasNext()){ n = fileInput.nextInt(); d = fileInput.nextInt(); //Creating Fraction Fraction f = new Fraction(n, d); //Adding Fraction to list } int num, denom; System.out.println("Input fraction to find: "); Scanner s = new Scanner(System.in); System.out.println("Numerator:"); num = s.nextInt(); System.out.println("Denominator:"); denom = s.nextInt(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started