Assignment #16
Instructions
-
The purpose of this assignment is to practice writing a class, creating objects of this class,
storing objects in a linked list, and modifying the object that are in the linked list.
-
Download these four (4) files to your desktop:
LinkedList.java
ListInterface.java
ListException.java
Node.java
-
Write a Java application that reads from an input file,
which contains your data from Assignment #9 in CSV (Comma Separated Values) format,
stores the data in a linked list,
and outputs the linked list to the screen.
We are re-doing Assignment #9, but using a linked list instead!
-
The 1st step is to make your CSV file.
Open up jGRASP. Cick: File, New, Plain Text, to create a new text file.
Then, save the file as a CSV file by adding the .csv file extension when you save it: filename.csv
Otherwise, open up a CSV excel file and type in your data.
The 1st row should be the column names, separated by commas.
The 2nd row, 3rd row, etc. should be the data from Assignment #2.
Add a little more data, so you have at least 10 rows of data.
Here is an example CSV file from the grocery list program: groceries.csv.
And here is an exaple CSV file from my "Marine Mammals of Hawai'i" example: mammals.csv
** Make sure you submit your CSV file with your assignment. **
-
The 2nd step is to write your program!
Here is starter code: LastnameFirstname16.java
You will have two classes - your
public class LastnameFirstname16
and class HawaiianTheme
.
** The class HawaiianTheme
does NOT have the public modifier. **
** The class HawaiianTheme
is NOT nested inside public class LastnameFirstname16
. **
** The class HawaiianTheme
is a class definition,
which contains the variables (data fields) and methods to define objects, which store data. **
-
class HawaiianTheme
is the type of data that you stored in your input file.
For example, I am using "Marine Mammals of Hawai'i", so my class is: class MarineMammalsOfHawaii
-
The 1st commandline argument (args[0]) is the name of your input file that you just created.
Have some error checking to make sure there is only one command argument.
-
In your main() method, make a linked list to store objects of class
class HawaiianTheme
.
See example file LinkedListDriver.java for examples of declaring a linked list of objects.
-
The next step is to initialize the array with the data from the file.
Earlier this semester, we read a file and stored it in an array with this program:
ArrayBasedGroceryList.java.
For this assignment, we do not use arrays. We are using a linked list.
-
For each row in the CSV file, create a HawaiianTheme object.
-
Then, use the add() method to add each HawaiianTheme object to the linked list.
See example files LinkedListDriver.java
and GroceryMenu.java
for examples of adding objects to a linked list.
-
After adding all the data from the CSV file to the linked list,
display the linked list by using the toString() method.
-
Loop through the linked list and use the get() method to store the address of each object in a variable.
See example file LinkedListDriver.java
and GroceryMenu.java
for examples of the linked list get() method.
-
Use get() and set() methods (or other methods you wrote) to modify all the data fields in each object.
-
After updating all the data fields in all the objects in the linked list,
display the linked list by using the toString() method.
-
Before we store data in our linked list, and update the data fields in each object in the linked list,
we have to create data fields, a constructor, and toString() method for
class HawaiianTheme
.
This was done in Assignment #8 and #9. Also see instructions below.
-
Below your
public class LastnameFirstname16
class,
create data fields, a constructor, and toString() method for class HawaiianTheme
.
-
Create three data fields - one data field for each of the attributes,
which is the same as the three column names, in your
class HawaiianTheme
.
For example, for my class MarineMammalsOfHawaii
, I have these data fields: name, population, and length.
-
Write the constructor for your
class HawaiianTheme
. You should have a three parameters, which initialize your three data fields.
For example, I have three parameters of type String, Integer, and Double in my constructor.
-
Write the
toString()
method for your class HawaiianTheme
.
The return value should return a String with the three data fields, so they can be displayed.
-
See SimpleClass.java
for an example of a class, constructor, toString() method, and an array of objects.
-
The next step is to make get() and set() methods, so you can change each data field as you wish. You should have three (3) get() methods - one get() method for each data field. And you should have three (3) set() methods - one set() method for each data field.
Or, you can make a single method that also updates each data field.
You should have three (3) methods - one method for each data field you are updating.
-
After you make the changes to your data fields for each element,
print the array of HawaiianTheme objects to the screen again.
-
See SimpleClassWithMethods.java
for an example of a class, constructor, toString() method,
an array of objects, get() methods, set() methods, and other update methods.
-
Make sure your code follows the ICS 211 Java Coding Standard, in particular the Java documentation (Javadoc) comments that go above each method.
- Write your original comments every 3-5 lines of code.
-
WARNING: In the edit method, do NOT copy my code or my comments.
Use my code as a guide to write your own code.
Example Output
You output will be similar, but not the same, as my output.
The formatting doesn't have to be exactly the same, but make sure it is neat and easy to read.
Here are details on the format() method, if you so wish to use it:
https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
Or, in your CSV file, just make sure you leave extra spaces to evenly space the words.
Or, maybe you have your own unique way to do the formatting!
** Make sure you submit your CSV file with your assignment. **
Below is example output for using this input file as the first commandline argument: mammals.csv
Read from input file: mammals.csv
Display MarineMammalsOfHawaii linked list
after adding objects from the input file into a linked list:
# name population length
1, Hawaiian monk seal 1100 2.40
2, humpback whale 10000 16.00
3, spinner dolphin 3351 2.35
4, common bottlenose dolphin 235 3.50
5, Risso's dolphin 85000 4.00
6, rough-toothed dolphin 150000 2.83
7, striped dolphin 2000000 2.60
8, pygmy killer whale 817 20.50
9, false killer whale 150 2.80
10, melon-headed whale 2950 3.00
11, short-finned pilot whale 8850 3.70
12, sperm whale 7082 17.30
13, dwarf sperm whale 19000 3.00
14, pygmy sperm whale 50 3.50
15, orca 50 10.70
16, Blainville's beaked whale 2200 5.00
17, Cuvier's beaked Whale 13000 8.30
18, pantropical spotted dolphin 3000000 2.50
Display MarineMammalsOfHawaii linked list
after changing the data in each object in the linked list:
# name population length
1, HAWAIIAN MONK SEAL 1101 7.87
2, HUMPBACK WHALE 10001 52.49
3, SPINNER DOLPHIN 3352 7.71
4, COMMON BOTTLENOSE DOLPHIN 236 11.48
5, RISSO'S DOLPHIN 85001 13.12
6, ROUGH-TOOTHED DOLPHIN 150001 9.28
7, STRIPED DOLPHIN 2000001 8.53
8, PYGMY KILLER WHALE 818 67.26
9, FALSE KILLER WHALE 151 9.19
10, MELON-HEADED WHALE 2951 9.84
11, SHORT-FINNED PILOT WHALE 8851 12.14
12, SPERM WHALE 7083 56.76
13, DWARF SPERM WHALE 19001 9.84
14, PYGMY SPERM WHALE 51 11.48
15, ORCA 51 35.10
16, BLAINVILLE'S BEAKED WHALE 2201 16.40
17, CUVIER'S BEAKED WHALE 13001 27.23
18, PANTROPICAL SPOTTED DOLPHIN 3000001 8.20