SlideShare a Scribd company logo
www.sdacademy.pl
Exercises for Live Coding of “Java
Advanced - programming” module
www.sdacademy.pl
www.sdacademy.pl
Exercise 1.
Create a method that takes a string list as a parameter, then returns the list sorted alphabetically from Z to
A.
www.sdacademy.pl
www.sdacademy.pl
Exercise 2.
Create a method that takes a string list as a parameter, then returns that list sorted alphabetically from Z to
A case-insensitive.
www.sdacademy.pl
www.sdacademy.pl
Exercise 3.
Create a method that takes the map as a parameter, where the key is string and the value number, and then
prints each map element to the console in the format: Key: <k>, Value: <v>. There should be a comma at the
end of every line except the last, and a period at the last.
Example:
Key: Java, Value: 18,
Key: Python, Value: 1,
…
Key: PHP, Value: 0.
www.sdacademy.pl
www.sdacademy.pl
Exercise 4.
Create a Storage class that will have a private Map field, a public constructor, and methods:
addToStorage(String key, String value) → adding elements to the storage
printValues(String key) → displaying all elements under a given key
findValues(String value) → displaying all keys that have a given value
The Storage class should allow you to store multiple values under one key.
www.sdacademy.pl
www.sdacademy.pl
Exercise 5.
Implement the SDAHashSet<E> class that will implement the HashSet<E> logic. It should support methods:
• add
• remove
• size
• contains
• clear
www.sdacademy.pl
www.sdacademy.pl
Exercise 6.
Create a method that accepts TreeMap and prints the first and last EntrySet in the console.
www.sdacademy.pl
www.sdacademy.pl
Exercise 7.
Create a class imitating a weapon magazine. The class should be able to define the size of the magazine
using the constructor. Implement the methods:
loadBullet(String bullet) → adding a cartridge to the magazine, does not allow loading more cartridges
than the capacity of the magazine
isLoaded() → returns information about whether the weapon is loaded (at least one cartridge) or not
shot() → each call shots one bullet (prints string value in console) - the last loaded cartridge - and
prepares the next one, loaded before the last one, if there are no more cartridges, it prints "empty
magazine" in the console
www.sdacademy.pl
www.sdacademy.pl
Exercise 8.
Implement the Validator interface, which will include a boolean validate(Parcel input) method in its
declaration. Create a Parcel class with the parameters:
• int xLength
• int yLength
• int zLength
• float weight
• boolean isExpress
The validator should verify that the sum of the dimensions (x, y, z) does not exceed 300, whether each size is
not less than 30, whether the weight does not exceed 30.0 for isExpress = false or 15.0 for isExpress = true. In
case of errors, it should inform the user about them.
www.sdacademy.pl
www.sdacademy.pl
Exercise 9.
Create a Point2D class with fields double x, double y, getters, setters and constructor. Then create a Circle
class that will have a constructor:
Circle(Point2D center, Point2D point)
The first parameter specifies the center of the circle, the second is any point on the circle. Based on these
points, the Circle class is to determine:
• circle radius when calling double getRadius() method
• circle circumference when calling double getPerimeter() method
• circle area when calling double getArea() method
• * (challenging) three points on the circle every 90 degrees from the point given when calling the
List<Point2D> getSlicePoints() method
www.sdacademy.pl
www.sdacademy.pl
Exercise 10.
Create a MoveDirection class with fields double x, double y as well as getters, setters and constructor.
Create a Movable interface with the move(MoveDirection moveDirection) method.
Implement the interface in the classes from the previous task (Point2D and Circle). When the
move(MoveDirection moveDirection) method is called, the objects are to change their position based on
the provided direction (MoveDirection).
Validate the offset by calling the other Circle methods.
www.sdacademy.pl
www.sdacademy.pl
Exercise 11.
Create a Resizable interface with the resize(double resizeFactor) method.
Implement the interface in the class from the previous task (Circle). When calling the resize(double
resizeFactor) method, the circle should change its size by a given factor (1.5, 0.5, 10.0, etc.).
Validate the resizing by calling the other Circle methods.
www.sdacademy.pl
www.sdacademy.pl
Exercise 12.
Create a Manufacturer class that will contain fields: name, year of establishment, country. Include all
necessary methods and constructor parameters. Implement the hashCode() and equals() methods.
Create a Car class that will contain fields: name, model, price, year of manufacture, manufacturer list
(Manufacturer), and engine type (represented as the enum class, e.g. V12, V8, V6, S6, S4, S3). Include all
necessary methods and constructor parameters. Implement the hashcode() and equals() methods.
www.sdacademy.pl
www.sdacademy.pl
Exercise 13.
Create a CarService class that will contain a list of cars and implement the following methods:
1. adding cars to the list,
2. removing a car from the list,
3. returning a list of all cars,
4. returning cars with a V12 engine,
5. returning cars produced before 1999,
6. returning the most expensive car,
7. returning the cheapest car,
8. returning the car with at least 3 manufacturers,
9. returning a list of all cars sorted according to the passed parameter: ascending / descending,
10. checking if a specific car is on the list,
11. returning a list of cars manufactured by a specific manufacturer,
12. returning the list of cars manufactured by the manufacturer with the year of establishment <,>, <=,> =,
=,! = from the given.
www.sdacademy.pl
www.sdacademy.pl
Exercise 14.
Implement the following functionalities based on 100,000 element arrays with randomly selected values:
1. return a list of unique items,
2. return a list of elements that have been repeated at least once in the generated array,
3. return a list of the 25 most frequently recurring items.
Implement a method that deduplicates items in the list. If a duplicate is found, it replaces it with a new
random value that did not occur before. Check if the method worked correctly by calling method number 2.
www.sdacademy.pl
www.sdacademy.pl
Exercise 15.
Create a Car enum class with FERRARI, PORSCHE, MERCEDES, BMW, OPEL, FIAT, TOYOTA etc. constants.
Each vehicle has its own parameters, e.g. price, power, etc. Enum should contain boolean isPremium() and
boolean isRegular() methods. The isPremium() method should return the opposite result to the call of the
isRegular() method.
In addition, the boolean isFasterThan() method should be declared and implemented as part of the enum
class. This method should accept the Car type object and display information that the indicated vehicle is
faster or not than the vehicle provided in the argument. To do this, use the compareTo() method.
www.sdacademy.pl
www.sdacademy.pl
Exercise 16.
Create an enum Runner class with constants BEGINNER, INTERMEDIATE, ADVANCED. Enum should have two
parameters:
• minimum time of running a marathon in minutes
• maximum running time of the marathon in minutes
In addition, the Runner enum should contain the static getFitnessLevel() method, which takes any time
result of a marathon run, and as a result should return a specific Runner object based on the given time.
www.sdacademy.pl
www.sdacademy.pl
Exercise 17.
Create a ConversionType enum class with the constants METERS_TO_YARDS, YARDS_TO_METERS,
CENTIMETERS_TO_ICHES, INCHES_TO_CENTIMETERS, KILOMETERS_TO_MILES, MILES_TO_KILOMETERS.
Enum should have a Converter type parameter used to perform calculations for a given type.
Then create a MeasurementConverter class that will have the convert(int value, ConvertionType
conversionType) method and based on the value and type of conversion, used the Converter of the given
type and returned the result.
www.sdacademy.pl
www.sdacademy.pl
Exercise 18.
Create a Computer class with fields defining computer features: processor, ram, graphics card, company
and model. Implement setters, getters, constructor with all fields, toString(), equals() and hashcode()
methods.
Instantiate several objects and check how the methods work.
www.sdacademy.pl
www.sdacademy.pl
Exercise 19.
Create a Laptop class extending the Computer class from the previous task. The Laptop class should
additionally contain the battery parameter.
Implement additional getters, setters, constructor and overwrite the toString(), equals() and hashcode()
methods accordingly.
Use a reference to parent class methods.
www.sdacademy.pl
www.sdacademy.pl
Exercise 20.
Create an abstract Shape class with the abstract methods calculatePerimeter() for calculating the
perimeter and calculateArea() for calculating the area.
Create Rectangle, Triangle, Hexagon classes, extending the Shape class, and implementing abstract
methods accordingly. Verify the solution correctness.
www.sdacademy.pl
www.sdacademy.pl
Exercise 21.
Create an abstract 3DShape class that extends the Shape class from the previous task. Add an additional
method calculateVolume().
Create Cone and Qube classes by extending the 3DShape class, properly implementing abstract methods.
Verify the solution correctness.
www.sdacademy.pl
www.sdacademy.pl
Exercise 22.
Create a Fillable interface with the fill() method. Implement the method in the 3DShape class from the
previous task or separately in the Cone and Qube classes.
The fill() method should take a parameter, e.g. int, and check whether after the action of filling the figure:
• will pour too much water into the figure and overflow,
• fill the figure with water to the brim,
• not pouring enough water.
For each situation, it should write the status information in the console. Use the calculateVolume() method.
www.sdacademy.pl
www.sdacademy.pl
Exercise 23.
Create a Zoo class that will have a collection of animals and will allow you to receive statistics about your
animals:
int getNumberOfAllAnimals() → returns the number of all animals
Map <String, Integer> getAnimalsCount() → returns the number of animals of each species
Map <String, Integer> getAnimalsCountSorted() → returns the number of animals of each species
sorted based on the number of animals of a given species, where the first element is always the species with
the largest number of animals
void addAnimals(String, int) → adds n animals of a given species
www.sdacademy.pl
www.sdacademy.pl
Exercise 24.
Create a Basket class that imitates a basket and stores the current number of items in the basket. Add the
addToBasket() method, which adds the element to the basket (increasing the current state by 1) and the
removeFromBasket() method, which removes the element from the basket (reducing the current state by
1).
The basket can store from 0 to 10 items. When a user wants to remove an element at 0 items state or add an
element at 10 items state, throw the appropriate runtime exception (BasketFullException or
BasketEmptyException).
www.sdacademy.pl
www.sdacademy.pl
Exercise 25.
Change the BasketFullException and BasketEmptyException exceptions from runtime exception type to
checked exception type.
Handle them.
www.sdacademy.pl
www.sdacademy.pl
Exercise 26a.
Using functional programming mechanisms based on the given structure, display:
1. a list of all Models,
2. a list of all cars,
3. list of all manufacturer names,
4. list of all manufacturers' establishment years,
5. list of all model names,
6. list of all years of starting production of models,
7. list of all car names,
8. list of all car descriptions,
9. only models with an even year of production start,
10. only cars from manufacturers with an even year of foundation,
11. only cars with an even year of starting production of the model and an odd year of establishing the
manufacturer,
12. only CABRIO cars with an odd year of starting model production and an even year of establishing the
manufacturer,
13. only cars of the SEDAN type from a model newer than 2019 and the manufacturer's founding year less than
1919
www.sdacademy.pl
www.sdacademy.pl
Exercise 26b.
enum CarType {
COUPE, CABRIO, SEDAN, HATCHBACK
}
class Car {
public String name;
public String description;
public CarType carType;
public Car(String name, String description, CarType carType) {
this.name = name;
this.description = description;
this.carType = carType;
}}
class Model {
public String name;
public int productionStartYear;
List<Car> cars;
public Model(String name, int productionStartYear, List<Car> cars) {
this.name = name;
this.productionStartYear = productionStartYear;
this.cars = cars;
} }
class Manufacturer {
public String name;
public int yearOfCreation;
List<Model> models;
public Manufacturer(String name, int yearOfCreation, List<Model> models) {
this.name = name;
this.yearOfCreation = yearOfCreation;
this.models = models;
} }
www.sdacademy.pl
www.sdacademy.pl
Exercise 27.
Design the Joiner<T> class, which in the constructor will take a separator (string) and have a join() method
that allows you to specify any number of T-type objects. This method will return a string joining all passed
elements by calling their toString() method and separating them with a separator.
eg. for Integers and separator "-" it should return: 1-2-3-4 ...
www.sdacademy.pl
www.sdacademy.pl
Exercise 28.
Extend the ArrayList<E> class by implementing the getEveryNthElement() method. This method returns a list
of elements and takes two parameters: the element index from which it starts and a number specifying which
element to choose.
For the list: [a, b, c, d, e, f, g] and parameters: startIndex = 2 and skip = 3 it should return [c, g]
www.sdacademy.pl
www.sdacademy.pl
Exercise 29.
Implement the generic partOf method, which will return % of items satisfying the condition based on any
type of array and the function indicated as parameters.
www.sdacademy.pl
www.sdacademy.pl
Exercise 30.
Write a program that will read any file and save it in the same folder. The content and title of the new file should
be reversed (from the back).
www.sdacademy.pl
www.sdacademy.pl
Exercise 31.
Write a program that will count the occurrences of each word in a text file and then save the results in the form
of a table in a new file.
www.sdacademy.pl
www.sdacademy.pl
Exercise 32.
Write a program that will be able to save the list of items (e.g. cars) to a file in such a format that it can also read
this file and create a new list of items and write it to the console.
www.sdacademy.pl
www.sdacademy.pl
Exercise 33.
Write a program that will display all photos (.png, .jpg) in a given directory and subdirectories.
www.sdacademy.pl
www.sdacademy.pl
Exercise 34.
Create a class that extends the Thread class, e.g. MyThread, overload the run() method so it displays the
thread name in the console by reading it from the static method of the current thread:
Thread.currentThread().getName()
Create a class with the public static void main (String [] args) method. Inside the main method create a
type variable of our class created above, e.g. MyThread and initialize the class. Perform the start() method
on the variable.
www.sdacademy.pl
www.sdacademy.pl
Exercise 35.
Create a class implementing Runnable, e.g. MyRunnable. Implement the run() method, which will display
the name of the current thread in the same way as in the previous exercise.
Create a class with the public static void main (String [] args) method. Inside the main method, create a
class type variable created above, e.g. MyRunnable, and initialize the class.
Create a Thread variable and initialize it by passing the Runnable interface implementation as the
constructor parameter. Perform the start() method on a Thread type variable.
www.sdacademy.pl
www.sdacademy.pl
Exercise 36.
Create a ThreadPlaygroundRunnable class that implements the Runnable interface having a name field of
type String with a public constructor for that field. The class should implement the run() method from the
Runnable interface. This method should contain a loop iterating up to 10 and print the name of the current
thread using Thread.currentThread().getName(), the name given in the constructor and the current iteration
number.
Create a class that has two private static Thread fields and a standard public static void main (String []
args) method. Then initialize the Thread fields using the constructor that accepts the Runnable object and
pass ThreadPlaygroundRunnable creating it using the constructor, each time giving a different name.
On each thread (Thread) use the start() method.
www.sdacademy.pl
www.sdacademy.pl
Exercise 37.
Create a class containing the standard static method main and a variable of type Executor and using the
factory method newFixedThreadPool of the Executors class to create a pool of 2 executors.
In iteration, add 10 ThreadPlaygroundRunnable objects from the previous task to the executor. Use any
string and current iteration number as the name.
www.sdacademy.pl
www.sdacademy.pl
Exercise 38.
Write an application that will simulate a coffee making machine. In the event that any coffee brewing service
finds an empty water tank, the thread should stop. When the water is refilled in the machine, the thread
should be excited.
www.sdacademy.pl
www.sdacademy.pl
Exercise 39.
Write a program that will solve the problem below.
There is a system that stores current results in competitions. Many screens read the current results, while
several sensors update these results. Write a program that allows continuous reading of data through many
screens (threads) and updating data by many sensors (threads).
To update results, the sensor must provide current results along with new ones. The system then verifies that
the sensor has current data and updates the data. If the system data has changed during this time, the sensor's
data update is rejected.
For simplicity, you can assume that the sensor is reading data, waiting a random amount of time, and
increasing the data by a random value.

