Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

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

Tasks (to be submitted by 20.04)

Task 1. Correct the program

John wrote a program which helps get some free space on the hard disk. The first argument of the program is a file name which John does not often use. The second argument of the program is a name of a zip file where to place the first file. Once the file is zipped, the program deletes the initial file.

NB! The following code contains all sort of mistakes. Do not consider it as a good example!

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class MyZip {
  public static void main(String[] args) {
    File myFile = new File(args[0]);
    File myZippedFile = new File(args[1]);

    zipMyFile(myFile, myZippedFile);
    System.out.println("The file is zipped.");

    myFile.delete();
    System.out.println("The initial file is deleted.");

    System.out.println("Have a nice day!");
  }

  private static void zipMyFile(File myFile, File myZippedFile) {
    FileInputStream inputStream = null;
    try {
      inputStream = new FileInputStream(myFile);
      ZipOutputStream zipStream = new ZipOutputStream(
          new BufferedOutputStream(new FileOutputStream(myZippedFile)));

      zipStream.putNextEntry(new ZipEntry(myFile.getName()));
      byte[] buffer = new byte[1024];
      int length;
      while ((length = inputStream.read(buffer)) > 0) {
        zipStream.write(buffer, 0, length);
      }
      zipStream.closeEntry();

      inputStream.close();
      zipStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      inputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Once the program was written, IntelliJ marked in red several lines of code. John knew that these methods may throw IOException and, therefore placed the methods into the try block. IntelliJ filled automatically the catch block, and John did not add anything else there. John tested his program and everything worked well.

John gave his program to Mary because Mary wanted to get some free space on her computer. Mary used John's program to zip her e-books, and the program showed The file is zipped. In some day, Mary wanted to read the book and opened the zip file. Mary was very angry at John because the zipped file was empty and the initial file was deleted.

Change the program so that it deletes the initial file if no exception occurs. Rewrite the program so that the exceptions are caught only if they can be fixed; otherwise passed forward. All the program streams must be closed using the try-with-resources.

Task 2. Multiplication

Create a program which prompts the user for an integer, multiplies it by two and prints the result out. Convert the input into integers using the Integer.parseInt method. Note that the user may input whatever he wants (e.g. text) - in such case the parseInt method throws out the NumberFormatException exception. Add the catch block which catches the exception and asks the user for an integer.

Task 3. Deadline of a home assignment

Create an object class which has:

  • a private instance fields of the LocalDate data type called deadline;
  • a constructor which instantiates the field;
  • a get method;
  • a set method.

The deadline cannot be on Saturday or Sunday - if it happens, the IllegalArgumentException exception with a message about inappropriate date (in the format dd.mm.yyyy) is thrown out. Demonstrate the method using appropriate and inappropriate deadlines. Hint: use the getDayOfWeek method to show the day of the week.

Chapter 10
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment