Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Convert Miles To Kilometers program in java

Ads

 
 

Share on Google+Share on Google+

Antonio
Convert Miles To Kilometers program in java
1 Answer(s)      5 years and 5 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.

Ads
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();
}
}

Ads









Related Tutorials/Questions & Answers:
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
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
Advertisements
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> <
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
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
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
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
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks
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
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
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
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
How to convert String Date to Timestamp in Java?
for converting, creating, updating date object in Java program. In this example program we are going to convert String representation of Date object... is the full code of the program to convert Date String format to Timestamp
How to convert string to byte in Java
How to convert string to byte in Java  How to convert string to byte in Java
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
java program for
java program for   java program for printing documents,images and cards
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
Number Convert - Java Beginners
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... passString(int number){ int num, q ; if (number < 10 ){ displayOutput(a[number
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
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
convert an pdf file to html in Java
convert an pdf file to html in Java  Hi all, How to convert an pdf file to html in Java? Currently all my data is generated into a report in pdf... implementing this, is there any source code in java? Thanks
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
Java convert file to byte array
Java convert file to byte array  How to convert file to byte array in Java? Thanks   Hi, To convert file to byte array in Java you have to follow the following steps: a) Create the object of InputStream class b
Convert text file to XML file in Java
Convert text file to XML file Java In this section, you will learn how to convert the text file into xml file in Java language. To do this, we have used StreamResult which acts as an holder for a transformation result in XML. After
java program
java program  write a program to print 1234 567 89 10
Convert Class File to .java File - Java Beginners
Convert Class File to .java File  Sir, How To Convert .class file to .java File. Please Help me  Use cavaj software ,which is freeware, to convert .class to .java  Use jd-gui to convert ur .class to .java