Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java program 1 a) Write a program that reads a distance in kilometres as a doubl

ID: 3875863 • Letter: J

Question

Java program

1 a)

Write a program that reads a distance in kilometres as a double value from the console, then converts it to miles and displays the result, to 3 decimal places. Assume that 1 km = 0.625 miles.

Note: the Scanner class provides convenient ways of doing input from the console

b) Write a program that prompts the user to enter an elapsed time in seconds as an integer, then computes and displays the equivalent numbers of hours, minutes and seconds.

For example, if the user inputs 7520, the program should print

Hint: you will need to use the / and % operators here.

c) Write a program that prompts the user to enter the coordinates of two points (x1,y1)(x1,y1) and (x2,y2)(x2,y2), using the double type for all values. Your program should then display the Euclidean distance between these two points.

Explanation / Answer

1)

KMsToMiles.java

import java.util.Scanner;

public class KMsToMiles {

public static void main(String[] args) {

//Declaring variables

double kms, miles;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.print("Enter No of Kilometers :");

kms = sc.nextDouble();

//Converting kms to miles

miles = kms * 0.625;

//Displaying the output

System.out.printf("The Corresponding no of Miles is : %.3f ", miles);

}

}

_________________

Output:

Enter No of Kilometers :90
The Corresponding no of Miles is : 56.250

_________________

2)

SecondsToHrsMinsSecs.java

import java.util.Scanner;

public class SecondsToHrsMinsSecs {

public static void main(String[] args) {
//Declaring variables
int fromseconds, hours, minutes, seconds, toSeconds;

//Scanner class Object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);

while (true) {
//getting the number of seconds entered by the user
System.out.print(" Enter Total Number of Seconds :");
fromseconds = sc.nextInt();
if (fromseconds >= 86400) {
System.out.println("** Invalid.No of secs Must be less than 1 day **");
continue;
} else
break;
}


//calling the method secondTime() by passing the seconds as arguments
secondTime(fromseconds);

}
/*This method will converts the seconds to hours,minutes,seconds
* Params:total Seconds
* Return:String
*/
private static void secondTime(int totseconds) {
int remSecs;
//Declaring variable
int secs = totseconds;

//calculating the number of hours
int hours = totseconds / 3600;
remSecs = totseconds % 3600;

//calculating the number of minutes
int minutes = remSecs / 60;
remSecs = remSecs % 60;

//Calculating the number of seconds
int seconds = remSecs;

//displaying the total seconds to hours,minutes,seconds
System.out.println(secs + " Seconds corresponds to " + hours + " hours " + minutes + " minutes " + seconds + " seconds");


}
}

____________________

Output:


Enter Total Number of Seconds :7520
7520 Seconds corresponds to 2 hours 5 minutes 20 seconds

__________________

3)

DistanceBetweenTwoPoints.java

import java.util.Scanner;

public class DistanceBetweenTwoPoints {

public static void main(String[] args) {

double x1,x2,y1,y2;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

System.out.print("Enter the Point p1 (x1,y1) :");

x1=sc.nextDouble();

y1=sc.nextDouble();

System.out.print("Enter the Point p2 (x2,y2) :");

x2=sc.nextDouble();

y2=sc.nextDouble();

double res=calcDistance(x1,y1,x2,y2);

System.out.printf("The Distance between (%.1f,%.1f) and (%.1f,%.1f) is %.2f ",x1,y1,x2,y2,res);

}

/* Method implementation which

* calculates the distance between two points

*/

public static double calcDistance(double x1,double y1,double x2,double y2)

{

return Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));

}

}

__________________

Output:

Enter the Point p1 (x1,y1) :4 5
Enter the Point p2 (x2,y2) :6 9
The Distance between (4.0,5.0) and (6.0,9.0) is 4.47

_______________Could you plz rate me well.Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote