Home Answers Viewqa Java-Beginners Convert Miles To Kilometers program in java

 
 


Antonio
Convert Miles To Kilometers program in java
1 Answer(s)      a year and 2 months ago
Posted in : Java Beginners

Write a program to compute a table of distance conversions between miles and kilometres. You should create a main method which accepts three numbers on the command line as user input: these numbers will be the smallest, largest and step size for constructing the table. The program should then print out a table of converted values to 2 decimal places. You will need to write a separate method to convert a single distance in miles into kilometres. Create and run some unit tests for your program. Extend your program to support conversion from kilometres to miles. Use an extra input argument to tell the program whether to convert from kilometres to miles or from miles to kilometres.

View Answers

March 5, 2012 at 1:30 PM


import java.io.*;
import java.util.*;
class Converter {

public void convertDistance(){
System.out.println("1 KM to Miles");
System.out.println("2 Miles to KM");
System.out.println("3 Exit");

Scanner input=new Scanner(System.in);
System.out.print("Enter your choice: ");
int choice=input.nextInt();
switch(choice){
    case 1:
        int km[]=new int[3];
        double miles[]=new double[3];
        System.out.println("Enter distance in KM: ");
        for(int i=0;i<km.length;i++){
            km[i]=input.nextInt();
            miles[i] = km[i] * 0.621;
        }
        System.out.println("KMS \t Miles");
        for(int i=0;i<km.length;i++){
        System.out.println(km[i]+"\t"+miles[i]);
        }
        break;
    case 2:
        int mile[]=new int[3];
        double kms[]=new double[3];
        System.out.println("Enter distance in Miles: ");
        for(int i=0;i<mile.length;i++){
            mile[i]=input.nextInt();
             kms[i] = mile[i]/ 0.621;
        }
        System.out.println("Miles \t KMS");
        for(int i=0;i<mile.length;i++){
        System.out.println(mile[i]+"\t"+kms);
        }
        break;
    case 3:
        System.exit(0);
        break;
}
}
public static void main(String[] args){
Converter convert=new Converter();
convert.convertDistance();
}
}









