Please help in Java Create an array to hold 10 FeetInches ob
Please help in Java:
Create an array to hold 10 FeetInches objects. Input is from the keyboard (20 ints). To get the correct output you MUST use this input: 56 34 4 67 3 4 45 5 11 21 84 45 44 5 8 11 23 2 20 19
What I have so far :
public static class FeetInches {
private int feet;
private int inches;
public FeetInches()
{//write the code for the default constructor here
feet = 0;
inches = 0;
}
public FeetInches(int f, int i)
{
feet = f + i/12;
inches = i%12;
}
public void setFeet(int f){
feet = f;
}
public void setInches(int i){
feet = feet + i/12;
inches = i%12;
}
public int getFeet(){
return feet;
}
public int getInches(){
return inches;
}
public String toString()
{
return \" Feet: \" + feet + \" Inches: \" + inches ;
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int feet, inch, input;
//create an array to hold 10 FeetInches objects
FeetInches[] ft = new FeetInches [10];
//input 20 ints from the keyboard.
System.out.println(\"Enter 20 integers: \");
input=keyboard.nextInt();
feet=.nextInt();
inch=keyboard.nextInt();
//you MUST enter the ones I gave you to get the correct output
//input and output is in answers,txt in the module
for (int i=0; i<20; i++){
System.out.println(\"Feet: \"+ feet + \" Inches: \"+inch);
}
}
}
Also need to do the same thing again BUT with an ArrayList.
Thanks!
Solution
Hi
I have modifed the code and highlighted the code changes below.
FeetInches.java
import java.util.ArrayList;
import java.util.Scanner;
public class FeetInches {
private int feet;
private int inches;
public FeetInches()
{//write the code for the default constructor here
feet = 0;
inches = 0;
}
public FeetInches(int f, int i)
{
feet = f + i/12;
inches = i%12;
}
public void setFeet(int f){
feet = f;
}
public void setInches(int i){
feet = feet + i/12;
inches = i%12;
}
public int getFeet(){
return feet;
}
public int getInches(){
return inches;
}
public String toString()
{
return \" Feet: \" + feet + \" Inches: \" + inches ;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int feet, inch, input;
//create an array to hold 10 FeetInches objects
ArrayList<FeetInches> list = new ArrayList<FeetInches>();
//input 20 ints from the keyboard.
System.out.println(\"Enter 20 integers: \");
for(int i=0; i<10; i++){
feet=keyboard.nextInt();
inch=keyboard.nextInt();
FeetInches f = new FeetInches(feet, inch);
list.add(f);
}
//you MUST enter the ones I gave you to get the correct output
//input and output is in answers,txt in the module
for (int i=0; i<list.size(); i++){
FeetInches f = list.get(i);
System.out.println(\"Feet: \"+ f.feet + \" Inches: \"+f.inches);
}
}
}
Output:
Enter 20 integers:
56 34 4 67 3 4 45 5 11 21 84 45 44 5 8 11 23 2 20 19
Feet: 58 Inches: 10
Feet: 9 Inches: 7
Feet: 3 Inches: 4
Feet: 45 Inches: 5
Feet: 12 Inches: 9
Feet: 87 Inches: 9
Feet: 44 Inches: 5
Feet: 8 Inches: 11
Feet: 23 Inches: 2
Feet: 21 Inches: 7


