HELP IN JAVA: Write a program that inputs feet and inches, creates a FeetAndInch
ID: 2247022 • Letter: H
Question
HELP IN JAVA:
Write a program that inputs feet and inches, creates a FeetAndInches object, puts it in a Node, and then adds (prepends) 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);
int feet, inches;
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( )
{
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
}
}
Explanation / Answer
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
int feet, inches;
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 temporary node
Node temp=new Node(new FeetAndInches(feet, inches));
temp.next=null;
// if the list is empty
if(head==null)
head=temp;
// otherwise add the node tot the beginning of the list
else
{
temp.next = head;
head = temp;
}
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);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.