More Related Content

Similar to exercises_for_live_coding_Java_advanced-2.pdf (20)

Assignment DIn Problem D1 we will use a file to contain the dat.pdf
Assignment DIn Problem D1 we will use a file to contain the dat.pdf
fasttrackscardecors
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
YASHU40
 
Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerat...
Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerat...
Raffi Khatchadourian
 
11. Java Objects and classes
11. Java Objects and classes
Intro C# Book
 
Java Foundations: Objects and Classes
Java Foundations: Objects and Classes
Svetlin Nakov
 
data types.pptx
data types.pptx
FatimaGraceApinan
 
(eBook PDF) Data Structures and Other Objects Using Java 4th Edition
(eBook PDF) Data Structures and Other Objects Using Java 4th Edition
adweguggu
 
Sl no.docx
Sl no.docx
Parikshithn5
 
Ibm enterprise it and asset management 7.1 java for customization exercise
Ibm enterprise it and asset management 7.1 java for customization exercise
Hal Izi
 
Lecture3.pdf
Lecture3.pdf
SakhilejasonMsibi
 
Lecture1 classes4
Lecture1 classes4
Noor Faezah Mohd Yatim
 
Modern Compiler Design
Modern Compiler Design
nextlib
 
CS2309 JAVA LAB MANUAL
CS2309 JAVA LAB MANUAL
Lavanya Arunachalam A
 
Objects First With Java A Practical Introduction Using Bluej 1st Edition Davi...
Objects First With Java A Practical Introduction Using Bluej 1st Edition Davi...
szirtmbondo
 
Data Structures 2004
Data Structures 2004
Sanjay Goel
 
Java How To Program Fourth Edition Harvey M. Deitel
Java How To Program Fourth Edition Harvey M. Deitel
timerokhobor
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Michael Redlich
 
Collections and generic class
Collections and generic class
ifis
 
Introduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design Principles
Michael Redlich
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Assignment DIn Problem D1 we will use a file to contain the dat.pdf
Assignment DIn Problem D1 we will use a file to contain the dat.pdf
fasttrackscardecors
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
YASHU40
 
Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerat...
Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerat...
Raffi Khatchadourian
 
11. Java Objects and classes
11. Java Objects and classes
Intro C# Book
 
Java Foundations: Objects and Classes
Java Foundations: Objects and Classes
Svetlin Nakov
 
(eBook PDF) Data Structures and Other Objects Using Java 4th Edition
(eBook PDF) Data Structures and Other Objects Using Java 4th Edition
adweguggu
 
Ibm enterprise it and asset management 7.1 java for customization exercise
Ibm enterprise it and asset management 7.1 java for customization exercise
Hal Izi
 
