How do I solve this Java code?
-
I am having trouble with this code. This is our last homework assignment and I am just stumped on how to solve it. Consider the "List" class, and its "Node" class, Now, assume that the Node class is changed to this: class Node { public Object data; public Node next; public int priority; //THIS LINE IS NEW public Node() { this.data = null; this.next = null; this.priority = 10; //THIS LINE IS NEW } public Node(Object a) { this.data = a; this.next = null; this.priority = 10; //THIS LINE IS NEW } public Node(Object a, int c) //THIS CONSTRUCTOR IS NEW { this.data = a; this.next = null; this.priority = c; } public Node(Object a, Node b, int c) { this.data = a; this.next = b; this.priority = c; //THIS LINE IS NEW } } //end Node class The "priority" property (of a Node object) specifies how "important" its "data" property is. You can assume that "priority" only takes int values from 1 to 10, inclusive. Smaller numbers represent higher priority, larger numbers represent lower priority. (Example: 1 means the "data" is extremely important, 10 means the "data" is unimportant.) Create an extra "insertWithPriority" instance method, which you'll add to the List class. The "insertWithPriority" method receives these as parameters: * a String data item, such as "Suzi" * an integer priority value, from 1 to 10. Hint: public void insertWithPriority(String s, int p) As you know, the List class's insertAtFront(Object obj) method inserts a data item, such as "Suzi" or cat1 or dog1, at the front of the list. The List class's insertAtBack(Object obj) method inserts a data item at the back of the list. Your "insertWithPriority" method is similar, but should instead insert a specified data item, such as "Suzi", at this position: * The data item should come "after" (later in the list) than all data items which have the same or higher priority. * The data item should come "before" (earlier in the list) than all data items which have lower priority. * Repeating myself: high priority = small priority number, low priority = high priority number Note: For ease, you can assume that when a Prog1 uses "insertWithPriority", it will ONLY use insertWithPriority, and will not use insertAtFront or insertAtBack. (Because, these other functions may result in a scrambled list, which may then cause your insertWithPriority to work incorrectly.) " --------------------------------------…
-
Answer:
(There sure are a lot of LinkedList homework assignments lately)... Do you know compareTo() ? String uses compareTo() to sort. The wrapper classes do the same. Recall we have Types: int, byte, double. When we use the Wrapper classes such as Integer, we get the compareTo() for free. You need to use Integer for this priority feature. Let me refer you to a free, online textbook, Chapter 9. It has example code almost ready to go. I can't give a code example because this is a two piece problem. You have the class Node, and you have another class with the main(). The class with the main() has the LinkedList.
Nicholas P at Yahoo! Answers Visit the source
Related Q & A:
- How can I debug my php code?Best solution by Stack Overflow
- How can I add a HTML code to My Yahoo homepage?Best solution by Yahoo! Answers
- How do I solve the problem of downloading word document attached to my email?Best solution by support.google.com
- How do I solve a DNS error?Best solution by Yahoo! Answers
- How do I solve the coin problem?Best solution by Quora
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.