Class Assignments
Class Assignments
Java - fundamentals
Basic exercises
Exercise 1
• declare a variable named value_int of type int and assign the value 20 to it
• then declare the value_string variable of the String type and assign the value "Java"
to it
• declare the value_double variable of the double type and assign the value 23.63 to it;
• at the very end, display all variables
Exercise 2
• Declare a variable temp_F of the float type, then, based on the value of the variable,
calculate the value in Celsius: C = (F - 32) * 5 / 9
• Declare a variable of type float represents the temperature in degrees C, then, based
on the formula, convert to temp in K: K = C + 273.15F
Exercise 3
Read in 3 integers from the user and print the largest and smallest of them on the screen.
If the user has entered 3 of the same numbers, the applicable message should be
displayed
Exercise 4
Write a program that, based on the integer variable:evaluation, will display one of the
following information: no promotion to the next class, promotion to the next class,
promotion with an excellent grade
Exercise 5
Write a program, that displays the following formula depending on the provided value: n=4
1
22
333
4444
n=5
1
22
333
4444
55555
1
Exercise 6
Write a Box class that will store elements of any type, e.g. int, String
Advanced exercises
Exercise 1
• declare a variable of type int and assign the value 50 to it
• declare variable another_value of type double and assign value 30.2
• convert a variable from type int to type double and assign the result of the conversion
to the new variable
• convert a variable from type double to type int and assign the result of the conversion
to the new variable
• display the variables
Exercise 2
Write a program that calculates the BMI (body mass index) value according to formula:
weight/height^2 . If the result is in the range (18.5 - 24.9) it prints "normal weight", if below
it is "underweight", if above it is "overweight".
Exercise 3
Write a program that calculates the personal income tax due.
The program is supposed to collect the income from the user and, after calculation, write
the tax due on the screen.
The tax is calculated according to the following rules:
• up to 85,528 the tax is 18% of the base minus PLN 556.02,
• from 85,528 the tax is PLN 14,839.02 + 32% of the surplus over PLN 85,528.00
Exercise 4
Using the switch statement, write a calculator that implements the following options:
• adding
• subtraction
• sharing
• multiplication Handle the case where the user entered a second number equal to 0
when selecting the division options.
Exercise 5
Write a program that draws a "Christmas tree" in the console consisting of asterisks (*).
The user of the program should enter an integer n, n > 0, specifying the height of the
Christmas tree (number of lines).
Example: for n = 5 the result should look like this:
2
* -> 4 spaces, 1 asterisk
*** -> 3 spaces, 3 asterisks
***** -> 2 spaces, 5 asterisks
******* -> 1 spaces, 7 asterisks
********* -> 0 spaces, 9 asterisks
Exercise 6
• Write a Box class that will store elements of any type, e.g. int, String
• Based on the Box class, create an object and a new reference to the same object,
present modifications on any reference
• Write a mechanism that will create a new independent copy of the Box object created
in the subsection above
Exercise 7
Write a program that reads a string of characters from the user and then displays
information about how many times its last character is repeated in that string. For example,
for the string "Abracadabra", the program should display 4, because the last character is
the letter "a", which appears in the given string 4 times in total.
Exercise 8
Using StringBuilder, write a program that reads a string from the user, then creates a string
that is the inverse of the given string, and displays it on the screen. For example, for the
string "Cat" the result should be the string "toK".
Exercise 9
Check if the date is in the correct day-month-year format, e.g. 01-01-2020 including
validation of the days of the month and the value of months
Exercise 10
Write a program that displays the given date in the format:
• hour::minute day of the month-year
• hh:mm:ss a day of the month year
Java - fundamentals: programming
Basic exercises
Exercise 1
Given a list of numbers and a number k, return whether any two numbers in the list add up
to k.
For example, given [10, 15, 3, 7] and k of 17, returns true because 10 + 7 is 17.
3
Exercise 2
Find the longest single-character substring in the given string -> AABCDDBBBEA -> “B:3”
<br>
Exercise 3
Write an encryption and decryption mechanism using the Caesar cipher (https://
en.wikipedia.org/wiki/Caesar_cipher)
Plaintext: BE BRAVE, PROTECT YOUR REGIMENT AND SIX FLAGS
Ciphertext: EH EUDYH, SURWHFW BRXU UHJLPHQW DQG VLA IODJV
Exercise 4
The task is to write a Java program in which the user will be given K attempts to guess a
randomly generated number. Below are the rules of the game:
• If the guessed number is greater than the real number, the program will reply with a
message that the guessed number is higher than the real one.
• If the guessed number is less than the real number, the program will reply with a
message that the guessed number is lower than the real one.
• If the guessed number is equal to the real number, or if K attempts are exhausted, the
program will exit with the appropriate message.
Exercise 5
Write a program to invert the contents of an array, e.g.
[1, 10, 4, 5, 2, 12] -> [12, 2, 5, 4, 10, 1]
[1, 2, 5, 3, 10] -> [10, 3, 5, 2, 1]
Exercise 6
Write a program to remove duplicates from an array and return a new array, e.g.
Input array: [20, 20, 30, 40, 50, 50, 50],
Output array: [20, 30, 40, 50]
Exercise 7
Write a program that calculates the difference between the largest and smallest values in
an array.
Input array: [20, 20, 30, 40, 50, 50, 50],
Result: 50 - 20 = 30
Exercise 8
Write your own exponentiation function without using multiplication and division operators.
4
Advanced exercises
Exercises 1
Given a two-dimensional binary matrix filled with 0s and 1s, find the largest rectangle
containing all 1s and return its area.
Bonus if you can solve it in O(n^2) or less.
Example::
A:[111011100]
Output : 4
Since the rectangle with the maximum area is formed by the 2x2 rectangle formed by
(0,1), (0,2), (1,1) and (1,2)
Exercise 2
Given an array of size n, find the majority element. A majority element is an element that
appears more than floor(n/2) times. You can assume that the array is not empty and the
majority element always exists in the array.
Example :
Input : [2, 1, 2] Return : 2 which occurs 2 times and is greater than 3/2.
Exercise 3
A sorted array of integers has been rotated an unknown number of times.
Given such an array, find the index of an element in the array in less than linear time. If the
element does not exist in the array, return -1.
For example, given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 (index 8 in
the array).
Exercise 4
MegaCorp wants to reward its employees based on the number of lines of code they write.
They would like to give each employee as little positive amount as possible, consistent
with the constraint that if a programmer wrote more lines of code than his neighbor, he
should get more money.
Given a table representing the number of lines of code employees at MegaCorp have
written, determine how many each should receive.
For example, given [10, 40, 200, 1000, 60, 30], return [1, 2, 3, 4, 2, 1].
Exercise 5
Indicate a number 1 greater than the number represented by the digits in the array, e.g.
[1,3,2,4] -> 1324 + 1 -> [1,3,2,5]
5
Exercise 6
Based on the two given tables [-1,3,8,2,9,5] [4,1,2,10,5,20] find pairs of numbers whose
sum is equal to n=24
Exercise 7
Find the peak element in a two-dimensional array An element is the top element if it is
greater than or equal to its four neighbors, left, right, top, and bottom. For example, the
neighbors for A[i][j] are A[i-1][j], A[i+1][j], A[i][j-1], and A[i][j+1]. For edge elements, they are
considered a negative infinite value.
Input : 10 20 15 21 30 14 7 16 32 Output : 30
Input : 10 7 11 17 Output : 17
Exercise 8
Given a matrix of N rows and M columns, the task is to find the minimum absolute
difference between any two adjacent elements of an array of size N, which is formed by
taking one element from each row of the matrix. Notice that the element selected from row
1 becomes arr[0], the element fetched from row 2 becomes arr[1], and so on.
Input : N = 2, M = 2 m[2][2] = { 8, 2, 6, 8 } Output : 0.
Input : N = 3, M = 3 m[3][3] = { 1, 2, 3 4, 5, 6 7, 8, 9 } Output : 1.
Software testing - fundamentals
Basic exercises
Exercise 1
Implement a mechanism to find the first repeated character in a string:
BACDGBAF -> B
CERCRE -> C
ABCD -> none
Based on the prepared implementation, write unit tests to check the correctness of its
implementation
Advanced exercises
Exercise 1
Write a mechanism that parses the following string of characters into a Movie object
7
Exercises 4
Create an enum class Continent, which will contain information about the name of the
continent and its area (https://en.wikipedia.org/wiki/Continent).
Within the enum, declare all continents and static methods to:
• return the largest continent (use the values() method)
• return the smallest continent (use the values() method)
Exercises 5
Write a Pair class that accepts two generic types:
• first
• second
Implement the methods:
• first() -> which will return the value first
• second() -> which will return the second value
Exercise 6
Write a mechanism that converts a String list to an Integer list..
Exercise 7
Write a Car class that will contain the fields name and type of caru.
Create a queue of Cars.
• Add some cars to the queue.
• iterate through all available cars
• demonstrate how the peek(), pop() method works
Exercise 8
Based on the following list, write a mechanism that will only return unique elements:
List<Integer> values = new ArrayList<>();
values.add(10);
values.add(11);
values.add(10);
values.add(11);
values.add(12);
values.add(11);
Exercise 9
Using the Stream API, write a function that for the example below:
List<Movie> movies = Arrays.asList(….)
where Movie is a class
8
class Movie {
String title;
String type;
long duration;//min
LocalDate releaseDate
}
will return a list of movies where:
• the length of the video will be greater than 20 minutes
• the year of release will not be older than 2018
• horror is the type
Exercise 10
Schedule a task that executes once a second.
Advanced exercises
Exercise 1
Write a class that implements the lotto system.
The class should contain fields like:
• last draw (array of numbers)
• number of wins
• a method for making a random draw (generating 6 random numbers from the range
1-49)
• the method should accept a set of 6 numbers as input and check the number of hits, if
someone hits 6, we increase the win counter
Make sure you have adequate access to information about the last draw.
Exercise 2
Person class
• Implement the abstract class Person. The class should contain:
two String fields: name, address
parameterless constructor that sets the value of the name and address fields to an
empty String
constructor with two parameters: String name, String address
getter type methods responsible for returning the variable value: name, address
setter methods responsible for setting the values of the name, address fields
abstract method showDetails
Student class
9
• Implement the class Student. This class should extend the Person class. In addition, it
should contain:
three fields: type of study, year of study, cost of study
constructor with three parameters: type of study, year of study, cost of study
getter methods for declared fields
setter methods for declared fields
the showDetails method displaying detailed information about the student
Lecturer class
• Implement the Lecturer class. This class should extend the Person class. Additionally,
it should contain:
two fields: specialization and salary
constructor with two parameters: specialization, salary
getter methods for declared fields
setter methods for declared fields
the showDetails method displaying detailed information about the lecturer
Present the solution implemented above using an example.
Exercise 3
Write a mechanism that converts temperature in the following formats:
* C -> K
* K -> C
* F -> C
* C -> F
Prepare an appropriate interface to represent the conversion operation. Implement an
interface for each of the converting classes and demonstrate the polymorphism
functionality.
Exercise 4
Create a Car class that will contain fields like:
• type
• engine
• wheels
Create an Engine class containing information such as type, capacity, power.
Create a Wheels class containing information such as size, type.
Exercise 5
Write a generic method that will replace (swap) elements with the indicated positions in the
10
array.
The method should accept:
• an array of any type of element
• position of the first item
• position of the second item
i.e., e.g. if we have arrays of 10 elements, we give position 1: 4, position 2: 8, then
you should replace:
• the item at index 4 must be at index 8 and the item at index 8 must be at index 4
Exercise 6
Write a method whose purpose is to display the contents of any of the following lists:
- List< Integer > - List< Long > - List< Double > - List< Float >
Exercise 7
Write your own implementation of adding and reading data from LinkedList. Apply generic
types.
Exercise 8
Prepare a class with the following fields:
• title (String)
• director (String)
• release year (int)
Add two identical objects to lists and HashSet:
• first without implementing the hashCode method and the Equals method
• with an implementation of the equals method
• with the implementation of the hashCode method
Observe the difference.
For the list above, the number of duplicates should equal: 2
Exercise 9
Based on the class structure of students in a school, write a mechanism that will return all
students of a given school. Each school shall contain a list of school classes, and each
school class should contain a list of students.
Exercise 10
Write a thread class that calls the inc() method multiple times on a Counter object. The
object should be a shared global variable. Create several threads, start them all, and wait
for all threads to finish. Print the final counter value and check that it is correct.
11
Java - advanced: programming
Basic exercises
Exercise 1
Based on the structure of football leagues, perform the following tasks using the Stream
API:
• Return all coaches based on all available leagues.
• Based on all available leagues, return the names of all coaches who are at least 50
years old.
• Based on all available leagues, return all coaches aged 30 or less.
• Return all coaches of German nationality based on all available leagues.
• Return all players based on all available leagues.
• Return all attacking players based on all available leagues.
• Based on all available leagues, return the names of all defenders under 30 years old.
• Based on all available leagues, return all football goalkeepers who are at least 50
years old.
• Return all clubs based on all available leagues.
• Based on all available leagues, return all clubs with at least 2 players.
• Based on all available leagues, return all clubs with a coach of Brazilian nationality.
• Based on all available leagues, return all Polish clubs.
• Based on all available leagues, return all clubs over 50 years old.
• Based on all available leagues, return all those that contain at least 3 teams in their
competitions.
• Based on all available leagues, return all first division clubs (level == 1).
Advanced exercises
Exercise 1
Based on the structure set in the file, create:
• the City model class, which will contain the fields contained in the CSV file in
accordance with the documentation (https://simplemaps.com/data/world-cities ->
basic) and prepare a mechanism for extracting the following information (using
Stream API):
Display all City objects downloaded from a CSV file.
Display all city names.
Display all geographic coordinates of each city.
Display all country names of cities in the world.
12
Display only unique countries of world cities.
Display ew all administrative regions.
View all the population of cities in the world.
Display the names of all cities with a population greater than 100,000.
Display all countries that have cities included in the file with a population greater
than 200,000.
Display all countries smaller than 20,000.
Display all cities starting with the letter A.
Display all cities that do not contain the letter Z in their name.
Display all cities belonging to Pakistan.
Display all cities belonging to Russia.
Display a pair of values: country, number of cities from the file.
Display all country capitals.
View all administrative capitals.
Display all cities that are within 10000km of Warsaw (https://en.wikipedia.org/wiki/
Great-circle_distance).
Design patterns and good practices
Basic exercises
Exercise 1
Write an application that implements a design pattern that will have the functionality to
change the volume system of a mobile phone.
The phone can be in the following modes::
• sound
• vibration
• silent
• write a mechanism that allows you to change the volume level within the sound state
• write unit tests to check the correctness of the implemented implementation
Exercise 2
Write an application using a design pattern that will issue a command using the example of
a TV.
TV should be able to:
• turn on • change the source (HDMI1, HDMI2)
• turn off • change the volume level
13
Advanced exercises
Exercises 1
Write an application that simulates an ATM withdrawal. Use the Chain of Responsibility for
this.
A single link in this case should be the payment of a specific denomination, e.g. as below:
The algorithm should work in such a way that at the beginning it checks whether we are
able to withdraw how much requested money with the highest denomination, and possibly
look for the missing change for a lower denomination. Consider the following example:
Exercise 2
Write a mechanism that implements a tree structure representing the structure of system
files.
Use the composite design pattern for this:
The single element (leaf) will be the executable in this case.
A composite will be a directory that can contain multiple files and other directories.
Implement a mechanism that will allow you to display all system files (executable files,
directories).
• Abstract class: SystemFile
Single object: ExecutableFile - we cannot add other SystemFile
objects to it Composite: Directory - may contain other SystemFiles
Root (D)
• test.txt (E)
• music (D) - sample.mp3
• photos (D) - p1.png (E), p2.png (E)
14
SQL databases
Basic exercises
Exercise 1
Completed the tables with data
Select all data from all cinemas plus data from the movie that is being shown in the cinema
(if it is showing)
Select all data from all movies and, if the movie is shown in a theater, show theater data
View movie and theater data for theaters that are playing movies (currently active)
Advanced exercises
Exercise 1
Create the following tables and the relationship between them, and then prepare queries
to solve the following problems:
• Select the names of all products in the store.
• Select the names and prices of all products in the store.
• Select the name of products with a price less than or equal to $200.
• Select all products from $60 to $120.
• Select name and price in cents (i.e. price must be multiplied by 100).
• Calculate the average price of all products.
• Calculate the average price of all products with a manufacturer code of 2.
• Calculate the number of products with a price greater than or equal to $180.
• Select the name and price of all products with a price greater than or equal to $180
and sort first by price (in descending order), then by name (in ascending order).
• Select all data from products, including all manufacturer data for each product.
• Select the product name, price and manufacturer name of all products.
• Select the average price of each manufacturer's products, showing only the
manufacturer's code.
• Select the average price of each manufacturer's products by showing the
manufacturer's name.
• Select the names of manufacturers whose products have an average price greater
than or equal to $150.
• Select the name and price of the cheapest product.
• Select the name of each manufacturer with an average price of more than $145 and
containing at least 2 different products.
• Update the name of product 8 to "Laser Printer".
15
• Apply a 10% discount on all products.
• Apply 10% discount on all items priced greater than or equal to $120.
JDBC and Hibernate
Basic exercises
Exercise 1
Write a movie ticket booking system for this purpose:
• create a command (using the command design pattern) responsible for creating the
Reservation table (id, price, seat number, row, showtime, repertoire name)
• create a command that adds records to the table
• create a command that displays all records from the table
Exercise 2
Create tables according to the following diagram with consideration:
• of inheritance of common features
• use the composition to isolate the characteristics common to humans into a separate
class
• nomenclature consistent with the diagram
Advanced exercises
Exercise 1
Create a Teacher table that uses the Person class on an aggregated basis. As part of the
table, present the operation of the following operations:
• introducing new teachers
• update of existing ones
• deleting
• teacher list download
Exercise 2
Implement a basic database CRUD for Movie records. Implement the implementation
based on JPA.
HTML, CSS, Javascript
Basic exercises
Exercise 1
Creating a website displaying a list of movies with basic information: title, year of
production, genre, summary and information whether the movie has been watched by the
user. The task is to verify knowledge of HTML, CSS and JavaScript.
16
• Install Node.js version 8.9.3 LTS and the http-server package globally.
• Create a project directory, under the control of git, and files in it:
data.js
index.html
script.js
style.css
• In the data.js file, place the content by assigning it to the global variable
"moviesData".
• In the index.html file, create a document that complies with the HTML5 standard, and
then:
import other files (data.js, script.js, style.css);
• In the script.js file, put the code compliant with the ES6 standard which:
It will count all and watched movies from the "moviesData" array, and display this
information in the "moviesCounterSeen" and "moviesCounterAll" elements.
It will create a new "moviesList" for each element of the "moviesData" array. The
elements of the "moviesList" list should contain all basic information about the
movie and a graphic symbol representing information whether the movie has been
watched or not.
It will handle clicking on the graphic symbol:
It will toggle the "seen" attribute value in "moviesData" for the given movie
It will update the symbol in the list
It will update the video watched counter
Advanced exercises
Exercise 1
Modification of the application from the first task with the ability to add videos. The task is
to verify the ability to modularize JavaScript applications and code refactoring.
• Delete the data.js file.
• Rename the script.js file to index.js.
• Create a file movies-counter.js with the definition of the setCounterOfTo function.
The method should:
• Take two arguments, an element and the value that should be placed in it.
• Be used to set counter values throughout the application.
• Be exported by default.
• Create a file movies-storage.js with the MoviesStorage class definition. The class
should be exported by default.
17
• MoviesStorage:
When instantiated, it checks whether the browser's Local Storage contains
the movies attribute and whether it is an array. If not, an array with default movies
should be assigned to the variable (copy data from data.js).
Contains implementations of functions with the following signatures: get() - returns a list of
all videos get(id) - returns the movie object with the given id set(data) - adds a new movie
to the movie table set(id, data) - updates the movie with the given id with the
data remove(id) - removes the video with the given id
It is responsible for manipulating movies, and since the moviesData variable no longer
exists, all places in your application should be refactored accordingly.
• Create add.html and add.js files.
• Ucreate a video upload form, it should be in the div#formContainer element
• Remember the video counter.
• In add.js put the logic that should:
• Don't allow a movie to be uploaded without a title, year or genre.
• Do not allow video to be added if the year is not a 4 digit number.
• Don't allow a movie to be added if a movie with the title already exists.
• Clear the form after adding a video.
• If there is a problem with adding a video, an error message explaining the reason
should be displayed.
• Add links to each other in the index.html and add.html pages. The role of a front-end
developer is often to fill the gap left by a missing graphic designer or UX in the team.
Especially in smaller projects there is no dedicated person. So style the form to make
it look good and be useful.
Frontend technologies
Basic exercises
Exercise 1
Create an application based on the assumptions of HTML, CSS, Javascript tasks based
on the Angular framework. The created application should be divided into components
(e.g. form, counter or list of movies).
Advanced exercises
Exercise 1
Create a TODO list app that:
• Adds
18
• Removes
• Edits
• Displays TODO elements
Take care of the proper deployment of the application on the server.
Spring
Basic exercises
Exercise 1
• Write an application that allows you to perform basic CRUD for the VOD platform.
There should be an option within the app:
adding series to the vod platform
returning all available series within the platform
delete selected series
update of the selected series
returning selected series by id
searching for a series by: name, season number, number of episodes, year of
release
if you try to delete, download or update a non-existent series, return error code 400
and the following form: No show with #id found!
Perform operations based on any data structure that will store data locally in memory.
The series should have a:
id
name
description
season number
release date
number of episodes
• Ensure proper validation with Bean Validation:
the name should be between 4 and 30 characters
description should not be empty
season number cannot be negative
year of issue cannot be older than 1900
the number of episodes cannot be negative
• Create a new VodRepository class whose purpose is to return a simple API that
returns a list of objects.
19
At VodController level, inject VodRepository
via the constructor and replace the current calls to the list with calls to the repository
object
• Create a VodRepository extending JpaRepository. For this purpose:
you must add the applicable annotation to the Series module
you should replace the current VodController action with a new database one
let the getSeries method use the findAll method directly
• Create a business logic layer in accordance with the assumptions of the three-tier
architecture.
Advanced exercises
Exercise 1
Write two microservices:
• config microservice -> responsible for providing the api key to the openweathermap
website
• weather micorservce -> responsible for returning the current temperature for any city
based on openweatherapi
The weather microservice should be able to query the micorservice config to fetch the api
key.
https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
Exercise 2
Using Spring Security and JSON Web Token, implement a mechanism that performs
authorization and authentication of users based on tokens.
Software testing - advanced
Basic exercises
Exercise 1
Write a console application implementing a hotel reservation system
The booking entity should contain:
• id
• a name
• a hotel name
• a number of people
• a room standard (Normal, Exclusive)
• a price
• booking start date
20
• booking end date
Write ReservationService along with the method:
• which will return a result if it exists, and NoSuchElementException if there is no
element with the given id
• which will return the result with the given name if the objects exist, otherwise it will
return an empty list
• which will add the reservation
• which will delete the reservation
• which will update the reservation
Write tests for the implemented methods. Use parameterized tests.
Advanced exercises
Exercise 1
• Write a REST API implementing a hotel reservation system
The booking entity should contain:
- id
- a name
- a hotel name
- a number of people
- a room standard (Normal, Exclusive)
- a price
- booking start date
- booking end date
The repository should provide:
- searching for all available reservations by name
- searching for all available reservations by standard and price
- checking if there are any reservations on a given day
• Write repository tests that test the following scenarios:
search for all bookings
adding a reservation
searching for all available reservations by name
searching for all available reservations by standard and price
checking if there are any reservations on a given day
• Write unit tests for service classes including parameterized tests.
• Write unit tests for the controller classes.
• Write integration tests to verify the correctness of the implemented API.
21