Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. What advice would you give to Carol Sullivan-Diaz?

Answered: 1 week ago