Write code in java. Create a class that will be used to hold measurement in feet
ID: 3745568 • Letter: W
Question
Write code in java.
Create a class that will be used to hold measurement in feet, inches, yards, meters and centimeters.
Use constant fields for the conversions. These constants can be assigned value when the fields are declares, since they are non-changing every time an instance is created. Write two constructor methods: the no-args which set the fields to zero and a second constructor that does take arguments.
Write five set( ) methods and five get( ) methods, one for each field.
Write a toString( ) method.
Create a second class named Driver. This class will have only one method And no fields. The only method in this Driver class is main( ).
In main( ) create an instance of your measurement class using the no-args constructor. Then in a loop provide the user a chance to set whichever field they chose. Make this looks good to the user, a menu would be best. Every Change the user enters requires a call to a set( ) method. Be sure your set( ) methods ensure ALL the fields are consistent. For each iteration of the loop, call the toString( ) method in a print statement to print the state of the measurement object.
The loop should repeat as long as the user wants to continue.
Once the loop exits, print a “good-bye” message.
Explanation / Answer
i have implemented two methods for inches, feet. rest can be implemented the same way
import java.util.Scanner;
public class question
{
float feet;
float inches;
float yards;
public question()
{
set all to zero;
}
public question(float feet, float inches, float yards,....)
{
this.feet=feet;
this.inches=inches;
}
public int getFeet()
{
float feet;
Scanner keyIn = new Scanner(System.in);
feet = keyIn.nextInt();
return feet;
}
public int getInches()
{
float inches;
Scanner keyIn = new Scanner(System.in);
inches = keyIn.nextInt();
return inches;
}
public getYardds()
public getCentim()
public getMetre()
public setFeet( float feet)
{
this.feet=feet
}
public setInches(float inches)
{
this.inches=inches
}
public string toString()
{
String s = Float.toString(this.inches);
String s1 = Float.toString(this.feet);
//print all values
}
};
class driver
{
public static void main (String [] args)
{
question q1= new question();
//declare data types and variables
int choice=0;
float feet=0;
float inches=0;
Scanner keyIn = new Scanner(System.in);
do{
System.out.println("Please select a option");
System.out.println("1. inches 2.feet 3.yards ..6exit");
choice = keyIn.nextInt();
if(choice==1)
{
inches=q1.getInches();
q1.setInches(inches);
q1.toString();
}
else if(choice==2)
{
}
else
//wrong choice
}
while(choice!=4)
//convert entered measurement in inches to yards, (feet & inches) | 1 foot is 12 inches | 1 yard is 3 feet
}//end of main
}// end of class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.