HELP IN JAVA: Write a program that inputs feet and inches, creates a FeetAndInch
ID: 2247072 • 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 (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";
}
}
Explanation / Answer
Node.java:
public class Node {
Object item;
Node next;
static Node head;
Node(Object newItem)
{ item = newItem;
next=null;
}
Node(Object newItem, Node nextNode)
{ item = newItem;
next=nextNode;
}
public static void PrintList()
{
Node curr=head;
FeetAndInches m;
while(curr !=null)
{m= (FeetAndInches) curr.item;
System.out.println(m);
curr=curr.next;
}
}
}
FeetAndInches.java
import skydive.Node;
import java.util.*;
public class FeetAndInches {
int f;
int i;
int inches,feet;
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";
}
public static void main(String[] args) {
int inches,feet,feetVal,inchesVal;
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();
FeetAndInches llist = new FeetAndInches();
Node node=new Node(head);
System.out.println("Enter the feet value");
feetVal = keyboard.nextInt();
for(int i=0;i<feet;i++)
{
llist.setFeet(feetVal);
}
System.out.println("Enter the inches value");
inchesVal = keyboard.nextInt();
for(int i=0;i<inches;i++)
{
llist.setInches(inchesVal);
}
//node(llist);
node.PrintList();
}
output:
Please enter the number of feet and inches separated by space, enter 0 0 to quit
1
1
Enter the feet value
1
Enter the inches value
1
1 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.