Usually, people get confused regarding the parameters that measure distances. This program describes the conversion of distances between miles and kilometres by using Java.
We have different parameters for calculating distance. In this program, we are using miles and kilometres as parameters, and an if-else-if ladder to consider three conditions: if, elseif and else statements. First, we import a package and declare variables for types, m and km as string and double datatypes, respectively. The usage of the Scanner class allows the user to provide input to the system, and a print statement displays the statement kept in parentheses. In the if condition, we use the type 'm'. If it is satisfied, then it proceeds with the loop inside and considers the conversion of miles to kilometres (km=1.60943*miles). If not satisfied, then it checks the next condition: else if. If the parameter is 'k', it satisfies the condition and applies the rule mile=km/1.60943 to convert the distance in miles to kilometres. If the condition is not satisfied, it checks the last condition:else. If the two conditions are not satisfied, then the else part will be executed. It describes the parameters other than m and km.
Tools used:
1. JDK
2. Notepad
The if-else-if ladder comes under selection statements in Java. Users can provide their conditions to get the desired output. In this, we can use n number of else -if statements according to the checking conditions required.
Submitted by YANIGANTI SUCHITRA (Suchitra)
Download packets of source code on Coders Packet
Comments