In: Computer Science
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following:
Solution for the above java program is provided below. Please comment if you have any doubt.
ConvertToMetricMain.java
import java.util.Scanner;
public class ConvertToMetricMain {
public static void main(String[] args) {
// Create scanner
object
Scanner sc=new
Scanner(System.in);
// Create object of
convertToMetrric class
convertToMetric c=new
convertToMetric();
// Input value from
keyboard
System.out.println("Input a value for standared: ");
double
value=sc.nextDouble();
// Set value
c.setStandard(value);
// Get value in
meters
double
valueInMeter=c.getMeters();
// Get value in
centimeters
double
valueInCentimeter=c.getCentimeters();
// Display the
values
System.out.println("The
standard value in meter: "+c.getMeters());
System.out.println("The
standard value in centimeter: "+c.getCentimeters());
}
}
convertToMetric.java
// Class declaaration
class convertToMetric {
double standard;
// Constructor
public void convertToMetric(double length)
{
standard=length;
}
// Function to set the value
public void setStandard(double length)
{
standard=length;
}
// Function to return the value
public double getStandard()
{
return standard;
}
// Function to convert it it meter
public double getMeters()
{
return
standard*0.3048;
}
// Function to convert it to centimeter
public double getCentimeters()
{
return
standard*30.48;
}
}
Output: