Java Reference
In-Depth Information
Then the method should produce the following console output:
12 numbers, sum = 214
8 evens (66.67%)
3. Write a method called negativeSum that accepts a Scanner reading input from a file containing a series of
integers, and print a message to the console indicating whether the sum starting from the first number is ever negative.
You should also return true if a negative sum can be reached and false if not. For example, suppose the file
contains the following text:
38 4 19 -27 -15 -3 4 19 38
Your method would consider the sum of just one number (38), the first two numbers (38
4), the first three
numbers (38
19), and so on to the end. None of these sums is negative, so the method would produce the
following output and return false :
4
no negative sum
If the file instead contains the following numbers:
14 7 -10 9 -18 -10 17 42 98
The method finds that a negative sum of
8 is reached after adding the first six numbers. It should output the
following to the console and return true :
sum of -8 after 6 steps
4. Write a method called collapseSpaces that accepts a Scanner representing an input file as its parameter, then
reads that file and outputs it with all its tokens separated by single spaces, collapsing any sequences of multiple
spaces into single spaces. For example, consider the following text:
many spaces on this line!
If this text were a line in the file, the same line should be output as follows:
many spaces on this line!
5. Write a method called readEntireFile that accepts a Scanner representing an input file as its parameter, then
reads that file and returns its entire text contents as a String .
6. Write a method called doubleSpace that accepts a Scanner for an input file and a PrintStream for an output file
as its parameters, writing into the output file a double-spaced version of the text in the input file. You can achieve
this task by inserting a blank line between each line of output.
7. Write a method called wordWrap that accepts a Scanner representing an input file as its parameter and outputs each
line of the file to the console, word-wrapping all lines that are longer than 60 characters. For example, if a line con-
tains 112 characters, the method should replace it with two lines: one containing the first 60 characters and another
containing the final 52 characters. A line containing 217 characters should be wrapped into four lines: three of length
60 and a final line of length 37.
8. Modify the preceding wordWrap method so that it outputs the newly wrapped text back into the original file. (Be
careful—don't output into a file while you are reading it!) Also, modify it to use a class constant for the maximum
line length rather than hard-coding 60.
9. Modify the preceding wordWrap method so that it only wraps whole words, never chopping a word in half. Assume
that a word is any whitespace-separated token and that all words are under 60 characters in length.
Search WWH ::




Custom Search