Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions