JAVA PROGRAMMING:
Create a program which will calculate and output as formatted output the kilometers or miles, given as keyboard input the miles or kilometers, as inputted as a double number. That is, the user has a choice what to calculate. The original value inputted should be displayed along with the calculated miles or kilometers. Demonstrate escape sequences, a while loop or a do…while loop, and named constants as needed, should be implemented. Finally, input, processing, and output must be implemented by methods; at least three, probably four methods.
Please find below code and don't forget to give a Like.
Here, we used total of 4 methods as mentioned.
1.input (input())
2.processing(conversion_miles2kms())
3.processing(conversion_kms2miles())
4.output(display_output())
Scanner is used to take input from user. Check the below screenshot on how to give the inputs:
Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
double value,convert_Value=0;
boolean b=true;
char inp=' ';
value=input();
while(b){
System.out.println("Enter 'k' to
convert Miles to KMs or 'm' for viceversa:");
Scanner input1=new
Scanner(System.in);
inp=input1.next().charAt(0);
if(inp=='k' || inp=='m'){
b=false;
}
}
if(inp=='k'){
convert_Value=conversion_miles2kms(value);
System.out.print(value+" miles
converted value into kms is ");
}
else if(inp=='m'){
convert_Value=conversion_kms2miles(value);
System.out.print(value+" kms
converted value into miles is ");
}
display_output(convert_Value);
}
public static double input(){
double val;
System.out.println("Enter a value to convert");
Scanner input=new Scanner(System.in);
val=input.nextDouble();
return val;
}
public static double conversion_miles2kms(double value){
double convert;
convert=value*1.6;
return convert;
}
public static double conversion_kms2miles(double value){
double convert;
convert=value/1.6;
return convert;
}
public static void display_output(double convert_Value){
System.out.println(convert_Value);
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.