Please help me code the following in: JAVA! Please use a lot of COMMENTS, so I c
ID: 3741682 • Letter: P
Question
Please help me code the following in: JAVA!
Please use a lot of COMMENTS, so I can understand better.
Full points will be awarded, thanks in advance!
PLEASE HAVE THE PROGRAM READ FROM THIS TXT. FILE!:
Here is the grading criteria:
Introduction Your project is to read in a series of fractions from a text file, which will have each line formatted as follows:"A/B". A sample text file is listed below, and the purpose of your program is to read in each fraction and count the number of occurrences for the current fraction. When all the input is consumed (or as the input is consumed), your program will print out its list of unique fraction and their corresponding count - see the output below (and you may assume no blank lines or misleading characters; see the text file link on the website for one of the actual inputs I'll use when testing your submission)Explanation / Answer
package January;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
// Class FileFrequency definition
public class FileFrequency
{
// To store the fraction numbers from file
ArrayList<String> fraction = new ArrayList<String>();
// To store user required fraction
ArrayList<String> userFraction = new ArrayList<String>();
// Counter for number of records in the file
int fCounter;
// Counter for number of records in the user fraction number
int uCounter;
// Method to read fractions from file
void readFile()
{
// try block begins
try
{
// Scanner class object created to open file for reading
// Using anonymous object of class FileInputStream
Scanner inFile = new Scanner (new FileInputStream("fractionNumber.txt"));
// Loops till data available
while(inFile.hasNextLine())
{
// Reads fraction from file
String data = inFile.next();
// Adds the fraction in array list
fraction.add(data);
// Increase record counter by one
fCounter++;
}// End of while
// Close the file
inFile.close();
}// End of try block
// Catch block to hand File not found exception
catch(FileNotFoundException fnfe)
{
// Displays error message
System.out.println("File shopingCart.txt was not found!");
}// End of catch block
}// End of method
// Method to read fractions from file required by the user
void userFrequency()
{
// try block begins
try
{
// Scanner class object created to open file for reading
// Using anonymous object of class FileInputStream
Scanner inFile = new Scanner (new FileInputStream("userFraction.txt"));
// Loops till data available
while(inFile.hasNextLine())
{
// Reads fraction from file
String data = inFile.next();
// Adds the fraction in array list
userFraction.add(data);
// Increase record counter by one
uCounter++;
}// End of while
// Close the file
inFile.close();
}// End of try block
// Catch block to hand File not found exception
catch(FileNotFoundException fnfe)
{
// Displays error message
System.out.println("File shopingCart.txt was not found!");
}// End of catch block
}// End of method
// Method to display the frequency count of the required fraction numbers
void displayFraction()
{
// Creates an integer array to store the frequency count of size uCounter
int arrFraction[] = new int [uCounter];
// Loops till number of fraction frequency required by the user
for(int c = 0; c < uCounter; c++)
{
// Initializes to zero
arrFraction[c] = 0;
// Loops till number of records in the file
for(int d = 0; d < fCounter; d++)
{
// Checks if the file fraction is equals to user fraction
if(fraction.get(d).equals(userFraction.get(c)))
// Increase the fraction counter of c index position by one
arrFraction[c]++;
}// End of inner for loop
}// End of outer for loop
// Loops till number of fraction frequency required by the user
for(int c = 0; c < uCounter; c++)
// Displays the frequency
System.out.println(userFraction.get(c) + " has a count of " + arrFraction[c]);
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates an object of class FileFrequency
FileFrequency ff = new FileFrequency();
// Call the method to read frequency file contents
ff.readFile();
// Call the method to read user required frequency file contents
ff.userFrequency();
// Calls the method to calculate and display the frequency
ff.displayFraction();
}// End of method
}// End of class
Sample Output:
6/3 has a count of 1
7/3 has a count of 0
1/3 has a count of 2
12/6 has a count of 0
fractionNumber.txt file contents
6/3
4/2
5/9
80/90
800/900
15/25
5/5
1/1
1/10
1/100
1/1000
1/3
2/6
1/2
1/3
1/1
1/4
1/5
1/6
1/7
1/8
1/9
2/1
2/2
2/3
2/4
2/5
2/6
2/7
2/8
2/9
userFraction.txt file contents
6/3
7/3
1/3
12/6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.