Please help in Java: Complete this program by finishing the FeetInches class. im
ID: 3781936 • Letter: P
Question
Please help in Java:
Complete this program by finishing the FeetInches class.
import java.util.Scanner;
public class Lab2Num4 {
public static class FeetInches {
private int feet;
private int inches;
public FeetInches()
{//write the code for the default constructor here
}
public FeetInches(int f, int i)
{ //write the code for the constructor.
//do not worry (for now) about negative values
//but if the user tries to construct an object
//with 4 feet and 15 inches, the constructor
//has to fix it so that feet becomes 5 and inches 3
}
//write the accessors and mutators here
public String toString()
{
return feet + " feet and " + inches + " inches. ";
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the number of feet as a whole number");
int feet1 = keyboard.nextInt();
System.out.println("Enter the number of inches as a whole number");
int inch1 = keyboard.nextInt();
FeetInches f = new FeetInches(feet1, inch1);
System.out.println(f);
f.setFeet(4);
f.setInches(50);
System.out.println(f);
}
}
thanks!!
Explanation / Answer
// Lab2Num4.java
import java.util.Scanner;
public class Lab2Num4
{
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)
{
//write the code for the constructor.
//do not worry (for now) about negative values
feet = f;
inches = i;
//but if the user tries to construct an object
//with 4 feet and 15 inches, the constructor
//has to fix it so that feet becomes 5 and inches 3
if(feet == 4 && inches == 15)
{
setFeet(5);
setInches(3);
}
}
public void setFeet(int f)
{
feet = f;
}
public void setInches(int i)
{
inches = i;
}
public int getFeet()
{
return feet;
}
public int getInches()
{
return inches;
}
public String toString()
{
return feet + " feet and " + inches + " inches. ";
}
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the number of feet as a whole number");
int feet1 = keyboard.nextInt();
System.out.println("Enter the number of inches as a whole number");
int inch1 = keyboard.nextInt();
FeetInches f = new FeetInches(feet1, inch1);
System.out.println(f);
f.setFeet(8);
f.setInches(2);
System.out.println(f);
}
}
/*
output:
Enter the number of feet as a whole number
4
Enter the number of inches as a whole number
6
4 feet and 6 inches.
8 feet and 2 inches.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.