Saturday, February 2, 2013

Java CONSTANT, How to convert miles to kilometer

Java Programming, Constant

To declare CONSTANT the syntax as stated below,

final datatype CONSTANTNAME = value;

In this example , we declare constant MILES_TO_KILOMETER as stated below

final double MILES_TO_KILOMETER = 1.609 ;

I use scanner class to read the miles value, the full code as stated below.

// Miles to kilometer example

// Use scanner class

import java.util.Scanner;

public class MilesKilometer {
public static void main(String[] args){
double miles ;
// use constant 
final double MILES_TO_KILOMETER = 1.609 ;
//Initialize scanner class
Scanner newscan = new Scanner(System.in);
System.out.print("Enter miles value : ");
// Read miles value
miles = newscan.nextDouble();
// Print out the value
System.out.println(" Kilometer value is : " + ( miles *  MILES_TO_KILOMETER ) + "\n");

}
}

No comments:

Post a Comment