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...
-
At the surface of a certain planet, the gravitational acceleration g has a magnitude of 12.0 m/s2. A 24.0-kg brass ball is transported to this planet. What is (a) The mass of the brass ball on the...
-
Discuss why planning is beneficial.
-
Use the technique discussed in Section9.4 to develop a GEE approach for zeroinflated Poisson model for count responses in longitudinal studies. Section9.4: 9.4 Marginal Models for Longitudinal Data...
-
The following is an excerpt from a note to the nancial statements of the city of Dallas (dates changed): The city prepares its annual appropriated general fund, debt service fund, and proprietary...
-
Objectives: Your company is currently planning to automate many different system administration tasks. Your manager has asked you to research a system administration task and implement it using...
-
Barlow Company manufactures three products: A, B, and C. The selling price, variable costs, and contribution margin for one unit of each product follow: The same raw material is used in all three...
-
Oxymoron, a large pharmaceutical firm, is developing a new iPhone app that will test for COVID-19. While the company has currently developed a version that is 80 percent reliable, they know that the...
-
Two (2) high ranking managers of Anrun Corp. know that the company's revenue is rapidly declining. However, at a recent shareholder meeting, they tell the shareholders to expect record profits in the...
-
1. A person weighing 900 N stands with their left foot on one bathroom scale, and their right foot on a second bathroom scale. 0 A: If their weight is divided evenly between each foot, how many...
-
Write an introduction about the company ( Microsoft Corporation) Find the organizations Mission & Vision Statement(s) and put this in your discussion Using academic research, identify what should be...
-
Kathy is the project manager, and the project concerns with developing an active prosthetic arm. Kathy is responsible for writing the human resources management plan. Part of the plan consist s of...
-
You took a 5-year $256,000 interest only loan. The APR is 10.7%/year and payments are monthly. What will be your loan balance ($) a year from, right after paying your 12th payment?
-
Explain the relationship between the effectiveness of monetary policy and the interest elasticity of investment. Will monetary policy be more or less effective the higher the interest elasticity of...
-
Test whether the 5-year survival rate for breast cancer is significantly different between African American and Caucasian women who are younger than 50 years of age and have localized disease....
-
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?
-
Define decision making as it relates to total quality.
-
The plant managers of ABC Corporations five plants have met at a common location at 8:00 am every Monday morning for 20 years. The people serving as plant managers have changed several times during...
-
New Piper, an aircraft manufacturer, issued a Mandatory Service Bulletin grounding 76 new aircraft manufactured during the first 5 months of 2004. Thirty-nine of the airplanes were in customers...
Study smarter with the SolutionInn App