Question
HELP IN JAVA: Write a program that inputs feet and inches, creates a FeetAndInches object, puts it in a Node, and then adds (appends) these
HELP IN JAVA:
Write a program that inputs feet and inches, creates a FeetAndInches object, puts it in a Node, and then adds (appends) these nodes to a list. Input is from the keyboard. Files Node.java and FeetAndInches.java have already been uploaded, you just have to write the main method.
public static void main(String[] args) { Scanner keyboard = new Scanner (System.in);
Node tail= null; Node head=null; System.out.println("Please enter the number of feet and inches separated by space, enter 0 0 to quit"); feet = keyboard.nextInt(); inches = keyboard.nextInt(); while( ) { //create a node and add to list } System.out.println("Please enter the number of feet and inches separated by space, enter 0 0 to quit"); feet = keyboard.nextInt(); inches = keyboard.nextInt(); } PrintList(head); } public static void PrintList(Node head) { Node curr=head; FeetAndInches m; while(curr !=null) {m= (FeetAndInches) curr.item; System.out.println(m); curr=curr.next; //end while } }
public class Node { Object item; Node next; Node(Object newItem) { item = newItem; next=null; } Node(Object newItem, Node nextNode) { item = newItem; next=nextNode; } }
public class FeetAndInches {
int f; int i; FeetAndInches () { f=0; i=0;} FeetAndInches (int newf, int newi) { f=newf; i=newi;} public void setFeet(int newf) {f = newf;} public void setInches(int newi) { i = newi;} public int compareTo(FeetAndInches c) {int thisInches, inches; thisInches = this.f*12 + this.i; inches = c.f*12 + c.i; if (thisInches < inches)return -1; else if (thisInches>inches) return 1; else return 0; } public String toString() { return this.f + " feet and " + this.i + " inches"; }
}
Step 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