Please help in java: Create an array to hold 10 FeetInches objects. Input is fro
ID: 3785690 • Letter: P
Question
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
This is 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 = 0,inch=0;
//create an array to hold 10 FeetInches objects
FeetInches[] ft = new FeetInches [10];
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);
for(int i=0; i<10;i++){
System.out.println("Feet: "+f.getFeet()+ " "+ "Inches: "+f.getInches());
}
}
}
Explanation / Answer
Note: I Made some changes to get the correct output.If you want me to make anymore changes
just tell me.I will do it.
_________________
FeetInches.java
import java.util.Scanner;
public class FeetInches {
//Declaring variables
private int feet;
private int inches;
//Default constructor
public FeetInches() {
feet = 0;
inches = 0;
}
//Parameterized constructor
public FeetInches(int f, int i) {
feet = f + i / 12;
inches = i % 12;
}
//Setters and getters
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;
}
//toString() method is used to display the contents of an object inside it
public String toString() {
return " Feet: " + feet + " Inches: " + inches;
}
public static void main(String[] args) {
//Scanner class object is used to read the inputs entered by the user
Scanner keyboard = new Scanner(System.in);
//Declaring variables
int feet = 0, inch = 0;
// create an array to hold 10 FeetInches objects
FeetInches[] ft = new FeetInches[10];
//This for loop will read the 20 integers entered by the user
System.out.println("Enter 20 integers: ");
for (int i = 0; i < 10; i++)
{
feet = keyboard.nextInt();
inch = keyboard.nextInt();
/* Every time Creating FeetInches Class Object by passing feet and inch
* as arguments and populate those objects into an array
*/
ft[i] = new FeetInches(feet, inch);
}
//Displaying the contents of Each FeetInch class Object
for (int i = 0; i < 10; i++) {
System.out.println("Feet: " + ft[i].getFeet() + " " + "Inches: "+ ft[i].getInches());
}
}
}
__________________________
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
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.