Session 4 |
Tasks (to be submitted by Sun 11.03 23:55) 1 point
- Have you solved the during 3 tasks? If not, do it first!
- Do you any question about before 3 or before 4? If so, write your question to the forum in Moodle or to your instructor.
- Can you answer to the questions in the forum (if there are any)? If so, write the answer.
Task 1.
Make up a sentence which has at least 7 words. Create StringAnalysis
class which looks for all the words in the sentence and measures their length. The program has to print the initial sentence, the words and their length.
Task 2.
In file names.txt, there are names and surnames of people:
Aadu Suur Mari Sale Blond Mari-Liis Maasikas Timothy Henry Charles Tamm
Pay attention that some people have several names (delimitated by a space or a hyphen).
Write a program in class Acronyms
which prints the people's name in the format: surname and the first letter of the name(s). If the first name has a hyphen, it must stay there; in such case, the point must be after the second name.
For example (using the above mentioned file):
Suur A. Blond M. S. Maasikas M-L. Tamm T. H. C.
Extra 1 (optional)
Try to solve the task so that it would be possible to add the following line:
System.out.println(Acronyms.shorten("Mari Sale Blond")); // output: Blond M. S.
Extra 2 (optional)
Solve the task so that it would be possible to define the file name via the command line (use the array of the main method). The program should also work if the file name is not given via the command line - in such case, the program should use the file by default (names.txt). In other words, if you run the program in the command line, command java Acronyms
should give the same result as java Acronyms names.txt
.
Task 3.
Babysitter company has a list of its workers in a file in the format shown below:
1 Mary 1982 534123456 Tartu 2,5 2 Sokk 1985 534765432 Tallinn 6 30 Maria 1994 534191919 Tallinn 1,5 41 Jane 1960 534333444 Rakvere 25
In the file, the data about each babysitter is presented on one line in the following sequence: babysitter's code, first name, year of birth, national id number, city and the length of employment in years. The babysitters' codes in the file might not be sequential, yet in the ascending order.
Create Babysitter
class which has private instance fields for the babysitter's code (int
) and for the name (String
). The class must have a constructor , get
methods and toString
method.
Create BabySitting
class. The program in this class has to read the data about the babysitters from babysitters.txt
file (the file is encoded in UTF-8 format) and place the data into a list (the list elements are of type Babysitter
).
Add babysitter Jana, whose code is 35, into the list so that the order of the codes is not broken (the codes in the list must stay in the ascending order).
Check if the babysitter whose code is 46 is still in the list. If present, delete this element from the list.
Print the data about the babysitters in the format <code> <name>. An example of the output (using the above mentioned file):
1 Mary 2 Sokk 30 Maria 35 Jana 41 Jane
The program must also work in case if the file is empty. To test the program, you can use the following file.
Simplified version
If you get in trouble with Babysitter
class or ArrayList<Babysitter>
list, try to solve the task using two lists: one for the babysitters' codes ArrayList<Integer>
and the second one for the names ArrayList<String>
.
Session 4 |