I need to write an application that reads a distance in metres. The program will then convert the distance to kilometres, feet and inches, by calling methods. Here is my code so far... looking for pointers please.
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
/** This program converts meters to kilometers, inches, and feet
depending on user's input. After information is converted the
program will quit.
*/
public class A5Q1
{
public static void main(String[] args)
{
double kilometers; //to hole the number of kilometers
double meters; //to hold the number of meters
double inches; //to hold the number of inches
double feet; //to hold the number of feet
double choice; //to hold the choice number
// Get the number of meters.
meters = getMeters();
//Show the meters to kilometers
kilometers = showKilometers(meters);
//Show the meters to inches
inches = showInches(meters);
//Show the meters to feet
feet = showFeet(meters);
//Display the results
//displayResults(meters, kilometers, inches, feet);
//System.exit(0);
}
/**
The getMeters method prompts the user to enter a number
in meters and return the value in kilometers, inches, or feet
*/
public static double getMeters()
{
String input; //to hold input
double numMeters; //to hold number of meters
//Get the number of meters from the user.
input = JOptionPane.showInputDialog(
"Enter a distance in meters: \n");
input = JOptionPane.showInputDialog(
"Enter your choice: \n" +
"1. Convert to kilometers \n" +
"2. Convert to inches \n" +
"3. Convert to feet \n" +
"4. Quit the program");
if (choice == 1);
{
/**The showKilomeeters method converts a number of meters to kilometers
using the formula 1 meter = 0.001 kilometers
*/
JOptionPane.showMessageDialog(null, "you chose to convert to kilometers");
//public static void showKilometers(double numMeters);
// return numMeters * 0.001;
}
if (choice == 2)
{
return numMeters * 39.37;
}
//convert the input to a double.
numMeters = Double.parseDouble(input);
//Return the number of meters.
return numMeters;
}
/**The showInches method converts a number of meters to inches using
the formula 1 meter = 39.37 inches
*/
public static double showInches(double numMeters)
{
return numMeters * 39.37;
}
/**The showFeet method converts a number of meters to feet using
the formula 1 meter = 3.281 feet
*/
public static double showFeet(double numMeters)
{
return numMeters * 3.281;
}
/**The displayResults method displays a message showing the
results of the conversions.
*/
public static void displayResults (double meters, double kilometers, double inches, double feet)
{
//Display the numbe of kilometers
JOptionPane.showMessageDialog(null,
meters + " meters equal " +
kilometers + " kilometers.");
}
}