Modern Compiler Design
Modern Compiler Design
nextlib
 
Objects First With Java A Practical Introduction Using Bluej 1st Edition Davi...
Objects First With Java A Practical Introduction Using Bluej 1st Edition Davi...
szirtmbondo
 
Data Structures 2004
Data Structures 2004
Sanjay Goel
 
Java How To Program Fourth Edition Harvey M. Deitel
Java How To Program Fourth Edition Harvey M. Deitel
timerokhobor
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Michael Redlich
 
Collections and generic class
Collections and generic class
ifis
 
Introduction to Object Oriented Programming & Design Principles
Introduction to Object Oriented Programming & Design Principles
Michael Redlich
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 

Recently uploaded (20)

Caterpillar Cat 318D2 L Excavator (Prefix HAH) Service Repair Manual Instant ...
Caterpillar Cat 318D2 L Excavator (Prefix HAH) Service Repair Manual Instant ...
nu174136zhao
 
Hitachi Zaxis 500LC-3 Technical Repair Manual – Download Full PDF.pdf
Hitachi Zaxis 500LC-3 Technical Repair Manual – Download Full PDF.pdf
Service Repair Manual
 
Restore Porsche Performance How to Spot and Fix Fuel Injection System Failures
Restore Porsche Performance How to Spot and Fix Fuel Injection System Failures
Bay Diagnostic
 
++(full video,,!!)* Sophie Rain Spiderman Video Clip Sophie Rain Original Dow...
++(full video,,!!)* Sophie Rain Spiderman Video Clip Sophie Rain Original Dow...
Capcut Pro Crack
 
Volvo EC140B LC, EC140B LCM Excavator Parts Catalogue Manual (SN 10001 - 1500...
Volvo EC140B LC, EC140B LCM Excavator Parts Catalogue Manual (SN 10001 - 1500...
Service Repair Manual
 
April ANA org Chart Company Structure .pdf
April ANA org Chart Company Structure .pdf
joeleharder
 
Caterpillar Cat 304E Mini Hydraulic Excavator (Prefix TSR) Service Repair Man...
Caterpillar Cat 304E Mini Hydraulic Excavator (Prefix TSR) Service Repair Man...
jie2404li
 
最新版西班牙马德里自治大学毕业证(UAM毕业证书)原版定制
最新版西班牙马德里自治大学毕业证(UAM毕业证书)原版定制
Taqyea
 
XMLLec1.pptsfsfsafasfasdfasfdsadfdsfdf dfdsfds
XMLLec1.pptsfsfsafasfasdfasfdsadfdsfdf dfdsfds
Kamrankhan925215
 
Volvo Ec140blc LCM d4d Eae1 2124351640 2002 09.pdf
Volvo Ec140blc LCM d4d Eae1 2124351640 2002 09.pdf
Service Repair Manual
 
Caterpillar Cat D7R XR TRACK-TYPE TRACTOR (Prefix R7C) Service Repair Manual ...
Caterpillar Cat D7R XR TRACK-TYPE TRACTOR (Prefix R7C) Service Repair Manual ...
xiao35lai
 
Diagrams Volvo Ec140b Lc Complete Manual Pdf
Diagrams Volvo Ec140b Lc Complete Manual Pdf
Service Repair Manual
 
Caterpillar Cat 428D BACKHOE LOADER (Prefix BNS) Service Repair Manual Instan...
Caterpillar Cat 428D BACKHOE LOADER (Prefix BNS) Service Repair Manual Instan...
xue582shao
 
Daewoo doosan mega 250 v wheel loader maintenance manual.pdf
Daewoo doosan mega 250 v wheel loader maintenance manual.pdf
Service Repair Manual
 
HMC x USA pitch presentation - Rough Draft
HMC x USA pitch presentation - Rough Draft
Parnika Mundra
 
nvidia nvidia nvidia nvidia nvidia nvidia
nvidia nvidia nvidia nvidia nvidia nvidia
chouirefkawtar
 
John Deere E100 E180 CVT Repa Service Manual.pdf
John Deere E100 E180 CVT Repa Service Manual.pdf
Service Repair Manual
 
Daewoo doosan mega 250 v wheel loader operation and maintenance manual
Daewoo doosan mega 250 v wheel loader operation and maintenance manual
Service Repair Manual
 
New Holland L565 Lx565 Lx665 Skid Steer Service Repair Manual.pdf
New Holland L565 Lx565 Lx665 Skid Steer Service Repair Manual.pdf
Service Repair Manual
 
Hitachi Zaxis 520LCH-3 Service Manual PDF – Complete Technical Documentation.pdf
Hitachi Zaxis 520LCH-3 Service Manual PDF – Complete Technical Documentation.pdf
Service Repair Manual
 
Caterpillar Cat 318D2 L Excavator (Prefix HAH) Service Repair Manual Instant ...
Caterpillar Cat 318D2 L Excavator (Prefix HAH) Service Repair Manual Instant ...
nu174136zhao
 
Hitachi Zaxis 500LC-3 Technical Repair Manual – Download Full PDF.pdf
Hitachi Zaxis 500LC-3 Technical Repair Manual – Download Full PDF.pdf
Service Repair Manual
 
Restore Porsche Performance How to Spot and Fix Fuel Injection System Failures
Restore Porsche Performance How to Spot and Fix Fuel Injection System Failures
Bay Diagnostic
 
++(full video,,!!)* Sophie Rain Spiderman Video Clip Sophie Rain Original Dow...
++(full video,,!!)* Sophie Rain Spiderman Video Clip Sophie Rain Original Dow...
Capcut Pro Crack
 
Volvo EC140B LC, EC140B LCM Excavator Parts Catalogue Manual (SN 10001 - 1500...
Volvo EC140B LC, EC140B LCM Excavator Parts Catalogue Manual (SN 10001 - 1500...
Service Repair Manual
 
April ANA org Chart Company Structure .pdf
April ANA org Chart Company Structure .pdf
joeleharder
 
Caterpillar Cat 304E Mini Hydraulic Excavator (Prefix TSR) Service Repair Man...
Caterpillar Cat 304E Mini Hydraulic Excavator (Prefix TSR) Service Repair Man...
jie2404li
 
最新版西班牙马德里自治大学毕业证(UAM毕业证书)原版定制
最新版西班牙马德里自治大学毕业证(UAM毕业证书)原版定制
Taqyea
 
XMLLec1.pptsfsfsafasfasdfasfdsadfdsfdf dfdsfds
XMLLec1.pptsfsfsafasfasdfasfdsadfdsfdf dfdsfds
Kamrankhan925215
 
Volvo Ec140blc LCM d4d Eae1 2124351640 2002 09.pdf
Volvo Ec140blc LCM d4d Eae1 2124351640 2002 09.pdf
Service Repair Manual
 
Caterpillar Cat D7R XR TRACK-TYPE TRACTOR (Prefix R7C) Service Repair Manual ...
Caterpillar Cat D7R XR TRACK-TYPE TRACTOR (Prefix R7C) Service Repair Manual ...
xiao35lai
 
Diagrams Volvo Ec140b Lc Complete Manual Pdf
Diagrams Volvo Ec140b Lc Complete Manual Pdf
Service Repair Manual
 
Caterpillar Cat 428D BACKHOE LOADER (Prefix BNS) Service Repair Manual Instan...
Caterpillar Cat 428D BACKHOE LOADER (Prefix BNS) Service Repair Manual Instan...
xue582shao
 
Daewoo doosan mega 250 v wheel loader maintenance manual.pdf
Daewoo doosan mega 250 v wheel loader maintenance manual.pdf
Service Repair Manual
 
HMC x USA pitch presentation - Rough Draft
HMC x USA pitch presentation - Rough Draft
Parnika Mundra
 
nvidia nvidia nvidia nvidia nvidia nvidia
nvidia nvidia nvidia nvidia nvidia nvidia
chouirefkawtar
 
John Deere E100 E180 CVT Repa Service Manual.pdf
John Deere E100 E180 CVT Repa Service Manual.pdf
Service Repair Manual
 
Daewoo doosan mega 250 v wheel loader operation and maintenance manual
Daewoo doosan mega 250 v wheel loader operation and maintenance manual
Service Repair Manual
 
New Holland L565 Lx565 Lx665 Skid Steer Service Repair Manual.pdf
New Holland L565 Lx565 Lx665 Skid Steer Service Repair Manual.pdf
Service Repair Manual
 
Hitachi Zaxis 520LCH-3 Service Manual PDF – Complete Technical Documentation.pdf
Hitachi Zaxis 520LCH-3 Service Manual PDF – Complete Technical Documentation.pdf
Service Repair Manual
 
Ad

exercises_for_live_coding_Java_advanced-2.pdf

  • 1. www.sdacademy.pl Exercises for Live Coding of “Java Advanced - programming” module
  • 2. www.sdacademy.pl www.sdacademy.pl Exercise 1. Create a method that takes a string list as a parameter, then returns the list sorted alphabetically from Z to A.
  • 3. www.sdacademy.pl www.sdacademy.pl Exercise 2. Create a method that takes a string list as a parameter, then returns that list sorted alphabetically from Z to A case-insensitive.
  • 4. www.sdacademy.pl www.sdacademy.pl Exercise 3. Create a method that takes the map as a parameter, where the key is string and the value number, and then prints each map element to the console in the format: Key: <k>, Value: <v>. There should be a comma at the end of every line except the last, and a period at the last. Example: Key: Java, Value: 18, Key: Python, Value: 1, … Key: PHP, Value: 0.
  • 5. www.sdacademy.pl www.sdacademy.pl Exercise 4. Create a Storage class that will have a private Map field, a public constructor, and methods: addToStorage(String key, String value) → adding elements to the storage printValues(String key) → displaying all elements under a given key findValues(String value) → displaying all keys that have a given value The Storage class should allow you to store multiple values under one key.
  • 6. www.sdacademy.pl www.sdacademy.pl Exercise 5. Implement the SDAHashSet<E> class that will implement the HashSet<E> logic. It should support methods: • add • remove • size • contains • clear
  • 7. www.sdacademy.pl www.sdacademy.pl Exercise 6. Create a method that accepts TreeMap and prints the first and last EntrySet in the console.
  • 8. www.sdacademy.pl www.sdacademy.pl Exercise 7. Create a class imitating a weapon magazine. The class should be able to define the size of the magazine using the constructor. Implement the methods: loadBullet(String bullet) → adding a cartridge to the magazine, does not allow loading more cartridges than the capacity of the magazine isLoaded() → returns information about whether the weapon is loaded (at least one cartridge) or not shot() → each call shots one bullet (prints string value in console) - the last loaded cartridge - and prepares the next one, loaded before the last one, if there are no more cartridges, it prints "empty magazine" in the console
  • 9. www.sdacademy.pl www.sdacademy.pl Exercise 8. Implement the Validator interface, which will include a boolean validate(Parcel input) method in its declaration. Create a Parcel class with the parameters: • int xLength • int yLength • int zLength • float weight • boolean isExpress The validator should verify that the sum of the dimensions (x, y, z) does not exceed 300, whether each size is not less than 30, whether the weight does not exceed 30.0 for isExpress = false or 15.0 for isExpress = true. In case of errors, it should inform the user about them.
  • 10. www.sdacademy.pl www.sdacademy.pl Exercise 9. Create a Point2D class with fields double x, double y, getters, setters and constructor. Then create a Circle class that will have a constructor: Circle(Point2D center, Point2D point) The first parameter specifies the center of the circle, the second is any point on the circle. Based on these points, the Circle class is to determine: • circle radius when calling double getRadius() method • circle circumference when calling double getPerimeter() method • circle area when calling double getArea() method • * (challenging) three points on the circle every 90 degrees from the point given when calling the List<Point2D> getSlicePoints() method
  • 11. www.sdacademy.pl www.sdacademy.pl Exercise 10. Create a MoveDirection class with fields double x, double y as well as getters, setters and constructor. Create a Movable interface with the move(MoveDirection moveDirection) method. Implement the interface in the classes from the previous task (Point2D and Circle). When the move(MoveDirection moveDirection) method is called, the objects are to change their position based on the provided direction (MoveDirection). Validate the offset by calling the other Circle methods.
  • 12. www.sdacademy.pl www.sdacademy.pl Exercise 11. Create a Resizable interface with the resize(double resizeFactor) method. Implement the interface in the class from the previous task (Circle). When calling the resize(double resizeFactor) method, the circle should change its size by a given factor (1.5, 0.5, 10.0, etc.). Validate the resizing by calling the other Circle methods.
  • 13. www.sdacademy.pl www.sdacademy.pl Exercise 12. Create a Manufacturer class that will contain fields: name, year of establishment, country. Include all necessary methods and constructor parameters. Implement the hashCode() and equals() methods. Create a Car class that will contain fields: name, model, price, year of manufacture, manufacturer list (Manufacturer), and engine type (represented as the enum class, e.g. V12, V8, V6, S6, S4, S3). Include all necessary methods and constructor parameters. Implement the hashcode() and equals() methods.
  • 14. www.sdacademy.pl www.sdacademy.pl Exercise 13. Create a CarService class that will contain a list of cars and implement the following methods: 1. adding cars to the list, 2. removing a car from the list, 3. returning a list of all cars, 4. returning cars with a V12 engine, 5. returning cars produced before 1999, 6. returning the most expensive car, 7. returning the cheapest car, 8. returning the car with at least 3 manufacturers, 9. returning a list of all cars sorted according to the passed parameter: ascending / descending, 10. checking if a specific car is on the list, 11. returning a list of cars manufactured by a specific manufacturer, 12. returning the list of cars manufactured by the manufacturer with the year of establishment <,>, <=,> =, =,! = from the given.
  • 15. www.sdacademy.pl www.sdacademy.pl Exercise 14. Implement the following functionalities based on 100,000 element arrays with randomly selected values: 1. return a list of unique items, 2. return a list of elements that have been repeated at least once in the generated array, 3. return a list of the 25 most frequently recurring items. Implement a method that deduplicates items in the list. If a duplicate is found, it replaces it with a new random value that did not occur before. Check if the method worked correctly by calling method number 2.
  • 16. www.sdacademy.pl www.sdacademy.pl Exercise 15. Create a Car enum class with FERRARI, PORSCHE, MERCEDES, BMW, OPEL, FIAT, TOYOTA etc. constants. Each vehicle has its own parameters, e.g. price, power, etc. Enum should contain boolean isPremium() and boolean isRegular() methods. The isPremium() method should return the opposite result to the call of the isRegular() method. In addition, the boolean isFasterThan() method should be declared and implemented as part of the enum class. This method should accept the Car type object and display information that the indicated vehicle is faster or not than the vehicle provided in the argument. To do this, use the compareTo() method.
  • 17. www.sdacademy.pl www.sdacademy.pl Exercise 16. Create an enum Runner class with constants BEGINNER, INTERMEDIATE, ADVANCED. Enum should have two parameters: • minimum time of running a marathon in minutes • maximum running time of the marathon in minutes In addition, the Runner enum should contain the static getFitnessLevel() method, which takes any time result of a marathon run, and as a result should return a specific Runner object based on the given time.
  • 18. www.sdacademy.pl www.sdacademy.pl Exercise 17. Create a ConversionType enum class with the constants METERS_TO_YARDS, YARDS_TO_METERS, CENTIMETERS_TO_ICHES, INCHES_TO_CENTIMETERS, KILOMETERS_TO_MILES, MILES_TO_KILOMETERS. Enum should have a Converter type parameter used to perform calculations for a given type. Then create a MeasurementConverter class that will have the convert(int value, ConvertionType conversionType) method and based on the value and type of conversion, used the Converter of the given type and returned the result.
  • 19. www.sdacademy.pl www.sdacademy.pl Exercise 18. Create a Computer class with fields defining computer features: processor, ram, graphics card, company and model. Implement setters, getters, constructor with all fields, toString(), equals() and hashcode() methods. Instantiate several objects and check how the methods work.
  • 20. www.sdacademy.pl www.sdacademy.pl Exercise 19. Create a Laptop class extending the Computer class from the previous task. The Laptop class should additionally contain the battery parameter. Implement additional getters, setters, constructor and overwrite the toString(), equals() and hashcode() methods accordingly. Use a reference to parent class methods.
  • 21. www.sdacademy.pl www.sdacademy.pl Exercise 20. Create an abstract Shape class with the abstract methods calculatePerimeter() for calculating the perimeter and calculateArea() for calculating the area. Create Rectangle, Triangle, Hexagon classes, extending the Shape class, and implementing abstract methods accordingly. Verify the solution correctness.
  • 22. www.sdacademy.pl www.sdacademy.pl Exercise 21. Create an abstract 3DShape class that extends the Shape class from the previous task. Add an additional method calculateVolume(). Create Cone and Qube classes by extending the 3DShape class, properly implementing abstract methods. Verify the solution correctness.
  • 23. www.sdacademy.pl www.sdacademy.pl Exercise 22. Create a Fillable interface with the fill() method. Implement the method in the 3DShape class from the previous task or separately in the Cone and Qube classes. The fill() method should take a parameter, e.g. int, and check whether after the action of filling the figure: • will pour too much water into the figure and overflow, • fill the figure with water to the brim, • not pouring enough water. For each situation, it should write the status information in the console. Use the calculateVolume() method.
  • 24. www.sdacademy.pl www.sdacademy.pl Exercise 23. Create a Zoo class that will have a collection of animals and will allow you to receive statistics about your animals: int getNumberOfAllAnimals() → returns the number of all animals Map <String, Integer> getAnimalsCount() → returns the number of animals of each species Map <String, Integer> getAnimalsCountSorted() → returns the number of animals of each species sorted based on the number of animals of a given species, where the first element is always the species with the largest number of animals void addAnimals(String, int) → adds n animals of a given species
  • 25. www.sdacademy.pl www.sdacademy.pl Exercise 24. Create a Basket class that imitates a basket and stores the current number of items in the basket. Add the addToBasket() method, which adds the element to the basket (increasing the current state by 1) and the removeFromBasket() method, which removes the element from the basket (reducing the current state by 1). The basket can store from 0 to 10 items. When a user wants to remove an element at 0 items state or add an element at 10 items state, throw the appropriate runtime exception (BasketFullException or BasketEmptyException).
  • 26. www.sdacademy.pl www.sdacademy.pl Exercise 25. Change the BasketFullException and BasketEmptyException exceptions from runtime exception type to checked exception type. Handle them.
  • 27. www.sdacademy.pl www.sdacademy.pl Exercise 26a. Using functional programming mechanisms based on the given structure, display: 1. a list of all Models, 2. a list of all cars, 3. list of all manufacturer names, 4. list of all manufacturers' establishment years, 5. list of all model names, 6. list of all years of starting production of models, 7. list of all car names, 8. list of all car descriptions, 9. only models with an even year of production start, 10. only cars from manufacturers with an even year of foundation, 11. only cars with an even year of starting production of the model and an odd year of establishing the manufacturer, 12. only CABRIO cars with an odd year of starting model production and an even year of establishing the manufacturer, 13. only cars of the SEDAN type from a model newer than 2019 and the manufacturer's founding year less than 1919
  • 28. www.sdacademy.pl www.sdacademy.pl Exercise 26b. enum CarType { COUPE, CABRIO, SEDAN, HATCHBACK } class Car { public String name; public String description; public CarType carType; public Car(String name, String description, CarType carType) { this.name = name; this.description = description; this.carType = carType; }} class Model { public String name; public int productionStartYear; List<Car> cars; public Model(String name, int productionStartYear, List<Car> cars) { this.name = name; this.productionStartYear = productionStartYear; this.cars = cars; } } class Manufacturer { public String name; public int yearOfCreation; List<Model> models; public Manufacturer(String name, int yearOfCreation, List<Model> models) { this.name = name; this.yearOfCreation = yearOfCreation; this.models = models; } }
  • 29. www.sdacademy.pl www.sdacademy.pl Exercise 27. Design the Joiner<T> class, which in the constructor will take a separator (string) and have a join() method that allows you to specify any number of T-type objects. This method will return a string joining all passed elements by calling their toString() method and separating them with a separator. eg. for Integers and separator "-" it should return: 1-2-3-4 ...
  • 30. www.sdacademy.pl www.sdacademy.pl Exercise 28. Extend the ArrayList<E> class by implementing the getEveryNthElement() method. This method returns a list of elements and takes two parameters: the element index from which it starts and a number specifying which element to choose. For the list: [a, b, c, d, e, f, g] and parameters: startIndex = 2 and skip = 3 it should return [c, g]
  • 31. www.sdacademy.pl www.sdacademy.pl Exercise 29. Implement the generic partOf method, which will return % of items satisfying the condition based on any type of array and the function indicated as parameters.
  • 32. www.sdacademy.pl www.sdacademy.pl Exercise 30. Write a program that will read any file and save it in the same folder. The content and title of the new file should be reversed (from the back).
  • 33. www.sdacademy.pl www.sdacademy.pl Exercise 31. Write a program that will count the occurrences of each word in a text file and then save the results in the form of a table in a new file.
  • 34. www.sdacademy.pl www.sdacademy.pl Exercise 32. Write a program that will be able to save the list of items (e.g. cars) to a file in such a format that it can also read this file and create a new list of items and write it to the console.
  • 35. www.sdacademy.pl www.sdacademy.pl Exercise 33. Write a program that will display all photos (.png, .jpg) in a given directory and subdirectories.
  • 36. www.sdacademy.pl www.sdacademy.pl Exercise 34. Create a class that extends the Thread class, e.g. MyThread, overload the run() method so it displays the thread name in the console by reading it from the static method of the current thread: Thread.currentThread().getName() Create a class with the public static void main (String [] args) method. Inside the main method create a type variable of our class created above, e.g. MyThread and initialize the class. Perform the start() method on the variable.
  • 37. www.sdacademy.pl www.sdacademy.pl Exercise 35. Create a class implementing Runnable, e.g. MyRunnable. Implement the run() method, which will display the name of the current thread in the same way as in the previous exercise. Create a class with the public static void main (String [] args) method. Inside the main method, create a class type variable created above, e.g. MyRunnable, and initialize the class. Create a Thread variable and initialize it by passing the Runnable interface implementation as the constructor parameter. Perform the start() method on a Thread type variable.
  • 38. www.sdacademy.pl www.sdacademy.pl Exercise 36. Create a ThreadPlaygroundRunnable class that implements the Runnable interface having a name field of type String with a public constructor for that field. The class should implement the run() method from the Runnable interface. This method should contain a loop iterating up to 10 and print the name of the current thread using Thread.currentThread().getName(), the name given in the constructor and the current iteration number. Create a class that has two private static Thread fields and a standard public static void main (String [] args) method. Then initialize the Thread fields using the constructor that accepts the Runnable object and pass ThreadPlaygroundRunnable creating it using the constructor, each time giving a different name. On each thread (Thread) use the start() method.
  • 39. www.sdacademy.pl www.sdacademy.pl Exercise 37. Create a class containing the standard static method main and a variable of type Executor and using the factory method newFixedThreadPool of the Executors class to create a pool of 2 executors. In iteration, add 10 ThreadPlaygroundRunnable objects from the previous task to the executor. Use any string and current iteration number as the name.
  • 40. www.sdacademy.pl www.sdacademy.pl Exercise 38. Write an application that will simulate a coffee making machine. In the event that any coffee brewing service finds an empty water tank, the thread should stop. When the water is refilled in the machine, the thread should be excited.
  • 41. www.sdacademy.pl www.sdacademy.pl Exercise 39. Write a program that will solve the problem below. There is a system that stores current results in competitions. Many screens read the current results, while several sensors update these results. Write a program that allows continuous reading of data through many screens (threads) and updating data by many sensors (threads). To update results, the sensor must provide current results along with new ones. The system then verifies that the sensor has current data and updates the data. If the system data has changed during this time, the sensor's data update is rejected. For simplicity, you can assume that the sensor is reading data, waiting a random amount of time, and increasing the data by a random value.