159k views
3 votes
A. Write a class that declares a variable named inches that holds a length in inches,

and assign a value. Display the value in feet and inches; for example, 86 inches
becomes 7 feet and 2 inches. Be sure to use a named constant where appropriate.
Save the class as InchesToFeet.java.
B. Write an interactive version of the InchesToFeet class that accepts the inches
value from a user. Save the class as InchesToFeetInteractive.java.

User HyperN
by
4.9k points

1 Answer

2 votes

Answer:

A.

public class Main{

public static void main(String[] args) {

final int divisor = 12;

int inches = 86 ,feet;

feet =inches/divisor;

inches = inches%divisor;

System.out.print(feet+" feets "+inches+" inches");

}

}

B.

import java.util.*;

public class InchesToFeetInteractive{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

final int divisor = 12;

int inches,feet;

System.out.print("Measurement in Inches: ");

inches = input.nextInt();

feet =inches/divisor;

inches = inches%divisor;

System.out.print(feet+" feets "+inches+" inches");

}

}

Step-by-step explanation:

Both classes use the same algorithm; however, the slight difference is that

(A) assumes a values of inch measure while (B) prompts user for input

That is show in the differences below

A)

int inches = 86 ,feet; ---> This initializes inches to 86;

B)

This prompts user for measurement in Inches

System.out.print("Measurement in Inches: ");

This gets the measurement

inches = input.nextInt();

The next lines are common in both classes

This declares and initializes a constant which is used in the conversion

final int divisor = 12;

This calculates the number of feet in the user input

feet =inches/divisor;

This gets the remainder inches, if any. It uses % to calculate the remainder

inches = inches%divisor;

This prints the required output

System.out.print(feet+" feets "+inches+" inches");

User Entity
by
5.5k points