Programming and Problem Solving with Java

(やまだぃちぅ) #1

264


18.Rewrite the code segment for Exercise 17 so that it prints the times in 10-
minute intervals.
19.Write a loop or loops to count the number of not-equal operators (!=) in a file
that contains a Java application. Your algorithm should count the number of
times an exclamation mark (!) followed by an equals sign (=) appears in the in-
put. Process the input file one character at a time, keeping track of the two
most recent characters (the current value and the previous value). In each iter-
ation of the loop, a new current value is extracted from the input line with
charAtand the old current value becomes the previous value. When EOF is
reached, the loop is finished.
20.Write statements that print a floating-point number on the file outfile, with
four digits after the decimal point. Use the String.valueOfmethod to convert
the floating-point value to a string. (It is a value-returning class method that
takes the floating-point value as an argument.) Then use indexOfto locate the
decimal point. After the decimal point, select the next four digits using the sub-
stringmethod. Note that fewer than four digits may follow the decimal point.
You can check this by comparing the result of indexOfwith the lengthof the
string. When there are fewer than four digits to the right of the decimal point,
you should concatenate “0” characters to the string to fill it out to the correct
number of places.

Programming Problems


1.Design and write a Java application that takes as input an integer and a charac-
ter from the screen. The output should be a diamond on the screen composed
of the character and extending for the width specified by the integer. For exam-
ple, if the integer is 11 and the character is an asterisk (*), the diamond would
look like this:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Free download pdf