Distance Converter in Java

Overview

In this blog, we will develop a Java program that can convert distance in kilometers to miles and vice versa.

There are two two different units for the measurement of distance: Kilometers and Miles.

In general, kilometers are used to measure long distances whereas miles are used for comparing relatively shorter distances. Kilometers are used for metric systems whereas miles are used for imperial systems.

We will make use of two mathematical formulae for the conversion.

Distance in Miles = 0.621371 * Distance in Kilometers

Distance in Kilometers = 1.60934 * Distance in Miles

Distance Conversion

Program Description

Input the choice of conversion(Either ‘Km’ to ‘Miles’ or ‘Miles’ to ‘Km’). If it is an incorrect choice, display an error message and exit the program. The task is to convert kilometers to miles and vice versa according to the choice given by the user. Use try & catch blocks to handle the errors.

Source Code

Here is the complete source code of the Java program to convert kilometers to miles and vice versa.

Program Explanation

  1. “Scanner” class and its function “next()” is used to obtain the input from the user and “println()” class is used to print the output.
  2. The first “if” statement displays a message and terminates the execution if the entered choice is neither 1 nor 2.
  3. The second “if” statement checks if the choice entered is ‘1’. If it is true, it calls the static method “toMiles()”. This method gets the distance in kilometers from the user and multiplies it with 0.621371 to convert it to miles. It then returns the “miles” to the variable “milesResult” in main() function. The output is printed on the console.
  4. Otherwise, the “else” statement gets executed if the choice entered is ‘2’. It calls the static method “toKilometers()”. this method gets the distance in miles from the user and multiplies it with 1.60934 to convert it to kilometers. It then returns the “kilometers” to the variable “kilometersResult” in main() function. The output is printed on the console.
  5. If any exception occurs during the execution, it will be caught by the “catch” block.

Test Cases

  1. Choose any one option:
    1. Convert Kilometers to Miles
    2. Convert Miles to Kilometers
    Your choice(‘1’ or ‘2’): 1
    Enter distance in Kilometers: 3
    Distance in miles: 1.8641130000000001
  2. Choose any one option:
    1. Convert Kilometers to Miles
    2. Convert Miles to Kilometers
    Your choice(‘1’ or ‘2’): 2
    Enter distance in Miles: 1.5
    Distance in kilometers: 2.41401
  3. Choose any one option:
    1. Convert Kilometers to Miles
    2. Convert Miles to Kilometers
    Your choice(‘1’ or ‘2’): 12
    OOPS! Incorrect option!

Are you looking to master coding and crack top company interviews? Go ahead and try GUVI’s Pro Subscription to help you with everything needed to build a strong Technical Career.

Click here to know more about GUVI.

Also, download GUVI’s app to learn the latest IT skills in vernacular languages.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Better z-index management with Sass

Tutorial: Crowdsourcing tasks that need specific skills or traits

How to install OpenPBS 20.0.1 on ubuntu

5 Step process for Cloud Migration — Part II

How to install any Custom Component from GitHub in less than 5 minutes?

Neo Monthly Report — July 2020

Segfaults in Golang with Cgo

Why cloud is leading to a renaissance of IT infrastructure

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Baruni Priya T S

Baruni Priya T S

More from Medium

Create a Custom Annotation In JAVA

Java I/O Character Stream Implementation

What is Eclipse MicroProfile?

Lets create a java sample project part 1