Concept explainers
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 also reuse the ObjectList class we built in lab to store our list of unique FractionCounters, instead of directly using arrays or the ArrayList.
Remember
- NO DECIMALS!
- Handle input of any length
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 one line at a time and build a Fraction object from A and B. For each unique Fraction seen, your program will create a FractionCounter object used to track the number of occurrences of that specific fraction. When all the input is consumed, your program will print out its ObjectList of unique FractionCounters, 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
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 feature to implement in your software.
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
- Fraction(int,int) – constructor which initializes your data
- Overriding
- boolean equals(Fraction other) – compares “this” to “other”
- String toString() – replace this inherited method with one appropriate to the class
- Getters & Setters
- getNumerator()
- setNumerator(…)
- getDenominator()
- setDenominator(…)
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
- FractionCounter( 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
Error handling
- When encountering illegal data, print an error message, skip the data, and continue processing
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 0 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 4 times” only once (as it should, for the fractions.txt file linked in Fractions V1), or does it report “1/1 occurs 4 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.
NB: You need to code your program it so it can run with input files besides the example one. In particular, it has to work with input files that are of any length. You must enable the appropriate lists to dynamically resize.
OBJECT LIST
// pseudo code: when adding element
if new count exceeds size
{
create new larger array
copy elements from original array into new array
assign new array to array instance variable
update maxSize
}
// Minimal interface for this assignment ***
pubic ObjectList()
public void add(Object obj)
public String toString()
// Complete Interface
public String toString()
public int indexOf(Object obj)
public int getNumElements()
public Object getAt(int i)
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- Write code so that you make another image below the image currently in the code so that they are next to each other but not touching each other. And Translate - A function that moves the graphic according to the given input . Grow - a function that increases the size of the graphic according to the given input public class BasicGraphicViewer{ public static void main (String[] args) { Rectangle r1 = new Rectangle(100,10,20,30); r1.draw(); Picture p1 = new Picture(); p1.load(“someimageurl”); // you can put any image link here. p1.draw(); } }arrow_forwardRename the show method to people Override the show method in student and in the other class you created (this will be the polymorphic method) In main program, create an array of 3 objects of class people Instantiate each array position with a different object (people, student, other) With a loop, display all the objects in the array.arrow_forwardThe input to this method is supposedly an ArrayList of integers that ranges from 1 to 100, however this is not exactly the case. Sometimes, there will be gaps in the range for example, the range could be: [1 2 ... 7 8 ... 99 100], with missing ranges in between. Find all the missing numbers from the range, add them to an ArrayList, return the ArrayList.------------------------ Below is my code that is failing. It is returning the correct missing numbers but is not going passed the last number from the array. so [4,5,14,16,23,76,89] It is placing all of the missing numbers up to 88 when it should be going to 100 The solution should only return the missing numbers from 1-100. The original numbers from the given array should not be included in the return. How can I only return the missing numbers? ------------------------ public ArrayList<Integer> solution(ArrayList<Integer> nums) {ArrayList<Integer> result = new ArrayList<Integer>();int count =…arrow_forward
- Execute the following statements over initially empty ArrayList myList one after another (cascade). Explain what was changed after each statement (what did it do?) myList.add("z"); ArrayList content: Your explanation (where this output came from): myList.add(0, "a"); ArrayList content: Your explanation (where this output came from): myList.add("t"); ArrayList content: Your explanation (where this output came from): myList.add(2,"w"); ArrayList content: Your explanation (where this output came from): myList.set(0,"b"); ArrayList content: Your explanation (where this output came from):arrow_forwardWrite a program that includes in its MainClass the following methods: 1. A method called printArray that takes an array of integer values and a boolean value. The method prints the array elements on the same line with one space between each two elements. If the boolean value is true, the elements must be printed in their original order, if it is false they must be printed in reverse order. The method should print a new line after printing the array elements.arrow_forwardThere are two different methods you to add a range to an ArrayList.These methods are AddRange and InsertRange.implement a c# program to demonstrates how these two methods are used:arrow_forward
- There are two different methods you to add a range to an ArrayList.These methods are AddRange and InsertRange.implement a c# program to demonstrates how these two methods are used:arrow_forwardJava this piece of my code is not working .....I am trying to add Warship with a type or warship , but I keep getting an error ? //add another WarShip instance to the ArrayList. //use a foreach loop to process the ArrayList and print the data for each ship. //count the number of ships that are not afloat. Return this count to main for printing. //In the shipShow method: the number of ships that are still a Float public static int shipShow(ArrayList<Ship> fleetList) { //from the ArrayList, remove the CargoShip that was declared as type Ship. fleetList.remove(2); //Removing ElFaro form the list // review Type casting instanceof | up casting Ship < Warship , subclass is a object of its parents //add warship USS John Warner - add cast by (Ship) new WarShip Ship warship2 = (Ship) new WarShip("USS John Warner", 2015, true, "attack submarine", "United States"); fleetList.add(warship2); int count= 0; //use a foreach loop to process the ArrayList and print the data for each ship.…arrow_forwardPATTERNS: Shapes Using Python, recreate (as close as possible) the pattern given using only one class - the Pattern class. You are only allowed to use PIL.Image and PIL.ImageDraw from the Pillow Library, and pi, cos, and pi. Its data attributes are as follow: width_height – 2-digit tuple containing the width and height of the image space – Image object (with 8-bit RGB color space, specified size, and background color) draw – Image object that performs as the drawing context of the space background – 3-digit tuple of the 8-bit RGB background color of the image or picturearrow_forward
- Hello! I need some help with my Java homework. Please use Eclipse Please add comments to the to program so I can understand what the code is doing and learn Create a new Eclipse project named so as to include your name (eg smith15 or jones15). In this project, create a new package with the same name as the project. In this package, write a solution to the exercise noted below. public static double listAdder(ArrayList<? extends Number> list) { ???} Examine the listAdder method fragment above. Complete the method so that it displays the list elements all on one line and returns the sum of the elements. Demonstrate the method by calling it in main with ArrayLists of three different classes that extend class Number. The operator instanceof can test for object type which would be necessary to have in a foreach loop for the data types types supported ( Integer , Byte and Double ) SAMPLE OUTPUT 12 21 7 16 8 13 Integer list totals 77.0 1.1 2.2 3.3 4.4 5.5 6.6 Double list…arrow_forwardJAVA you guys returned the question and said that it is a writing assignment but it is not. It is a java question, multiple-choice no coding. Please help me with this.arrow_forwardImplement the above system using inheritance in the best possible way. Keep every object size as small as possible. Implement all methods (setter/getter/constructors and destructors) Note that the region area is 0 while the city is len*width and the country is the sum of their cities. Create array of countries called Arab of 22 countries. Write a function fill that fills the array Arab Write a method that finds the city that has the max area in a country Write a method that sorts the cities in a country from the largest to the smallest area Write a function that returns array of countries of the same area of Arab Write a function that compares between two countries. It returns true if country1 area greater than country2 area. Write a function to move a city from one country to another.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education