PLEASE HELP IN JAVA: Create a doubly-linked list containing nodes with FeetAndIn
ID: 3886306 • 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. All I need help with is inserting the nodes into the sorted list.
My code:
//feetInch class
public static 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;
private Node prev;
Node(Object newItem) {
item = newItem;
next = null;
p = null;
}
Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
}
}
public static void main(String[] args) {
Node head = NodeFI();
printList(head);
printList2(head);
}
//InputNode
public static Node NodeFI(){
//Scanner
Scanner scnr = new Scanner(System.in);
Node head=null;
//Prompt user for Input
System.out.println("Input Feet and Inches, end with 00: ");
int feet = scnr.nextInt();
int inches = scnr.nextInt();
while((feet!=0) || (inches!=0))
{ //create a node and add to list
FeetAndInches newItem = new FeetAndInches(feet, inches);
Node temp = new Node(newItem);
head = makeList(head, temp);
feet = scnr.nextInt();
inches = scnr.nextInt();
}
return head;
}
//MakeList
public static Node makeList(Node list, Node temp){
if(list== null){
list=temp;
}else{
list.next=temp;
temp.prev= list;
}
return list;
}
//PrintList
public static void printList(Node head){
Node curr = head;
//while the head is not null
while (curr != null){
System.out.println(curr.item.toString());
curr = curr.next;
}
}
//Print Backwards
public static void printList2(Node head){
Node curr = head;
while(curr.next!=null){
curr=curr.next;
}
while (curr != null){
System.out.println(curr.item.toString());
curr = curr.prev;
}
}
}
Explanation / Answer
Explanation :-
----------------------------
Programme divided into 3 classes
FeetAndInches - this is node class , no need of another node class. it has next/previous elements
ListManager -- This class manages the linked list. Insert elements/ Print Elements
ListDemo - This calls is having main() method
Please check comments in code
FeetAndInches.java :-
--------------------------------------------
//feetInch class
public class FeetAndInches {
private int feet;
private int inches;
//Next FeeAndInches node to the current node
private FeetAndInches nextNode;
//Previous FeeAndInches node to the current node
private FeetAndInches prevNode;
public FeetAndInches() {
feet = 0;
inches = 0;
//Set next node to NULL by default
this.nextNode = null;
//Set previous node to NULL by default
this.prevNode = null;
}
public FeetAndInches(int newf, int newi) {
feet = newf;
inches = newi;
//Set next node to NULL by default
this.nextNode = null;
//Set previous node to NULL by default
this.prevNode = null;
}
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;
}
}
//Method to set the next node
public void setNextNode(FeetAndInches next)
{
this.nextNode = next;
}
//Method to set the next node
public void setPrevNode(FeetAndInches prev)
{
this.prevNode = prev;
}
//Method to get the next node
public FeetAndInches getNextNode()
{
return this.nextNode ;
}
//Method to set the next node
public FeetAndInches getPrevNode()
{
return this.prevNode;
}
public String toString() {
return this.feet + " feet and " + this.inches + " inches";
}
}//end FeetAndInches class
ListManager.java :-
---------------------------------------
import java.util.Currency;
/**
*
* @author
* This Class manages the linked list .
* Add/Remove nodes
* Print node in specified order
*
*/
public class ListManager {
//Root node
FeetAndInches head = null;
//Current Node
FeetAndInches tail = null;
public void insertNode(FeetAndInches node)
{
if(head == null){
head = node;
tail = head;
}
else
{
FeetAndInches cur = head;
//Find next
while(cur != null && cur.getNextNode()!= null && cur.compareTo(node) < 1){
cur = cur.getNextNode();
}
if(cur.compareTo(node) < 1){
node.setNextNode(cur.getNextNode());
cur.setNextNode(node);
node.setPrevNode(cur);
}
else{
if(cur.getPrevNode() != null){
cur.getPrevNode().setNextNode(node);
}
node.setPrevNode(cur.getPrevNode());
node.setNextNode(cur);
cur.setPrevNode(node);
}
if(node.getPrevNode() == null){
head = node;
}
//Set Tail/Last Node
tail = head;
while(tail.getNextNode()!= null)
{
tail = tail.getNextNode();
}
}
}
public void printForward(){
System.out.println("************* Printing Forard Direction *****************");
//Display Error Msg if List is Not initilaized
if(head == null){
System.out.println("No Items in the list");
return;
}
//Start at head
FeetAndInches node = head;
//Loop from Head to Node.Next is Null
do{
System.out.println(node.toString());
if(node != null)
node = node.getNextNode();
}
while(node != null);
System.out.println("************* Done. *****************");
}
public void printBackward(){
System.out.println("************* Printing Backward Direction *****************");
//Display Error Msg if List is Not initilaized
if(head == null){
System.out.println("No Items in the list");
return;
}
//Start at head
FeetAndInches node = tail;
//Loop from Head to Node.Next is Null
do{
System.out.println(node.toString());
if(node != null)
node = node.getPrevNode();
}
while(node != null);
System.out.println("************* Done. *****************");
}
}
ListDemo :-
---------------------------
import java.util.Scanner;
public class ListDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ListManager manager = new ListManager();
//Scanner
Scanner scnr = new Scanner(System.in);
//Prompt user for Input
System.out.println("Input Feet and Inches, end with 00: ");
int feet = scnr.nextInt();
int inches = scnr.nextInt();
while((feet!=0) || (inches!=0))
{
FeetAndInches node = new FeetAndInches(feet, inches);
//create a node and add to list
manager.insertNode(node);
feet = scnr.nextInt();
inches = scnr.nextInt();
}
manager.printForward();
manager.printBackward();
}
}
Output :-
-------------------------------------
Input Feet and Inches, end with 00:
100 98
23 45
200 90
78 89
0 0
************* Printing Forard Direction *****************
23 feet and 45 inches
78 feet and 89 inches
100 feet and 98 inches
200 feet and 90 inches
************* Done. *****************
************* Printing Backward Direction *****************
200 feet and 90 inches
100 feet and 98 inches
78 feet and 89 inches
23 feet and 45 inches
************* Done. *****************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.