Arvutiteaduse instituut
  1. Kursused
  2. 2019/20 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2019/20 kevad

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links

Task 1. Encoding

The main goal of the task is to test different encodings when the program reads from the file or writes into the file. First of all, invoke the method Charset.defaultCharset() and print out the result.

If the operating system is Windows, the output is generally windows-1257; if the operating system is Linux or Mac OS, the output is generally UTF-8; however, the encoding depends on the individual settings of each computer. The computer preset encoding is used in Java I/O programs, if no other encoding is set in the program.

Write a program which tests different encodings:

  1. The program has to write into the file the following sentence "Andja käsi väsis ärä, võtja käsi ei väsi kunagi". Do not set any encodings yet.
  2. Add the functionality of reading from the file into the program. This time set the encoding so that is differs from the default encoding of your computer (e.g. if the default encoding is windows-1257, use UTF-8). Run the program and explain the result.
  3. Change the program so that if the program writes into the file, a non-default encoding is used for writing. Do not set any encodings for the reading (use OS default encoding). Explain the output of the program.

Task 2. Passwords

Write a program which has a static method called findPsw. The method has one parameter for the name of a file (String). The method looks for passwords in this file and writes the passwords into another file - "psw.txt". Each password in the file consists of a single word placed after the keyword " parool: ". A sample file with hidden passwords is here: http://www.ut.ee/~eno/peidetud.txt (save the file on your computer). In the main method, invoke findPsw.

Task 3. Buffered streams

Write a program which reads a word from the console and writes it into the file using the BufferedWriter class. After that the program has to read the word from the file using the BufferedReader class and output the word to the console. Use UTF-8 encoding. Note: do not use Scanner.

Task 4. Person

Create a class called Person. The class should have private instance fields for the first name (String), the last name (String) and the list of children's names (List<String>). The class should also have a constructor which instantiates the instance fields.

In the client class, create an instance of the class Person and write it into the file using the DataOutputStream stream. Create one more instance of the Person class and instantiate it with the data from the DataInputStream stream.

Output the data before writing into the file and after reading from the file. Note: the number of children's names in the list is not fixed (the number of children should be added into the file in order to ease reading from the file).

Task 5. Close streams

The goal of the task is to check what happens if the file is not closed. Write a program which opens a file given in the console. Then the program has to ask the user for a joke using System.in. Run the program and let it wait for the joke. In Windows File Explorer, try to rename the file. What happens? Change the program so that it closes the file before the user is prompted for a joke. Run the program and try to rename the file.

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused