Summary Build two classes (Fraction and FractionCounter) and a Driver for use in counting the number...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Related Book For
Systems analysis and design
ISBN: 978-0136089162
8th Edition
Authors: kenneth e. kendall, julie e. kendall
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
The income statement for Performance Limited is given below along with some supplementary information. Performance Limited Income Statement for the year ended December 30, 2020 (in $000s) Division A...
-
In the context of a government audit, what is an opinion unit, and of what significance is an opinion unit to the auditor?
-
A machine with a cost of $ 2 0 0 0 0 0 & useful life of 8 yearswas acquired at the beginning of year 1 . The machine isrevalued every 2 years and the revaluation reserve will beregarded as realized...
-
What are the three basic steps involved in settling a case? How can the paralegal assist the lawyer with each of these steps?
-
If an asset that is not fully depreciated is sold or disposed, but the fixed asset records are not adjusted, what effect will this have on the financial statements?
-
Using the DE-Goodman criteria and a design factor of 1.5, calculate the diameter based on the shaft's loadings and your guess for the shaft's diameter at the critical location, what is the shaft...
-
You are working on a free-form Packet Tracer challenge activity as seen in Figure 1, you have been given the London Railways network.' The purpose of this EMA question is to build upon each of the...
-
12. 2 sine sin20 2 sin 0 + sin 20 tan 2 %3D
-
Show the output of the following program fragment when start () method is called: static void start() ( int x 1, y = 2, z = 3; int ansi fun1 (x, y); int ans2 fun2 (ansi, funi (x, y), z); )...
-
Pastina Company sells various types of pasta to grocery chains as private label brands. The company's reporting year-end is December 31. The unadjusted trial balance as of December 31, 2024, appears...
-
The application of new technologies in business is transforming the landscape of International Trade and pushing the frontiers of how business is done domestically, as well as internationally. While...
-
A company produces three products, X, Y, and Z. Each unit of Product X requires 2 hours of labor, 1 hour of machine time, and 3 hours of assembly time. Each unit of Product Y requires 1 hour of...
-
Use a DCF valuation to estimate the company's worth. Discuss the pros and cons of using this method. Cash and cash equivalents Trade receivables Receivables from financing activities Inventories...
-
Q4. PH of the buffers solution depends upon the concentration of * Acid Conjugate Base Acid and conjugate base Q5. Buffers are mixtures of * Strong acid and strong base Strong acid and weak base Weak...
-
Will the prediction interval always be wider than the estimation interval for the same value of the independent variable? Briefly explain.
-
List the four major steps in building decision trees.
-
Using the interview data you gained from the group exercise on Maverick Transport in Project 1, your group should meet to design an email or Web questionnaire for surveying the companys 20...
-
What is UML?
-
Solve Problem 33 with (a) \(T=2 \mathrm{~s}\), and (b) \(T=\) \(0.5 \mathrm{~s}\).
-
Solve Problem 33 with \(\zeta=1.0\).
-
A body is subjected to the two harmonic motions, \[ \begin{aligned} & x_{1}(t)=100 \sin \left(\omega t+\frac{\pi}{3} ight) \mathrm{mm} \\ & x_{2}(t)=50 \sin \left(\omega t+\frac{\pi}{6} ight)...
Study smarter with the SolutionInn App