Distance conversion

Ads

 
 

Share on Google+Share on Google+

dylan
Distance conversion
1 Answer(s)      7 years ago
Posted in : Java Beginners
Write an application that reads a distance in metres. The program will then present a user menu of conversion types, converting the distance to kilometres, feet and inches, according to the user?s selection. The converted distances will then be printed.
? Write a method readChoice() for the user selection.
? Write a method to read a distance in metres from the user.
? Write three methods to convert the distance to kilometres, feet and inches.
Ads
View Answers

August 11, 2010 at 12:11 PM


Hi Friend,

Try the following code:

import java.util.*;
public class ConvertMeter{
public void showKilometers(double meters){
double kilometers = meters * 0.001;
System.out.println(meters+" meters is "+kilometers+" kms");
}
public void showInches(double meters){
double inches = meters * 39.37;
System.out.println(meters+" meters is "+inches+" inches");
}
public void showFeet(double meters){
double feet = meters * 3.281;
System.out.println(meters+" meters is "+feet+" feet");
}
public void menu(){
System.out.print("Enter a distance in meters: ");
Scanner scan=new Scanner(System.in);
double m=scan.nextDouble();
boolean quit = false;

do{
System.out.println();
System.out.println("1. Convert to kilometers");
System.out.println("2. Convert to inches");
System.out.println("3. Convert to feet");
System.out.println("4. Quit the program");
System.out.println();
System.out.print("Enter your choice: ");
int menu = scan.nextInt();
switch(menu) {
case 1:
showKilometers(m);
break;
case 2:
showInches(m);
break;
case 3:
showFeet(m);
break;
case 4:
quit = true;
System.out.println("Bye!");
break;
default:
System.out.println("Invalid Entry!");
}
}

while (!quit);

}
public static void main(String[]args){
ConvertMeter convert=new ConvertMeter();
convert.menu();
}
}

Thanks

Ads









Related Tutorials/Questions & Answers:
Distance conversion - Java Beginners
Distance conversion  Write an application that reads a distance in metres. The program will then present a user menu of conversion types, converting the distance to kilometres, feet and inches, according to the user?s selection
Another distance conversion - Java Beginners
Another distance conversion  Write an application that reads a distance in metres. The program will then convert the distance to kilometres, feet... a method for each conversion ? toKilometres(), toFeet(), and toInches
Advertisements
class distance
class distance  class distance-integer data member km1,km2,km3,m1,m2,m3. run()-to input the two distance and add them in the third. display()-to display the total distance. display the constructor if required
Edit Distance
Edit Distance  I want java programming ask from user input two string and the program find the edit distance between two strings and find table and optimal solution using GUI
conversion Applet
conversion Applet  I am a java beginner.................. strong texthow to create JApplet that prompts the user to enter a distance in miles in a JTextField and then converts mile to kilometers and display the result as XX.XX
Delhi to Udaipur by road distance
Delhi to Udaipur by road distance  delhi to udaipur by road distance
Distance in meters - Java Beginners
Distance in meters  Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections... the program The program will convert the distance to kilometers, inches
Hill stations near Delhi distance
Hill stations near Delhi distance   Hi, Can any one tell the distances of the Hill stations near Delhi? Thanks
currency conversion
currency conversion   hi frds.. I wan jsp code to convert currency in different formats??... if u know plz plz plz post it   Please visit the following link: http://www.roseindia.net/tutorials/I18N/currency
GUI and how to convert a distance - Java Beginners
application that can be is used to convert a distance unit in miles into its... conversion is as follows: 1 mile = 2.5 km 1 km = 1000 m 1 meter = 60...=new JTextField(20); t5=new JTextField(20); l1=new JLabel("Enter distance
Version of com.shopizer>shopizer-shipping-distance-processor dependency
List of Version of com.shopizer>shopizer-shipping-distance-processor dependency
image conversion tools
image conversion tools  how many tools are available in image conversion at particularlly in java
Road distance using Java - Java Beginners
Road distance using Java  Dear All i am calculating the straight line distance between two latlong points using the simple mathematical formula but i need the Real Road Driving Distance // protected double distance(double pX
Distance learning business management
Distance learning business management courses are on high demand today.... But those students should not worry as many institutions have started distance... for the future and skilled workforce. Distance learning business management courses
Hill Stations near Delhi distance
Distance of Major Hill Stations From Delhi  In this page we have listed all the Hill Stations near Delhi with their distance from central Delhi... and northeastern India. List of Hill Stations near Delhi with distance Sr
VoIP Long Distance
VoIP Long Distance          VoIP Designing a Long-Distance Network The long-distance Voice over IP (VoIP) network solution is a set of network design
Number conversion in JavaScript
Number conversion in JavaScript  How do you convert numbers between different bases in JavaScript
conversion of language in a project
conversion of language in a project  how can we convert the database code in the project dynamically
.wav file conversion
.wav file conversion   how to read .wav file and converd the data into text format
How can i display the distance of a line drawing randomly?
How can i display the distance of a line drawing randomly?  How can i display the distance of a line drawing randomly
NSString to float conversion
NSString to float conversion  HI, I my iPhone and iPad application I have to convert NSString to float. Provide me example of NSString to float conversion. Thanks   Hi, Please see NSString to float conversion
object conversion - Java Beginners
object conversion   Hi, Can anybody tell me the object conversion in java.   Hi sandeep Can u please tell me in details about your.../java-conversion/ObjectToDouble.shtml Thanks... sandeep kumar suman
Conversion - Development process
is the theory of conversion from java to .NET? How it is done?   Hi... Conversion Assistant (JLCA). Code that calls Java APIs can convert to comparable C# code... to a topic with guidelines for modifications needed to finish the conversion
NSString to float conversion
NSString to float conversion  HI, I my iPhone and iPad application I have to convert NSString to float. Provide me example of NSString to float conversion. Thanks   HI, Use the following code: NSScanner *strXpos
Maven Dependency shopizer-shipping-distance-processor >> 2.0.3
You should include the dependency code given in this page to add Maven Dependency of com.shopizer >> shopizer-shipping-distance-processor version2.0.3 in your project
loop to input multiple points to calculate final distance and speed
loop to input multiple points to calculate final distance and speed  ...; private double distance; public Travel() { time=0; distance=0... getDistance() { return distance; } public static void main (String[] args
Version of com.groupdocs>groupdocs-conversion dependency
List of Version of com.groupdocs>groupdocs-conversion dependency
Version of com.ncona>conversion-graph dependency
List of Version of com.ncona>conversion-graph dependency