Exercise Sheet 1

advertisement
Exercise Sheet 1
Revision: Elementary Java
Exercise 1 [optional, for students less familiar with Java]
Write a java program to print “Hello World”. Amend it so that it reads a name from
the standard input (as an example, say the name entered is “Bill”) and prints an hello
message as in “Hello Bill”. (Recall that Console.readString() reads the text
entered on the remainder of the line, or use class Scanner.)
Exercise 2 [optional, for students needing re-familiarisation with Java]
Write a program called Largest which reads a sequence of integers from the
standard input (the keyboard) and prints the largest value read to the standard output
(the screen). The size of the input is unknown, and is signalled by an end-of-file
character. (If you’re not familiar with this, assume the integers are all natural and that
-1 signals the end of the input. Get familiar with formal “end of input” techniques
soon, using either Console or Scanner as preferred.)
Exercise 3 [optional, for students needing re-familiarisation with Java]
Re-run your solution to Exercise 2, but with the input coming not from the keyboard
but from a file called, say, numbers.txt via re-directing the standard input (i.e. the
program is invoked as java Largest < numbers.txt). Prepare
numbers.txt (containing the integers to be read) using any text editor (not
WORD).
Exercise 4 [optional, for students needing re-familiarisation with Java]
Write a program that reads an integer, and prints all its positive divisors if the integer
read is nonnegative, and otherwise prints the message “Sorry, I don’t do negatives”.
Recall that m%n yields the remainder on dividing m by n.
Exercise 5 [optional, for students needing re-familiarisation with Java]
Complete the following program to read a sequence of words, and print the
frequencies of their word-lengths. For example, inputting a text (of 22 words) might
give rise to the following output:
1-letter
2-letter
3-letter
4-letter
6-letter
words:
words:
words:
words:
words:
3
5
9
4
1
Assume that words have length at most 19. Recall that Console.readToken()
(or use the Scanner class) reads the next word from the input, and that the length of
a string s is given by s.length().
class WordCount {
public static void main(String[] args) {
..... read words, building up frequencies in an array
CA213
Exercises 1
1
.... called count (of length 20, the 0th component
.... being unused). Each component of count is initialised to 0
// Print result
i = 1;
while (i<count.length) {
if (count[i]>0)
System.out.println(i + "-letter words: " + count[i]);
i++;
}
}
}
Revision: Elementary algorithms
Exercise 6
Write a program invoked as java Grep pattern where pattern stands for
any string (without spaces). The program reads a sequence of lines from the keyboard
and when all lines have been read, prints out those lines that contain pattern. For
example,
java Grep cat
should display those lines read that contain the word cat. Use an array to store lines;
it is guaranteed that the input consists of no more than 1000 lines.
Exercise 7
Write an integer-valued (static) function sumFacs which takes an integer parameter
n (assumed to be positive) and returns the sum of its positive integer divisors. Write a
boolean-valued (static) function isPerfect which takes an integer parameter n
(assumed to be positive) and determines whether n equals the sum of its positive
integer divisors; make good use of sumFacs. A number is said to be perfect if it
equals the sum of its positive integer divisors. Using isPerfect, write a program
which prints all the perfect numbers less than 1000.
CA213
Exercises 1
2
Download