Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: Write a program that inputs feet and inches, creates a FeetAndInches objec

ID: 3743629 • Letter: J

Question

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.

Example.java

public class Example {


    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(!(feet==0 && inches==0) )
    { //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
}
}
}

FeetAndInches.java

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";
   }
}//end FeetAndInches class
  

Node.java

public class Node {

Object item;
Node next;

Node(Object newItem)
{ item = newItem;
    next=null;
}

Node(Object newItem, Node nextNode)
{ item = newItem;
    next=nextNode;
}
}

Explanation / Answer

import java.util.Scanner; public class Example { 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"); int feet = keyboard.nextInt(); int inches = keyboard.nextInt(); while (!(feet == 0 && inches == 0)) { //create a node and add to list Node node = new Node(new FeetAndInches(feet, inches), null); if(tail != null) tail.next = node; tail = node; if(head == null) { head = tail; } 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 } } }