Question
//LinkedList.java public class LinkedList { private Node firstNode = null; private class Node { Contact value; Node next = null; private Node(Contact c) { value
//LinkedList.java
public class LinkedList { private Node firstNode = null;
private class Node { Contact value; Node next = null; private Node(Contact c) { value = c; } public String toString() { return value.toString(); } }
public void add(Contact c) { Node newNode = new Node(c); if(firstNode == null) { firstNode = newNode; newNode.next = null; return; }
int compare = newNode.value.getName().compareTo(firstNode.value.getName()); if(compare
for(Node iterator = firstNode; iterator != null; iterator = iterator.next) { if(iterator.next == null) { iterator.next = newNode; return; }
Node nextNode = iterator.next; if(nextNode.value.getName().compareTo(newNode.value.getName()) > 0) { // newNode goes here iterator.next = newNode; newNode.next = nextNode; return; } } }
public boolean isInList(Contact c) { for(Node iterator = firstNode; iterator != null; iterator = iterator.next) { if(iterator.value.getName().equals(c.getName())) { return true; } } return false; }
public boolean remove(String s) { if(s.equals(firstNode.value.getName())) { firstNode = firstNode.next; return true; }
// Node we are removing is not the first Node in the list for(Node iterator = firstNode; iterator != null; iterator = iterator.next) { Node nextNode = iterator.next; if(nextNode != null && nextNode.value.getName().equals(s)) { iterator.next = nextNode.next; return true; } } return false; }
public void removeLast() { for(Node iterator = firstNode; iterator != null; iterator = iterator.next) { if(iterator.next == null) { firstNode = null; }
if(iterator.next != null && iterator.next.next == null) { iterator.next = null; } } }
public String toString() { StringBuilder returnString = new StringBuilder(); for(Node n = firstNode; n != null; n = n.next) { returnString.append(n).append(System.getProperty("line.separator")); } return returnString.toString(); } }
//Product.java
public class Product { private String name; private double price;
public Product(String n, double p) { name = n; price = p; }
@Override public String toString() { return "Product Name: " + name + " Price: " + price; } }
I. Update LinkedList so it can store sorted generic objects Create a new class called Product that has a String name and a double price. 2. Have it implement Comparable and define its compareTo method. a. b. Sort Product objects by their price 3. Add several Product objects to the updated LinkedList. Print them out (you can override Product's toString method) to verify that they sort properly by price I. Update LinkedList so it can store sorted generic objects Create a new class called Product that has a String name and a double price. 2. Have it implement Comparable and define its compareTo method. a. b. Sort Product objects by their price 3. Add several Product objects to the updated LinkedList. Print them out (you can override Product's toString method) to verify that they sort properly by priceStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started