Assignment #18

Instructions

  1. The purpose of this assignment is to sort the data from your CSV file, by adding your data into a linked list and sorting the data with a bubblesort() method that sorts the linked list. And the purpose is to understand how to loop through the nodes in a linked list.
    1. Download these four (4) files to your desktop:
      LinkedList.java
      ListInterface.java
      ListException.java
      Node.java
    2. Add implements java.lang.Comparable<Nature> to the very first line of your class Nature definition, so that we can use it to sort objects in the linked list, which uses template class T. Here is similar example code: ComparableItem.java
    3. Add the compareTo() method to class Nature to sort any data field that you wish. We will make slight modifications to the compareTo() method from assignment #11, so that it works with template class T. Use class Nature (instead of class Object) as the class of the parameter to the compareTo() method. Here is similar example code: ComparableItem.java
    4. Do not change any code in the LinkedList.java program. Instead, we will make a subclass of LinkedList called MyLinkedList and add the bubblesort() method definition inside. MyLinkedList inherits all the data fields and methods of LinkedList, so the very first line of the class MyLinkedList definition will be:
      class MyLinkedList<T extends java.lang.Comparable<T>> extends LinkedList<T>
    5. In previous programs, we used class LinkedList to make a LinkedList object. For example, this code creates a LinkedList object called "list":
      LinkedList<Nature> list = new LinkedList<Nature>();
      Class MyLinkedList is a subclass to the class LinkedList. Class MyLinkedList has method bubblesort(), which class LinkedList does not have. So we have to use class MyLinkedList to make a MyLinkedList object, so that we can sort the linked list with the bubblesort() method. For example, this code creates a MyLinkedList object called "list":
      MyLinkedList<Nature> list = new MyLinkedList<Nature>();
    6. The bubblesort() method uses the bubblesort algorithm to sort the Nature objects in the class LinkedList, but the code is similar to the code in the class LinkedList's add(), get(), remove(), and toString() methods, which loop through the nodes in the linked list. In the bubblesort() method, you need to swap the values stored in the data field of the adjacent Nature objects (nodes that are right next to each other) that are out of order, and you need to move from one Nature object to the next Nature object in your loop. Depending on how your write your code, you will probably need to cast from datatype T to datatype Nature. Then, use get() and set() methods to swap the data field values between two nodes that contain Nature objects that are out of order. As an alternative to get() and set() methods, you could also write a swap() method in the class Nature to swap the datafield values between two Nature objects.

      ** You will NOT use arrays in this program. **
    7. The method call to your bubblesort() method is similar to the method calls in the class LinkedList. These method calls are instance (non-static) method calls. Instance (non-static) method calls potentially change the data in an object, or return some data about the object, so we need to use the object with the method in this format:
      object.method(parameters);
      For example, in the LinkedListDriver.java program, we call the add() method from class LinkedList on a LinkedList object called "list", so we can add a letter (of datatype String) to the linked list:
      list.add(letter);
      As another example, in the LinkedListDriver.java program, we call the get() method from class LinkedList on a LinkedList object called "list3", to get the Item object stored in the 2nd node in the linked list:
      Item food = list3.get(2);
      Your method call to bubblesort follows the same pattern:
      list.bubblesort();

      In contrast, a static method call is a method call for the class, so we need to use the class with the method in this format:
      ClassName.method(parameters);
      For example, in the Factorials.java program, we called the recursive() method from class Factorials by using this code: Integer result2 = Factorials.recursive(number);
      As another example, in the Sorting.java program, we called the bubbleSort() method from class Sorting by using this code: Sorting.bubbleSort(array);
    8. For examples on how to loop through the nodes in a linked list, see the NodeDriver.java program, and the add(), get(), and remove() methods of LinkedList.java program.
    9. Here is starter code: LastnameFirstname18.java Note that bubblesort() has NO parameters. Also it has NO return value. It is NOT a static method. Don't forget to add Javadocs that describe what your bubblesort() methods does and how it works.
  2. Make sure your code follows the ICS 211 Java Coding Standard, in particular the Java documentation (Javadoc) comments that go above each method.
  3. Write your original comments every 3-5 lines of code.
  4. 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 sorting linked list with bubblesort() method:
#  name                        population   length
1, Blainville's beaked whale        2200      5.00
2, Cuvier's beaked Whale           13000      8.30
3, Hawaiian monk seal               1100      2.40
4, Risso's dolphin                 85000      4.00
5, common bottlenose dolphin         235      3.50
6, dwarf sperm whale               19000      3.00
7, false killer whale                150      2.80
8, humpback whale                  10000     16.00
9, melon-headed whale               2950      3.00
10, orca                               50     10.70
11, pantropical spotted dolphin   3000000      2.50
12, pygmy killer whale                817     20.50
13, pygmy sperm whale                  50      3.50
14, rough-toothed dolphin          150000      2.83
15, short-finned pilot whale         8850      3.70
16, sperm whale                      7082     17.30
17, spinner dolphin                  3351      2.35
18, striped dolphin               2000000      2.60