Java Reference
In-Depth Information
-
Edit the former file to add cos
6
and check numerically whether sin
2
6
+
cos
2
6
= 1 or not, recompile the code, etc.
-
Copy the file and rename it to implement another simple program that
computes, say, a triangle area, etc.
Then we suggest readers become familiar with input/output (I/O) file redirec-
tions (
<
and
>
symbols in command line) before proceeding to the following
exercises.
1.11.2 First set of exercises
Exercise 1.1 (Time conversion)
Write a program
Convert.java
that converts a given number of seconds
int
seconds;
into the format of hours, minutes and seconds. Print this
equivalent number of seconds using a formatting rule like HH:MM:SS.
Exercise 1.2 (Time conversion with user keyboard input)
Modify the previous program to read the number of seconds from the
console. Let us call this program
ConvertIO.java
. For example, the
program execution will yield the following session:
How many seconds? 4000
4000s=1hour, 6 min and 40 sec
Create an input text file
inputsec.txt
that contains the line
4000
,and
run the program by redirecting both input and output to text files:
prompt% java ConvertIO <inputsec.txt
...
prompt% java ConvertIO <inputsec.txt >result.txt
Exercise 1.3 (Time comparison)
Design a program that compares two time durations (in hour/min/sec
format) by first converting them into their respective total number of sec-
onds, and then comparing the two delays using the
if
(booleanPredicate
)
{}
conditional. This voluntarily anticipates the next chapter.
Write another program for comparing the two durations but without
converting them into a total number of seconds. Declare two boolean
values
Smaller
and
Bigger
both initialized to
false
. Then compare
the respective hours, and decide whether we need to assign one of these
boolean values to
true
or not. For hours matching, test the minutes,
{}
else
Search WWH ::
Custom Search