Session 11 |
Tasks (to be submitted by Sun 29.04 23:55) 1 point
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.
You task is the following. 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 calleddeadline
; - 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.
Session 11 |