Exercises Java Advanced Features
Exercises Java Advanced Features
Task 1
Point2D class
toString method which should return string in the following format: (x,
y)
Point3D class
Using the Point2D class implement the Point3D class. It should extend the
Point2D class. It should contain:
getter method which will be responsible for returning the z eld value
setter method which will be responsible for setting the z eld value
Task 2
Person class
setter methods which will be responsible for setting name , address elds
values
toString method which should return string in the following format: ?->? ,
where ? is the name and adress value accordingly
Student class
Staff class
Implement the Lecturer class. It should extend the Person class.
Implementation should meet the below criteria:
Task 3
Shape class
color information
non-arguments constructor which will set: the color eld to unknown and
the isFilled eld to false
Circle class
Implement the Circle class. It should extend the Shape class. Implementation
should meet the below criteria:
information about the radius value
non-arguments constructor which will set: the color eld value to unknown ,
the isFilled eld value to false and the radius eld value to 1
getter methods which will be responsible for returning the radius value
setter methods which will be responsible for setting the radius value
getArea method which will be responsible for calculating the surface area
Rectangle class
non-arguments constructor which will set: the color eld value to unknown ,
the isFilled eld value to false and the width and length eld value to
1
getter methods which will be responsible for returning the width and
length value
setter methods which will be responsible for setting the width and
length value
getArea method which will be responsible for calculating the surface area
Implement the Square class which will extend the Rectangle class. It should
not add any new eld or feature, but it should force square behaviour on the base
class methods.
Task 4
Modify implementation prepared as a scope of task 2. Refactor the following
functionality:
Each class which is extending the Shape class directly/indirectly should override
abstract methods from base class.
Task 5
Implement the Line class which will contain (as a composition) the instance of
two Point2D object from task 1. Those points should be the start and end point
of the line. In addition, this class should implement:
getter method which will be responsibe for returning start and end points
setter method which will be responsible for setting start and end points
the method responsible for calculating the length of the line based on the
set points
the method responsible for returning the coordinates of the point being the
center of the created line
Task 6
Implement the Movable interface which will contain a de nition of common
methods for MovablePoint and MovableCircle classes.
void moveUp()
void moveDown()
void moveLeft()
void moveRigth()
MovablePoint class
The MovablePoint class should implement the Movable interface and should
contain 4 elds: int : x , y , xSpeed , ySpeed . The x , y elds should de ne the
coordinates of point and the xSpeed , ySpeed elds should determine how much
the appropriate coordinates should change.
MovableCircle class
The MovableCircle class should implement the Movable interface and should
contain (as a composition) instance of MovablePoint class. It should contain
elds necessery to de ne circle radius.
Task 7
Implement the GeometricObject interface which should contain common
behaviours de nition for each sub class:
double getPerimeter()
double getArea()
Circle class
Resizable interface
ResizableCircle class
Task 1
Create the divide method which has to divide the two numbers that are the
attributes of the method. In case the second parameter of the method is 0, a
non-default exception should be thrown: CannotDivideBy0Exception .
Task 2
BookRepository class
searching for objects of the Book type with the indicated name
Book class
id
title
author
year of release
NoBookFoundException
In case of lack of searched results an exception should be thrown. This
exception should accept the String parameter object with information about
which elements could not be found.
Classes and interfaces - exercises
Task 1
Create the UserValidator class which with the validateEmails method will be
responsible for validating user data such as: email, alternative email. Within the
scope of the validateEmails method, please create the local Email class
which will be responsible for formatting the provided email. Validation should
cover the following scenarios:
if the given email address is empty or it is null, set the value to unknown
if the given email address does not meet the email criteria, set the value to
unknown (use regular expressions)
Task 2
Movie class
Create the Movie class which will cover elds: title, director, year of release,
genre, distributor. This class should contain a default constructor and getter
and setter methods. Please consider creating toString method which will be
responsible for returning info about a speci c object.
MovieCreator class
methods which will be responsible for setting movie values. Each method
should return an object instance of the object for which the method is being
called
the createMovie method will create the instance of the Movie class. It will
return it as a method results
Task 3
Car class
Create the Car class which will store information about a car make and type. It
should contain getter and setter methods.
Engine class
Implement the Engine class which will be a nested non-static class under the
Car class. This class should contain the eld: engine type and setEngine
method which will set a type based on the car type. If the car type is economy ,
then the engine type should be set to diesel . If the car type is luxury , then the
engine type should be de ned as electric . Otherwise, the engine type should
be de ned as petrol .
Task 4
Validator interface
Create the Validator interface, which will include the boolean validate(T
input) method.
User class
Create the User class which will include: * elds: name, last name, age, login,
password * default constructor * setter and getter methods * setter
methods should accept as method params: value for the eld and the
Validator interface instance * setter methods should execute the validate
method based on the instance of the transferred object. The parameter passed
to the validate method should be the value of the argument
Anonymous class
last name validation: the last name cannot be empty or null, it should start
with a capital letter
Task 1
Crete an enum Weekday with constants MONDAY , TUESDAY , ... SUNDAY . The
enum should contain boolean isWeekDay and boolean isHoliday methods.
The isHoliday method should return the opposite result to the call of the
isWeekDay method. Please implement the whichIsGreater method as a scope
of the enum class. This method should accept an object of Weekday type. This
method should display information that the indicated day of the week is the
predecessor or successor of the day of the week passed as the method
argument. Please consider using the compareTo method.
Task 2
Create the PackageSize enum class with constants SMALL , MEDIUM , LARGE .
Enum should include two parameters in the constructor:
The PackageSize enum class should adopt the static getPackageSize method.
This method should accept package size and as a result it should return a
speci c PackageSize object based on the package size passed.
Task 3
Create the TemperatureConvert enum class with constants C_F , C_K , K_C ,
F_C , F_K , K_F . Enum should adopt a constructor that includes three
parameters:
input temperature unit
This method should return the converted value. To nd the right constant from
set of enum values, use the values() method.
Collections - exercises
Task 1
Implement the SDAArrayList<T> class which will implements ArrayList<T>
logic. For this purpose please implement following methods:
add
remove
get
display
Task 2
Author class
Implement the Author class which will contains elds: name, last name, gender.
Please consider all available methods and constructor parameters. Please
prepare hashCode and equals implementation.
Book class
Implement the Book class which will contains: title, price, year of release, author
lists, genre (represented as enum class) elds. Please consider all necessery
methods and constructor parameters. Please prepare hashCode and equals
implementation.
BookService class
Implement the BookService class which will include book lists and it needs to
cover following methods:
Task 3
Based on 100 element array with randomly selected elements from the range
0-50 please implement following features:
returning a list of elements that have been repeated in the generated array at
least once
Task 4
Based on Task 2 please implement a method which will be responsible for
returing unique key-value pairs. The key should be represented as a book genre,
value need to contain a title.
Task 5
Based on Task 2 , implement a method that will be responsible for creating a
stack of books sorted from highest to lowest price.
Functional programming - exercises
Task 1
Using the functional programming mechanisms based on the given structure,
please provide:
enum VideoType {
CLIP, PREVIEW, EPISODE
}
class Video {
public String title;
public String url;
public VideoType videoType;
class Episode {
public String episodeName;
public int episodeNumber;
List<Video> videos;
class Season {
public String seasonName;
public int seasonNumber;
List<Episode> episodes;
Task 1
Create the Pair class which, based on generic types, will allow to store any pair
of objects.
Task 2
Design the countIf generic method wich, based on an array of elements of any
type will count the number of elements meeting the condition using an
functional interface. Any interface implemented anonymously can be a function.
Task 3
Design the generic swap method, which will be responsible for swapping the
position of the selected elements of the array.
Task 4
Create a class that will behave like a library for the following types of media:
books
newspapers
movies
Please provide a solution for generic types. For data collection, use any array or
Collection API class.
Task 5
Create a class that will behave like a pet house for the following animals:
cat
dog
Please provide a solution for generic types. For data collection, use any array or
Collection API class.
Java IO - exercises
Task 1
Create a solution which will display all les/directories included in the provided
directory.
Task 2
Prepare a solution which will read and display a le line by line.
Task 3
Prepare a solution which will add a string to the speci ed le.
Task 4
Prepare a solution which will return the longest word from the provided le.
Task 5
Create a CSV parser:
John,Smith,23
Sam,Johnson,40
Andrew,Manly,43
With the le above the program should return the three-element list of objects of
the User type with elds: name, surname, age.
Task 6
Create a program which will provide following features based on the Movie
class objects:
adding objects
The Movie class should contains elds: title , genre , director , year of
release . Adding objects should send their serialized form to a le. Displaing
object list should enable deserialization of the text le to convert individual lines
to Movie objects.
Parallel and concurrent programming -
exercises
Task 1
Write a program that in parallel will nd even numbers in two intervals: 1000-
2000 and 14300-17800 .
Task 2
Write a program that will solve the problem below.
On the road between the towns A and B there is a bridge on which there can be
only one car. Implement a mechanism that will allow synchronized access by a
car objects to object of the Bridge class.
car name
car type
driveThrough, which will accept as parameter the object of the Car class.
The journey should take 5s.
Task 3
Write a program which will execute two independent sorting algorithms on two
separate threads. The main goal of the implementation is to return information
about the algorithm that has completed faster.
Task 4
Write a program which will synchronize access to a bank account. If any cyclical
Internet service wants to charge the account with a higher amount than currently
available, then the thread should be suspended. When additional money will be
transfered to the account, the thread should be raised.
Task 5
Write a data structure that will allow you to navigate the array in two directions:
forward ( next() )
backwards ( prev() )
The data structure should store the currently searched index. Please take care of
its additional synchronization.
Re ection API basics - exercises
Task 1
Implement the Student class with the following details:
the class should contain elds: rst name, last name, no. index, eld of study
setter methods
getter methods
available methods
available elds
available constructors
Task 2
Using the Student class for the task no. 1 please implement following
operations using the re ection mechanism: