a. Write a Java class that declares named constants to represent the number of k
ID: 669484 • Letter: A
Question
a. Write a Java class that declares named constants to represent the number of kilometers (1.852) and the number of miles (1.150779) in a nautical mile. Also declare a variable to represent a number of nautical miles and assign a value to it. Compute and display, with explanatory text, the value in kilometers and in miles. Save the class as NauticalMiles.java. b. Convert the NauticalMiles class to an interactive application. Instead of assigning a value to the nautical miles variable, accept it from the user as input. Save the revised class as NauticalMilesInteractive.java.
Explanation / Answer
working java code compiled on ideone
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
double nmiles,miles,km;
nmiles=8;
km=1.852*nmiles;
miles=1.150779 * nmiles;
System.out.println("Kilometers:" + km);
System.out.println("Mles:" + miles);
}
}
http://ideone.com/R0RwmF
b)
working java code for second part
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
double nmiles,miles,km;
Scanner sc=new Scanner(System.in);
nmiles=sc.nextDouble();
km=1.852*nmiles;
miles=1.150779 * nmiles;
System.out.println("Kilometers:" + km);
System.out.println("Mles:" + miles);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.