Question: Can you add comments to this code please? import java.util.*; import java.io.File; public class Fraction_V1 { public static int gcf(int a, int b) { return

Can you add comments to this code please?

import java.util.*; import java.io.File;

public class Fraction_V1 { public static int gcf(int a, int b) { return b == 0 ? a : gcf(b, a % b); }

public static void main(String [] args) { HashMap fractionCount =new HashMap(); String[] fractions = new String[100000]; int i = 0; try { File file = new File("fractions.txt");//locate the file Scanner sc = new Scanner(file);//reads file while(sc.hasNext()) { String line = sc.nextLine(); String [] part = line.split("/"); int numa = Integer.parseInt(part[0]);//convert to string int denom = Integer.parseInt(part[1]);//convert to string int gecma = gcf(numa, denom);//determines the most common factor int num = numa/gecma; int dec = denom/gecma; String simplifiedFraction = Integer.toString(num) + "/" + Integer.toString(dec);//reads txt as number/number from txt if (fractionCount.containsKey(simplifiedFraction)) { int countFraction = fractionCount.get(simplifiedFraction); countFraction++; fractionCount.put(simplifiedFraction, countFraction); } else { fractionCount.put(simplifiedFraction, 1); fractions[i] = line; i++;

} } } catch (Exception e) {//throws exception if file not found System.out.println(e); }

for (int j = 0; j < i; j++) { String fract = fractions[j]; String []part = fract.split("/"); int nume = Integer.parseInt(part[0]); int denom = Integer.parseInt(part[1]);

int gecma = gcf(nume, denom); int num = nume/gecma; int dec = denom/gecma; String simplifiedFraction = Integer.toString(num) + "/" + Integer.toString(dec);

int count = fractionCount.get(simplifiedFraction); System.out.println(fract + " has a count of " + count);// returns fraction with the number of occurances } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!