Meters to Kilometers

Using if statements

Page 1 of 1

13 Replies - 11731 Views - Last Post: 16 March 2010 - 02:20 PM Rate Topic: -----

#1 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 10

Meters to Kilometers

Posted 16 March 2010 - 01:15 PM

I'm getting lost in my code. I need to convert meters to kilometers, meters to inches, and meters to feet. Also need a menu to select which factor to convert to meters.

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

Is This A Good Question/Topic? 0
  • +

Replies To: Meters to Kilometers

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Meters to Kilometers

Posted 16 March 2010 - 01:17 PM

Quote

I have attached my CovertMeters.java file.


Please post your code here instead, using the code tags :code:

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Meters to Kilometers

Posted 16 March 2010 - 01:20 PM

View Posterik.price, on 16 March 2010 - 03:17 PM, said:

Quote

I have attached my CovertMeters.java file.


Please post your code here instead, using the code tags :code:

Thanks! :)


That, and if he's going to attach it, he should... :P
Was This Post Helpful? 0
  • +
  • -

#4 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 10

Re: Meters to Kilometers

Posted 16 March 2010 - 01:21 PM

View Posterik.price, on 16 March 2010 - 12:17 PM, said:

Quote

I have attached my CovertMeters.java file.


Please post your code here instead, using the code tags :code:

Thanks! :)

not sure how....please help
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Meters to Kilometers

Posted 16 March 2010 - 01:25 PM

Just copy your code from your IDE/editor and paste it between the tags. :code:

//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
Was This Post Helpful? 0
  • +
  • -

#6 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 10

Re: Meters to Kilometers

Posted 16 March 2010 - 01:29 PM

can't put code between tags
Was This Post Helpful? 0
  • +
  • -

#7 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 10

Re: Meters to Kilometers

Posted 16 March 2010 - 01:35 PM

View Posterik.price, on 16 March 2010 - 12:25 PM, said:

Just copy your code from your IDE/editor and paste it between the tags. :code:

//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...
Was This Post Helpful? 0
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Meters to Kilometers

Posted 16 March 2010 - 01:37 PM

I don't see an attachment...
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Meters to Kilometers

Posted 16 March 2010 - 01:41 PM

There is no attachment.

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.
Was This Post Helpful? 0
  • +
  • -

#10 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 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

Was This Post Helpful? 0
  • +
  • -

#11 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 10

Re: Meters to Kilometers

Posted 16 March 2010 - 01:50 PM

View Postdoganro, 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?
Was This Post Helpful? 0
  • +
  • -

#12 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Meters to Kilometers

Posted 16 March 2010 - 01:55 PM

Here's one error
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

Was This Post Helpful? 1
  • +
  • -

#13 doganro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 10

Re: Meters to Kilometers

Posted 16 March 2010 - 02:18 PM

is the code in some kind of general order or is it all over the place?
Was This Post Helpful? 0
  • +
  • -

#14 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Meters to Kilometers

Posted 16 March 2010 - 02:20 PM

Besides the errors I pointed out, your code is in pretty good shape. What I explained probably didn't fix all the errors, so let us know if you have any more (along with error messages)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1