Professional Documents
Culture Documents
0.Warm-up
a. Define an empty class named Time. Then create 2 separate instance of it. Check if they
are equal (with “==” operator). Question: why does == return that result for this case?..
b. Add 3 fields to the Time class: hours, minutes, seconds, all of type int. Create an
instance of class Time and set all it’s fields to some values ( like: 1:10:30).
c. Create a static method (outside of Time class) with signature “static String
convertToString(Time)” which receives as input parameter an instance of Time and
returns the values of its fields as a single String value in format
“<hour>:<minutes>:<seconds>”, and test it with previously created Time instance.
d. Move the method convertToString inside the Time class, and change it: rename it as
“toString”, make it non-static, public and remove its unnecessary input parameter (as it
should use the fields of current instance). It should now have the signature “public
String toString()”, but return similar value like the old method.
Question1: could this method have remained of type static? (why yes/no?)
Question2: if you have a variable “Time t1= new Time();”, what happens when you call
“System.out.println(t.toString());” ? What about “System.out.println(t);” ? How do you
explain that?...
e. Add one constructor to Time class, which should receive 3 parameters and use them to
initialize the 3 fields. Test that it works ok (create some instances, call .print() on them)
f. Add a validation to your constructor so it will ignore any negative values (will not set the
fields for those parameters, leaving them set to default 0 value). Test your validation.
g. Add a 2nd constructor to Time class, which has no parameters and will set the field
values to 23:59:59. Test it.
h. Modify one of your constructors (question: which one?) so that to avoid repeating code
between them - you should use “this(...)” in one of them to call the other (with the right
values for that case).
i. Change your code so that the fields of Time instances cannot be changed after they
were build (making Time instances “immutable”) - make the fields “private” (and possibly
also “final”), and add just getters (but no setters) for them. Check that you can still read
the fields (like hours) from outside the Time class, by using the getters.
j. Add a method on Time class with signature “int secondsSinceMidnight()” which will
compute and return the number of seconds since start of current day (0:0:0) up to the
current value of Time instance. Test this method for a few Time instances.
k. Add to your Time class a static field “int createInstancesCounter”, and change your
code (constructors) so each time a new instance is created, this counter is incremented.
Create then 3 new instances of Time class, and print the value of this counter after each
instance creation.
l. Add a new method to Time clase, “int secondsSince(Time)” which receives another
Time instance as parameter, and returns the number of seconds since that time to
current instance’s time (value may be negative or positive, depending on their order).
Hint: try to use secondsSinceMidnight() to avoid repeating code.
m. Optional: draw an UML diagram for your Time class (on paper);
(UML info: https://medium.com/@smagid_allThings/uml-class-diagrams-tutorial-step-by-step-520fd83b300b)
1.Person
Create a class Person, which will model some properties of a real person (hint: may use the
similar examples from presentation)
Testing: test all the code of your Person class: run some manual tests (in main() method of
class Person or some other separate class), checking things like: creating instances of Person,
calling methods to compare their age, change hair color, etc…). Also run all provided JUnit tests
(from PersonTests class) and make sure they all pass.
2. Room of persons
Create a class Room, modeling a room in which multiple persons can be present at once:
a. the room has a field named capacity (showing the maximum number of person which
can be in the room at the same time)
- this property is mandatory and should be always required when a new Room
instance is built (hint: need to handle it in a constructor)
- once a room is built, the capacity cannot be changed, but it should be readable
from outside (hint: need a getter, but no setter)
b. the room should be able to store multiple Person objects (up to its max capacity)
- hint: you’ll need a field holding an array of Person objects (initialized also in
constructor, based on the given room capacity)
- void enter(Person person) -> should add the given person to the room, if there
is still space left (current number of persons < max capacity), or else print an
error message and ignore this person
- hint: you may need to have a separate field to count the actual number of
persons in the room (personsCount, which is different/lower than max
room capacity)
- optional: add a validation so we don’t accept in the room 2 persons with
the exact same name (in that case may just print an error message and
ignore the person with duplicate name)
- String[] getNames(String hairColor) -> returns an array with the names of all
persons in room which have the hairColor property equal to the given color
- void exit(String personName) -> search and remove from room the person(s)
which have the specified name
- hint: once a person is found and should be removed, you need to update
the array so all persons coming after it should be moved one position
lower (towards the beginning of the array); also, the current persons
counter needs to be updated after this
- hint: you may want to define a helper method int indexOf(String
personName) which searches for and returns the index of the person in
room or -1 if not found; this would have some code very similar to one in
isPresent() method, so you should avoid duplication and make isPresent()
use this helper method; you can then easily use same helper method for
this exit() method
Testing: test all your code, by running some manual tests, and also all provided JUnit tests
(from class RoomTests). You may also try to any any new JUnit tests, if you have ideas on
extra things to check.
a. Add some fields to store the values for 2 coordinates (x,y of type double).
Question1: should you also make them ‘private’? What about ‘final’? (yes/no? why?)
Question2: should you add getters/setter from them from the start? (are they needed for
this case, considering all the requirements below?)
b. Add one constructor for easily building new points with given x,y values;
Question: before you add this constructor, can you build instances like this: “new
Point()” (with no params) ? what values will the x,y fields have in this case? will that
exact syntax (no params) work after you add your constructor? why yes/no?..
d. Add some static methods, which we can then use to check if a list of any three Point
instances can be the corners of a regular or a right-angled triangle:
- static boolean canFormTriangle(Point p1, Point p2, Point p3)
- static boolean canFormRightAngledTriangle(Point p1, Point p2, Point p3)
Hint: to implement these, you could first compute the 3 distances between each
of the pairs of 2 points (this being the sides of our possible triangle), then
remember what you did for exercises from week 1.
Testing: run some manual tests (in the main() method of some class), creating instances,
calling methods and printing results, etc. Then also all provides JUnits tests (from PointTests)
4. Intercoms
Think of the objects needed for an intercom system in an apartment building.
Design the objects, their properties and the messages needed to be exchanged between them
to allow someone to enter the building:
● either by scanning their key card
● or by ringing an apartment
5. ATM machine
Think of the objects needed for an ATM machine.
Design the objects, their properties and the messages needed for the operation of withdrawing
an amount of money from the ATM.
6. MyArrayList
Create a class MyArrayList, which can hold a collection of integer values:
- The object should hold the integers in a field of type array (int[] values)
- It should start with an empty collection (meaning an empty array - of size 0)
- It should have methods for:
- Add new values to its end: void add(int newValue)
- Delete last value from the end: void remove()
- Get elements count: int size()
- Get element at specific index: int get(int index)
- BONUS:
- a method to insert a new element at a specific position (by index, shifting
all elements after it to the next position)
- A method to remove an element from a specified position (by index)
Hints: for operations which modify the number of elements (add, remove), you will need to
“change the size” of the array field holding the values -> basically you will need to create a new
array of proper size, and then copy some of the values there…
Testing: write some tests (in the main() method of a separate class) testing all methods, for all
regular/tricky cases you can think of..
Optional:
7. MyLinkedList
A linked list is a linear collection of data elements, whose order is not given by their physical
placement in memory. Instead, each element points to the next.
In its most basic form, each node contains: data, and a reference (in other words, a link) to the
next node in the sequence.