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.");
        }
                                 
    }

Recommended Answers

All 3 Replies

One comment:
Make all of the hardcoded constants into finals with self documenting names:

final static double InchesPerMeter = 39.37;

What is the problem?
I can't figure out what is the problem.

Okay, you're not really calling any of those methods except the getInput one, and that's kind of weird.

In the input, you want to set choice to something (right now, you're reading into input and then overwriting it immediately). Once you have an input and a choice, you want to use some decision logic (probably a switch - if this were perl, I'd use an array of function references, because i just love that, but here we'll say a switch) to call the correct function and set a String equal to the dimension they've selected. Then you build a string, something like

input + " meters converts to " + result + units;

Print that output and take a bow. So you're not going to build string in the conversion routine - do it that way, and you're building the same string four times. If you decide to add cubits, you have to build the string again, likewise if you want to add paces, or parsecs, or AUs or rubiks-cube-sides or whatever. Do the reporting once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.