Related Pages:
Convert Miles To Kilometers program in java
Convert Miles To Kilometers program in java  Write a program... input argument to tell the program whether to convert from kilometres to miles... in miles into kilometres. Create and run some unit tests for your program
Kilometers to Miles - Complete
Java NotesExample - Kilometers to Miles - Complete Here is the entire.../KmToMiles.java - Converts Kilometers to Miles. // Fred Swartz, 2004-10-28 import java.awt.... convertKmToMi(double kilometers) { // Assume there are 0.621 miles in a kilometer
Convert Miles To Kilometers
Convert Miles To Kilometers     ... to Kilometers. The following program helps you in converting Miles to Kilometers.  Code Description: To convert Miles to Kilometers we have used a formula
Programming: Convert Miles to Kilometers
Java NotesProgramming: Convert Miles to Kilometers Name ________________________________ Description Write a program which inputs miles (a double) and displays the equivalent number of kilometers. Assume there are 1.61 kilometers
Dialog I/O: Kilometers to Miles with Method
Java NotesDialog I/O: Kilometers to Miles with Method  This program is the same as Kilometers to Miles, but with one change: the conversion.../KmToMilesMethod.java // Purpose: Convert kilometers to miles using a method. JOptionPane IO
Kilometers to Miles - User Interface Only
Java NotesExample - Kilometers to Miles - User Interface Only This program produces a user interface, but there is no listener for the button so... characteristics this.setTitle("Kilometers to Miles"); }//end constructor
Dialog I/O: Kilometers to Miles
Java NotesDialog I/O: Kilometers to Miles This basic program asks the user for a number of miles and converts it to kilometers. It uses JOptionPane... 2 //... Computation miles = kilometers * MILES_PER
Kilometer to Miles - Formatted
Java NotesKilometer to Miles - Formatted This program is the same as Kilometers to Miles, but it formats the output to display the output to only...-dialogKmToMiles.java // Purpose: Converts kilometers to miles. Formats output. // Author
Generic Calc
Java NotesExample - Generic Calc This program converts miles into kilometers. It provides a basic plan for making a program that, when a button...(); this.setTitle("Miles to Kilometers"); this.setResizable(false
Convert Meters To Miles
Convert Meters To Miles       In this section, you will learn to convert Meters To Miles. The following program helps you in converting Meters To Miles.  Code
Kilometers per Liter to Miles per Gallon
Kilometers per Liter to Miles per Gallon   ... the kilometers per liter into miles per gallon then there is one formula...;ConversionTable of Kilometers perLiter and Miles per Gallon </title> <
Java: Kilometer to Miles - IO Class
Java NotesKilometer to Miles - IO Class 1 2 3 4 5 6... kilometers to miles. // Michael Maus, 9 Sept 2004 // // Note: Move input methods... of kilometers."); //... Computation miles = kilometers
Convert Inches to Centimeters
Convert Inches To Centimeters       In this section, you will learn to convert Inches to Centimeters. The following program helps you in converting Inches to Centimeters
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
java program to convert decimal in words
java program to convert decimal in words  write a java program to convert a decimal no. in words.Ex: give input as 12 output must be twelve
Convert To Java Program - Java Beginners
Convert To Java Program  Can anyone convert this C program codes to Java codes,please...thanks! #include int array[20]; int dcn; int cntr=0; void add(); void del(); void insert(); void display(); void exit(); void
source code program - Java Beginners
source code program  I need the source code for a program that converts temperatures from celsius to fahrenheit and vice versa, as well as converting kilometers to miles and vice versa using Java "classes".  Hi
GUI convert to celsius program - Java Beginners
GUI convert to celsius program  how to write java GUI program to convert Fahrenheit to Celsius that need user to input value of Fahrenheit then click button convert and value of Celsius will display as a output.   Hi
Convert One Unit to Another
, Liters, Miles, Carats, Meters etc to convert them to another units. First... Convert One Unit to Another       Again in this section, you will learn to convert One Unit to Another
Convert to java - Java Beginners
Convert to java  Can anyone convert this javascript program codes to Java codes,please...thanks! var iTanggalM = 0; var iTanggalH = 0; var iBulanM = 0; var iBulanH = 0; var iTahunM = 0; var iTahunH = 0; var
Number Convert - Java Beginners
Number Convert  Dear Deepak Sir, I am entered in Command prompt Some value like 234.Value can be convert into Two Hundred and Thirty Four Rupees Only.Plz send me the program ASAP. Thanks & Regards, VijayaBabu.M  Hi
Java programming question - Java Beginners
Java programming question  I want to write a program that converts... kilometers to miles and vice versa using "methods".  Hi Friend, Try...(); System.out.print("Enter unit KM or Miles: "); try{ unit
convert to java plss - Java Beginners
convert to java plss  hi i am using microsoft VB6.0 and my friend ask me to make java program to convert number to words but i only use visual basic because i dont know much in java plss convert it to me this is my code
convert java Application to Java Applet
convert java Application to Java Applet  hi every Java Master or Java Professional , my Name Is Vincent i'm java beginner can i ask a question regarding for Java application convert to Java Applet. below her is my sample
convert java Application to Java Applet
convert java Application to Java Applet  hi every Java Master or Java Professional , my Name Is Vincent i'm java beginner can i ask a question regarding for Java application convert to Java Applet. below her is my sample
hi.. please give me a java coding for the following question
hi.. please give me a java coding for the following question  Write a program that displays the following table (note that 1 mile is 1.609 kilometers). Miles Kilometers
Java Program
Java Program for accepting numbers from 1 - 999 and printing them in form... in form of words   Java Convert Number to Words The given code accept... big numbers, then go through the following link: Java Convert Numbers to Words
java programming code - Java Beginners
java programming code  I want to write a program that converts temperatures from celsius to fahrenheit and vice versa. It also converts kilometers to miles and vice versa using classes
Convert Text to Binary
Convert Text to Binary       In this section, we will learn how to convert Text to Binary. The following program provides you the functionality to convert Text to Binary
Convert Text to Binary
Convert Text to Binary       In this section, we will learn how to convert Text to Binary. The following program provides you the functionality to convert Text
Convert String to long
Convert String to long       In this section, we will learn how to convert String to long. The following program provides you the functionality to convert String to long. Code
Convert Character into Integer
Convert Character into Integer       In this section, you will learn to convert the character into a integer. The java.lang package convert provides the facility to convert
GUI and how to convert a distance - Java Beginners
GUI and how to convert a distance  i need help.. how to create a GUI application that can be is used to convert a distance unit in miles into its... in the corresponding text fields. If the user enters the value of miles
Convert ArrayList To Array
Convert ArrayList To Array       This is a simple program of java util.package. In this section, you...;} } Download this program: Output this program. C:\corejava>java
convert to decimal
convert to decimal  f42a4 convert to decimal by using java
Convert Decimal To Character
Convert Decimal To Character       This is the simple program of java.lang package. In this example, we are going to convert decimal number to its corresponding character
Convert Decimal to Hexadecimal
;} } Download this program. Output of this program: C:\corejava>java... Convert Decimal to Hexadecimal       In this section, you will learn to convert a decimal data
Convert Object To String
of this program given below. C:\corejava>java ObjectToString... Convert Object To String       In this section, you will learn to convert an object to a string
Convert Pounds To Kilograms
Convert Pounds To Kilograms       In this section, you will learn to convert Pounds To Kilograms. The following program helps you in converting Pounds To Kilograms. 
Convert Grams To Pounds
Convert Grams To Pounds       In this section, you will learn to convert Grams To Pounds. The following program helps you in converting Grams To Pounds.  Code
Convert Feet To Meters
Convert Feet To Meters       In this section, you will learn to convert Feet to Meters. The following program helps you in converting Feet to Meters.  Code
Convert Hexadecimal number into Integer
of this program.  C:\corejava>java HexaToInteger Enter... Convert Hexadecimal number into Integer ... to convert hexadecimal data into integer. The java.lang package provides
Convert List to Array
Convert List to Array       Lets see how to convert List to Array.  Code Description: In this program we have taken a List of type String as shown below. For this we have used
Convert List to ArrayList
Convert List to ArrayList       In this section, you will learn how to convert List to ArrayList.  Code Description: This program helps you in converting List
Convert Integer to Double
;IntegerToDouble" for java component. Program takes an integer value...: Output of  this program given below. C:\corejava>java... Convert Integer to Double    
convert jpeg to gif - Swing AWT
convert jpeg to gif  i want to convert jpeg file to gif file. in the roseindia's already existing program,i can convert gif to jpeg.But i cant convert jpeg to gif. help me plz....  Hi friend, import java.io.
Convert Inputstream to ByteArrayInputStream
Convert Inputstream to ByteArrayInputStream       In this example we are going to convert.... Then convert this input stream into string and then string into byte by using
Convert Date to Milliseconds
Convert Date to Milliseconds       In this section, you will learn to convert a date... for measuring the time.  Description of program
Convert Milliseconds to Date
Convert Milliseconds to Date       In this section, you will learn to convert a millisecond into a date.  Description of program
Convert Number to Binary
: Output of this program given below. C:\corejava>java... Convert Number to Binary       In this section, you will learn to convert a number to a binary (0,1

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.