13 Replies  12434 Views  Last Post: 16 March 2010  02:20 PM
#1
Meters to Kilometers
Posted 16 March 2010  01:15 PM
Here are my instructions:
Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections:
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
The program will convert the distance to kilometers, inches, or feet, depending on the user’s selection. Here are the specific requirements:
Write a void method named showKilometers, which accepts the number of
meters as an argument. The method should display the argument converted to kilometers. Convert the meters to kilometers using the following formula:
kilometers = meters * 0.001
Write a void method named showInches, which accepts the number of meters as an argument. the method should display the argument converted to inches, Convert the meters to inches using the following formula:
inches = meters * 39.37
Write a void method named showFeet, which accepts the number of meters as an argument. The method should display the argument converted to feet. Convert the meters to feet using the following formula:
feet = meters * 3.281
Write a void method named menu that displays the menu of selections. This method should not accept any arguments.
The program should continue to display the menu until the user enters 4 to quit the program.
The program should not accept negative numbers for the distance in meters.
If the user selects an invalid choice from the menu, the program should display an error message.
Here is an example session with the program, using console input. The user’s input is shown in bold.
Enter a distance in meters: 500 [enter]
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice: 1 [enter]
500 meters in 0.5 kilometers.
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice: 3 [enter]
500 meters is 1640.5 feet.
1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program
Enter your choice: 4 [enter]
Bye!
I have attached my CovertMeters.java file.
Any help would be appreciated.
Thanks
doganro
Replies To: Meters to Kilometers
#2
Re: Meters to Kilometers
Posted 16 March 2010  01:17 PM
Quote
Please post your code here instead, using the code tags
Thanks!
#3
Re: Meters to Kilometers
Posted 16 March 2010  01:20 PM
#4
Re: Meters to Kilometers
Posted 16 March 2010  01:21 PM
#5
Re: Meters to Kilometers
Posted 16 March 2010  01:25 PM
//your code here
Hit reply on this post and check out the //your code here part if you're still confused on how to do it
#6
Re: Meters to Kilometers
Posted 16 March 2010  01:29 PM
#7
Re: Meters to Kilometers
Posted 16 March 2010  01:35 PM
erik.price, on 16 March 2010  12:25 PM, said:
//your code here
Hit reply on this post and check out the //your code here part if you're still confused on how to do it
can you post the code from the attached file i sent? Can't believe i'm this stupid...
#8
Re: Meters to Kilometers
Posted 16 March 2010  01:37 PM
#9
Re: Meters to Kilometers
Posted 16 March 2010  01:41 PM
All you have to do is open up CovertMeters.java, copy the text and post it here.
Type it like this:
[ code]
PUT CovertMeters.java text here!
[ /code]
Except when you type the code tags, there should be no space. At least post your code and let me fix the code tags if you really need me to.
#10
Re: Meters to Kilometers
Posted 16 March 2010  01:44 PM
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 ConvertMeters { 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 @param numMeters the number of meters to convert. @return The number of 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 @param numMeters the number of meters to convert @return The number of 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 @param numMeters the number of meters to convert @return The number of inches */ public static double showFeet(double numMeters) { return numMeters * 3.281; } /**The displayResults method displays a message showing the results of the conversions. @param meters A number of meters @param kilometers A number of kilomters @param inches A number of inches @param feet A number of feet */ 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."); } }
Edited by Dogstopper: See, I added the code tags to it. Try clicking the "edit" button to see what I did.
This post has been edited by doganro: 16 March 2010  01:48 PM
#11
Re: Meters to Kilometers
Posted 16 March 2010  01:50 PM
doganro, on 16 March 2010  12:44 PM, said:
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 ConvertMeters { 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 @param numMeters the number of meters to convert. @return The number of 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 @param numMeters the number of meters to convert @return The number of 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 @param numMeters the number of meters to convert @return The number of inches */ public static double showFeet(double numMeters) { return numMeters * 3.281; } /**The displayResults method displays a message showing the results of the conversions. @param meters A number of meters @param kilometers A number of kilomters @param inches A number of inches @param feet A number of feet */ 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."); } }
Edited by Dogstopper: See, I added the code tags to it. Try clicking the "edit" button to see what I did.
thanks, can you help with the program?
#12
Re: Meters to Kilometers
Posted 16 March 2010  01:55 PM
public static double getMeters() { ... if (choice = 1); { ... } if (choice = 2); { ... }
First of all, don't put a semicolon after an if statement! Example:
if(false); //problematic { System.out.println("Something's wrong here..."); //will be printed } if(false) //good { System.out.println("That's better"); //won't be printed }
The println is obviously not meant to be printed, yet it is because of the semicolon
Secondly, you never declare choice!
Next is that you use the assignment operator to compare two values!
if (choice = 1)
This should really be:
if (choice == 1)
= for assignment
== for equality
edit: incorrect usage of 'to' fail
This post has been edited by erik.price: 16 March 2010  01:56 PM
#13
Re: Meters to Kilometers
Posted 16 March 2010  02:18 PM
#14
Re: Meters to Kilometers
Posted 16 March 2010  02:20 PM
