Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Summary Build two classes (Fraction and FractionCounter) and a Driver for use in counting the number of unique fractions read from a text file.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Summary Build two classes (Fraction and FractionCounter) and a Driver for use in counting the number of unique fractions read from a text file. We'll use the ArrayList class to store our list of unique Fraction Counters. Rather than designing a monolithic chunk of code in main like we did in the previous homework, we'll practice distributing our code into containers (called classes) that you will design specifically to tackle this same problem in a different and more logically organized fashion. Once again the filename containing all the fractions will be "fractions.txt" Introduction SEP 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 one line at a time and build a Fraction object from A and B. For each unique Fraction seen, your program will create a Fraction Counter object used to track the number of occurrences of that specific fraction. When all the input is consumed, your program will print out its ArrayList of unique Fraction Counters, which should report the fraction and its count - see output below. You can assume no blank lines or misleading characters; see the text file link above for the some of the input I'll use when testing your submission. Your program must reduce fractions, as demonstrated in the output below. Sample text file input: 6/3 7/3 6/3 2/1 Sample output: 2/1 has a count of 3 7/3 has a count of 1 Hints (& Spoilers) Test your code as you add features (either methods or data) incrementally, making sure your small change works as you envisioned it to. Try to keep in mind OO principles; for example, to promote information hiding, we should probably make all of our data members private for each class. Consider reading one line at a time (with scanner.nextLine()), and using the split function (defined in class String) to give you an array with two elements in it (the numerator and denominator, respectively) Getting a null pointer exception or all Fractions seem to have the same value? Building an array does not put any elements inside the array, so make sure you are building new Fraction objects and adding them to your list if they are unique. You should use build a Fraction Counter object for each unique Fraction encountered, and store a list of Fraction Counters as your program executes. You would never have two Fraction Counters both storing and counting the same Fraction (say, 1/2). You can determine if a Fraction is unique by looping over your list of FractionCounters, calling compareAndIncrement() on each Fraction Counter, passing as input the Fraction you just read from the file. If all calls to compareAndIncrement() return false, then this is a unique Fraction that needs a Fraction Counter created for it. The Fraction Counter function compareAndIncrement() will have to compare two fractions, and this will require some kind of comparison function for the Fraction Class. You should implement some type of "equals" method in the Fraction class that simply compares the numerators and denominators of the two Fractions in question. A slightly different approach would be to define a static "isEqual" function that takes two Fraction objects as input, and gets both numerators and both denominators. If they are both equal, then the function returns true, otherwise false. (Remember: your Fractions class stores the numerator and denominator in reduced form.) Building Multiple Classes Class Fraction This class should be a simple abstraction (i.e., a small class) that represents the ratio of two numbers. There will be only two data elements, and only a few methods. Note that it is required for your Fraction class to store your ratio in reduced form, so this is a feature to implement in your software. Fractions are not allowed to have zero as the denominator. So if someone tries to set the denominator to zero (either in the constructor or in a setter method), don't let them -- and if you need to, supply an alternate default value. Data Members The numerator - what primitive type makes the most sense here? The denominator - same type as the numerator. Method Members Overloading Fraction() - default "no-arg" constructor o Fraction(int,int) - constructor which initializes your data Overriding boolean equals(Object other) - compares "this" to "other" To make it easier on you, if you want, you can make the signature this instead: boolean equals (Fraction other) Getters & Setters o getNumerator() o setNumerator(...) o getDenominator() o setDenominator(...) Other: o a toString() method might be a good idea! Class FractionCounter This class should also be small, and contain only a few state variables. The purpose of this class is to store a reference to a Fraction object and a count of how many times this fraction object has been seen in the input file. Data Members The Fraction - declare this class variable to be of type Fraction from above The counter - the integer value used to count the number of these fractions seen Method Members Fraction Counter( Fraction theFraction) - constructor bool compareAndIncrement( Fraction newFraction) - used to see if the newFraction passed into this function is the same as the Fraction we're storing, and if so increments the counter by one and returns true (otherwise, returns false). String toString() - replace this inherited method with one that prints out both the Fraction and its count: a string like, "1/2 has a count of 2" Testing and Boundary Cases Before submitting your assignment, be sure to thoroughly test it - for example, if your software fails to compile, the maximum grade falls to a 75%. Does your software reduce fractions? Test this common case. What about boundary cases or unusual situations? For example, does your software read in the fraction "4/0" and crash with a DivideByZeroException? Can your software handle O fractions? 1 fraction? 1,000 fractions? What does your software do with the fraction "-1/-4"? What if the numerator or denominator is very large, as in "1/9999"? Does your program report the fraction "1/1 occurs 3 times" only once (as it should), or does it report "1/1 occurs 3 times" multiple times (which it should not)? If you store the fraction using doubles, does the equals() function suffer from roundoff errors? Test your code for as many cases as you can think of by varying the "fractions.txt" input file. Note that I will try multiple "fractions.txt" input files when grading your assignment.

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

Systems analysis and design

Authors: kenneth e. kendall, julie e. kendall

8th Edition

135094909, 013608916X, 9780135094907, 978-0136089162

More Books

Students also viewed these Programming questions

Question

List the four major steps in building decision trees.

Answered: 1 week ago

Question

What is UML?

Answered: 1 week ago