PLEASE HELP IN JAVA: Create a doubly-linked list containing nodes with FeetAndIn
ID: 3883060 • Letter: P
Question
PLEASE HELP IN JAVA:
Create a doubly-linked list containing nodes with FeetAndInches objects. Input will be from the keyboard and of the form int int (two ints per line). The first int will be the feet, the second int the number of inches, and the user will enter 0 0 to stop. After you have built your list, print it out forwards and then in reverse. (you should write two different print methods). Insert the nodes into the list sorted.
What I have so far:
//feetInch class
public class FeetAndInches {
private int feet;
private int inches;
public FeetAndInches() {
feet = 0;
inches = 0;
}
public FeetAndInches(int newf, int newi) {
feet = newf;
inches = newi;
}
public void setFeet(int newf) {
feet = newf;
}
public void setInches(int newi) {
inches = newi;
}
public int compareTo(FeetAndInches c) {
int thisInches, inches;
thisInches = this.feet * 12 + this.inches;
inches = c.feet * 12 + c.inches;
if (thisInches < inches) {
return -1;
} else if (thisInches > inches) {
return 1;
} else {
return 0;
}
}
public String toString() {
return this.feet + " feet and " + this.inches + " inches";
}
}//end FeetAndInches class
//node class double linked list
public static class Node {
Object item;
Node next;
Node p;
Node(Object newItem) {
item = newItem;
next = null;
p = null;
}
Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
}
}
public static void main(String[] args) {
//promt user for input
System.out.println("Enter the feet and inches followed by 00: ");
Node temp= new Node();
}
// method to print list
public static void PrintList(Node head) {
Node curr = head;
Object i;
while (curr != null) {
i = curr.item;
System.out.println(i);
curr = curr.next;
}
}
// method to print list in reverse
public static void PrintList1(Node head) {
Node curr = head;
Object i;
while (curr != null) {
i = curr.item;
System.out.println(i);
curr = curr.next;
}
}
}
Explanation / Answer
FeetAndInches fi = new FeetAndInches(feet, inches);
Now data for Node would be FeetAndInches class object "fi" , and when you print the node contents then you can use the below algorithm for insertion and print ...
Give it a try yourself buddy will learn better...The algo is:
Note : Add java.io.BufferedReader for bufferedInputStream class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.