Chapter 4 |
Tasks (to be submitted by Mon 9.03)
- Have you solved the during 4 tasks? If not, solve them first!
- Do you have any question about Chapter 3 or Chapter 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 that has at least 7 words. Create a program called StringAnalysis which finds all words in the sentence (usually words are separated by a space) and measures their lengths. The program has to print the initial sentence, the words and their lengths.
Task 2
Create 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
Note that some people have several names (delimitated by a space or a hyphen).
Write a program called Acronyms, which reads the names form the file and prints them on the screen in the following format: surname, followed by the first letter of the name with dot. If the first name has a hyphen, it must stay there.
For example (using the file above):
Suur A. Blond M. S. Maasikas M.-L. Tamm T. H. C.
Extra 1 (optional)
Try to solve the task in such a way 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 in such a way that it would be possible to give the file name via the console (use the parameter 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
The 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 number of years of employment (which can also be a fractional number). The babysitters' codes in the file might not be sequential but they are in the ascending order.
Create a class Babysitter, 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 the file babysitters.txt (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 a 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 file above):
1 Mary 2 Sokk 30 Maria 35 Jana 46 Kate
The program must also work in the case if the file is empty. To test the program, you can use the following file.
Simplified version
If you get into 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>
.
Chapter 4 |