
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.

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