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

Object-Oriented Programming (Narva College) 2018/19 spring

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

slides_s11.pdf

NB! The practical session exercises are always provided enough and to spare with an eye to fast / experienced students have exercises to solve. To this end, do not get upset if you do not get on all the task in class. Take your time and complete the tasks at home!

Task 1. BufferedWriter

Create a program which uses the BufferedWriter class to write the following text Hello world into the file given as a program argument. Use the UTF-8 encoding to write into the file. Do not use the close() method nor try-with-resources. Run the program and check the file. Explain the output in the comments. Modify the program with try-with-resources so that it closes the BufferedWriter resource.

Task 2. Picture

Create an object class called Picture for monochrome pictures (pictures in black and white). Each pixel of a picture is encoded by a value from the range [0...255] (0 denotes the black colour, 255 is the white colour and the rest are the shades of grey). The picture size is instantiated in the constructor: the first parameter is the length of a picture and the second parameter is the width of a picture. It is assumed that initially all pixels of a picture are black. Add a method called setPixel which takes in three parameters - x (the length), y (the width) and a new value of the pixel. Note that the coordinates of the top right corner are (0;0). Add a method called getPixel which takes in two parameters - the coordinates of a pixel - and returns the value of the pixel.

Create two exception classes called InvalidCoordinateException and InvalidPixelException. Both should be subclasses of the RuntimeException class. Change the setPixel method so that the value is assigned after it has passed the validity check (on coordinates and colours); otherwise, the corresponding exception is thrown out. Add the check on the valid pixel coordinates to the getPixel method.

Create a test class which creates a 10x10 picture and asks the user for the methods via System.in. The user should have the opportunity to see the pixels and change them. If the entered coordinates of the pixel are invalid, the program has to prompt the user once again; if the entered value of the pixel is invalid, the program has to output some comments and exit.

Hint: use JavaFX to solve the task.

Task 3. Bank account

Create an exception class called NotEnoughMoneyException. Create an exception class called NegativeSumException.

Create a class called MyAccount which has a private instance field of String data type for the account owner and a private instance field of double data type for the amount of money on the account. The class should have the set methods for all the instance fields. If the entered sum of money on the account is negative, the NegativeSumException exception is thrown out. The class should have a constructor which instantiates the instance fields using the set methods. The class should have the overridden method toString. Also the class should have a method called buy which takes in a double data type value as an argument (the price of a purchase) and reduces the sum of money on the account correspondingly. If there is not enough money on the account, the NotEnoughMoneyException is thrown out. Furthermore, the class has a method called putMoneyOnAccount which takes in a double data type value and adds it to the sum of money on the account using the set method.

In the client class called TestAccount, create an instance of the MyAccount class, make two purchases, put some money on the account, catch the exception and output their info (the account and the exception).

Task 4. Nit-picker

Create an exception class BlankSpaceException which is thrown out if there is a blank space before a punctuation mark (a full stop, an exclamation point or a question) in the string. In the client class, add a piece of code which asks the user to enter a text line by line. If a line contains a blank space before the punctuation mark, the BlankSpaceException exception should be thrown out.

  • 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