-flesh out the creative bit a little, make them do more?
This lab will be (tentatively) due on Thursday, March 19 at 11:59pm. For this lab, you will write most of your methods in one class named MoreInput. You will also write some other classes to help you parse the data. You can download example files at this link: https://drive.google.com/drive/folders/1j-yK11eyVYLTFBCq3mYi1Vsyy6LuDMmX?usp=sharing . Please feel free to change the values in the file to make sure it works in different situations. When I grade your code, I will insert files with the same name but different values and test it with those.
This lab contains several files you will read data from (examples here: https://drive.google.com/drive/folders/1j-yK11eyVYLTFBCq3mYi1Vsyy6LuDMmX?usp=sharing ):
-numberlist.txt : each row of this file has a varying number of ints separated by commas
-cerealdata.csv : each row of this file represents a cereal and includes the name of the cereal, the calories, amount of fiber, amount of carbs, and size in cups of each serving [remember that a CSV file is roughly the same as a text file, with each entry in a row separated by commas]
-weather.txt : each row of this files gives a date and the low and high temperature on that date
In the MoreInput class, please write the following methods (ALL METHODS IN THIS CLASS WILL BE STATIC!):
Write a method named sumString( ) - this method will input a String and return an int. As a precondition, assume the input String is a series of ints separated by semi-colons and spaces like "22; 10; 2; 9". This method should return the sum of all those numbers. With this input, it should return 43. If I send this method "5; 0; 6; 0; 0; 0; 0; 0; 0; 3" it would return 14. Note that this method does not read from a data file, you are just getting practice with split and parse.
Write a method named takeGPA( ) - this method will input a String and return a double. As a precondition, assume the input String is a String with a name, a comma, a space and then an int like "Paula Bae, 4.81". Your method should return the double at the end of the String (in this case, 4.81"). Again, this method is just getting practice with parsing and will not read from a data file.
Write a method named howMany() - this method takes no inputs and reads its data from a file named numberlist.txt. It should return an int representing how many numbers are in the file. For example, if the text file has the following data:
6,7
2,4
2,4
6,7,6,7
1,0,0,0,2
Your method should return 15, as there are 15 numbers in the file (on different lines and separated by commas).
Write a method named totalSum() - this method takes no inputs and reads its data from a file named numberlist.txt. It should return an int representing the sum of all of the numbers in the file. In the above example, this would be 54.
NOTE: For the next few methods, you will be reading data from the cerealdata.csv file.
First, create a Cereal class that will store the name, calories, fiber, carbs, and size in cups of each Cereal. You do not need anything fancy, just a simple class that inputs String, int, int, int, double, sets up the instance variables and has accessor methods. As you loop through the file in your methods, create a new Cereal program for each row to help you store everything.
Then, write the following methods in your MoreInput class. At the start of each method, you will want to loop through the data file, read in one row at a time with nextline, use split and parse to break that String down into different parts, and create a Cereal object. This code will be identical in each method and cshould be copy pasted.
enoughFiber(int x) - this method inputs an int and reads its data from cerealdata.csv. It should return a boolean representing whether or not there is at least one cereal in the list that has at least x (the parameter) grams of fiber. For the example file attached above, enoughFiber(8) and enoughFiber(14) would return true, but enoughFiber(20) would return false.
carboLoading() - this method takes no inputs and reads its data from cerealdata.csv. It should return a String representing the name of the cereal with the most carbs in it. For the example file above, if I called carboLoading(), it would return "Rice Chex" as that is the only cereal with 23g of carbs. If there is a tie, it could return either one. Load up, bro.
randomCereal(int calories) - this method takes an int for an input, reads data from cerealdata.csv, and returns a String (not a Cereal object). It should return the name of a random cereal with calories less than or equal to the parameter to this method. First, add all the relevant cereals to an ArrayList, then pick a random one from that to return.
containsPhrase(String phrase) - this method inputs a String, reads data from cerealdata.csv, and returns an ArrayList<String> with the name of each cereal that contains phrase somewhere inside the name. For example, if I call containsPhrase("Delight") it would return the list ["Almond Delight"]. containsPhrase("Cheeri") would return the list ["Apple Cinnamon Cheerios", "Cheerios", "Honey Nut Cheerios", "Multi-Grain Cheerios"]. containsPhrase("borish") would return an empty list [ ].
carbsPerCup(String inputname) - this method inputs a String, reads data from cerealdata.csv, and returns a double. It should return the amount of carbs per cup of the cereal with the exact name given in the input. If there is no cereal with that name, it should return 0.0. For example, carbsPerCup("Apple Jacks") should return 11.0 (11 grams per 1 cup serving). carbsPerCup("Crispy Wheat & Raisins") should return 14.666666 (11 grams per 0.75 cup serving). carbsPerCup("Gravel Charms") should return 0.0. It's all about the macros, bro.
NOTE: Now we will be reading data from the weather.txt file. This file stores the high and low temperatures each day of a recent (randomly generated) summer.
Begin by writing a simple WeatherDay class (or whatever you want to call it). This class will store 4 ints representing the month, day, high temperature, low temperature, and have the usual accessors.
Now write the following methods in the MoreInput class, which all read from the weather.txt file:
getSpread(int mon, int day) - This method inputs a month and day and returns an int for the spread between the high and the low that day. For example, if you call getSpread(7,4), it should return 13, because the high was 76, the low was 63, and 76-63 = 13. You can assume that the input month and day will be included in the data file. (again, the tricky part of these methods is parsing each line into the four relevant pieces of data. Copy paste this part in all the methods once you figure it out once)
monthHigh(int mon) - This method inputs a month and will return and int for the highest temperature during that month. In the example data, monthHigh(6) would return 94 (which was reached twice in the month on 6/2 and 6/8). You can assume that the input month will be included in the data file.
coldestDay(int mon) - This method inputs a month will return an int for the day on which the lowest low temperature was recorded. If multiple days have the same low, you could return either one. In the example data, if you call coldestDay(6), it would return either 10 or 26, because those were the two days in June with the lowest temperature (55). You can assume that the input month will be included in the data file.
averageHigh(int mon) - This method will input a month and return a double representing the average high temperature in that month (you should disregard the lows for this method). For example, if you call averageHigh(8), it should return 97.1612903... with the example data. You can assume that the input month will be included in the data file.
The data is good! For this first part of your lab, please submit MoreInput.java, Cereal.java, WeatherDay.java, numberlist.txt, cerealdata.csv, and weather.txt (and any other classes you may have created).
For the second part of the lab, you will write a program called CreativeData.java. In this program, you will do an independent data analysis on something which interests you. Pick something you think is cool! Here is a general idea of what to do:
Identify a data set looks interesting to you
Come up with a question(or multiple) you would like to answer (calculate weather trends? Calculate batting average leaders in baseball stats? Identify which types of soda have the most sugar per ounce? Up to you!)
Create a class to model one entry of your data set.
Write a program with a main method to read the data from your into your program.
Use an ArrayList and the created class to store data that have been read.
Write code to process the data and determine an answer to your question(s).
You should be able to find a good .csv file of data from a source like http://www.kaggle.com (this site requires a free account, which can be your school Google account, in order to access and search data sets) or http://www.data.gov. Other sources are fine too.
Extra credit is available for in depth analysis that goes above and beyond.
Make sure to submit your dataset as well as CreativeData.org When I run the main method of your program, it should print out what questions you wanted to answer, what you found, and any other information you thought was cool! Have fun!