Convert Miles To Kilometers program in java

Convert Miles To Kilometers program in java

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 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
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
Kilometers per Liter to Miles per Gallon
need to convert the kilometers per liter into miles per gallon...Kilometers per Liter to Miles per Gallon          Kilometers per liter
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
Java program - convert words into numbers?
Java program - convert words into numbers?   convert words into numbers?   had no answer sir
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
Write a program in Java to convert Decimal number into Binary number
Write a program in Java to convert Decimal number into Binary number  Hi, I have decimal number and I want to convert it to Binary. How to Write a program in Java to convert Decimal number into Binary number? Thanks
ModuleNotFoundError: No module named 'Miles'
ModuleNotFoundError: No module named 'Miles'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'Miles' How to remove the ModuleNotFoundError: No module named 'Miles'
ModuleNotFoundError: No module named 'miles_nester'
ModuleNotFoundError: No module named 'miles_nester'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'miles_nester' How to remove the ModuleNotFoundError: No module named '
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
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
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 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
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
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
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
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
To convert Speech to Text in java
To convert Speech to Text in java  package abc; import javax.speech.*; import javax.speech.recognition.*; import java.io.FileReader; import java.util.Locale; public class HelloWorld extends ResultAdapter { static Recognizer
java program for
java program for   java program for printing documents,images and cards
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
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
java program
java program  write a program to print 1234 567 89 10
java program
java program  Write a program to demonstrate the concept of various possible exceptions arising in a Java Program and the ways to handle them.  ... in Java
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  Write a program to create an applet and display The message "welcome to java
java program
java program  hi friends how to make a java program for getting non prime odd numbers in a given series
java program
java program   Write a program to find the difference between sum of the squares and the square of the sums of n numbers
java program
java program  write a program to create text area and display the various mouse handling events
java program
java program  Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall
java program
java program  . Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
Java program
Java program  Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results
program in java
program in java  write a reverse program in java using string buffer.the input and out put as follows. input- hi good mornig out put-ih doog ginrom
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
java program
java program  write a java program to compute area of a circle.square,rectangle.triangle,volume of a sphere ,cylinder and perimeter of cube using method over riding
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 program
java program   A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A java program to display above triangle
java program
java program  write a program to create server and client such that server receives data from client using BuuferedReader and sends reply to client using PrintStream
java program
java program  write a program to read 10 numbers from user and store it in a array. display the maximum n minimum number in the array
java program
java program  write a java program to read a file which hold email address validate email address tohave formate @.* and replace all .com email address

Ads