Exercise Set 1

Put your solutions under the set1 directory in your repository.

Don't forget to commit your changes locally and then push them up to GitLab.

Ex1

In a file called Ex1.java, write a program that reads a temperature in degrees Celsius as a double value from the console, then converts it to degrees Fahrenheit and displays the result, to 1 decimal place.

Here is an example of program input and output:

$ java Ex1
Enter Celsius temperature: 14.2
14.2°C = 57.6°F

Note: the Scanner class provides convenient ways of doing input from the console - see Liang's book or the API documentation for examples. The printf method of System.out can be used to do formatted output, in much the same way as in C. You can generate the degree symbol using the Unicode escape sequence \u00b0.

Ex2

In a file called Ex2.java, 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

2 hours, 5 minutes, 20 seconds

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

Ex3

In a file called Ex3.java, write a program that prompts the user to enter the radius \(r\) of a circle, as a double value. If the entered value is less than or equal to zero, your program should print an appropriate error message; otherwise, it should compute and display the area \(A\) and perimeter \(P\) of the circle, given by $$A = \pi r^{2}, \;\;\; P = 2\pi r$$

\(\pi\) is available to your code as the constant Math.PI.

You should use System.out.printf to format numeric output to 3 decimal places.