Session 4 |
Tasks (to be submitted by Wed 6.03 12:55) 1 point
- Have you solved the during 3 tasks? If not, do it first!
- Do you have any question about before 3 or before 4? If so, write your questions to the forum in Moodle or to your instructor.
- Can you answer the questions in the forum (if there are any)? If so, write the answer.
- Have you started thinking about the first group work?
Task 1.
Make up a sentence which has at least 7 words. Create a program called StringAnalysis
which looks for all words in the sentence (usually words are separated by a space) and measures their length. The program has to print the initial sentence, the words and their length.
Task 2.
Creare a .txt file called names.txt which contains names and surnames of some 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 called Acronyms
which prints the people's names 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 console (use the array of main method). The program should also work if the file name is not given via the console - in such case, the program should use the file by default (names.txt). In other words, if you run the program in the console, 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 as follows:
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 years of employment. 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 the main class which is called BabySitting
. The program in this class has to read data about the babysitters from babysitters.txt
file (the file is encoded in UTF-8 format) and place the data into a dynamic list (the list elements are of type Babysitter
).
In the main class, add babysitter Kate 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 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 Kate
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 |