Java Classes and Object Exercises
Java Classes and Object Exercises
1. Create a Complex class that allows working with complex numbers (real part and part
imaginary). It includes the following methods: constructors (default and parameterized), accessors,
mutators, addition, subtraction, multiplication, division, accumulation and print().
2. Create a Rational class that allows working with rational numbers (fractions). Include the
the following methods: constructors (default and parameterized), accessors, read(), sum, subtract,
multiplication, division, comparisons, copy() and print().
3. Create a Rectangle class that models rectangles using four points (the vertices).
It will have two constructors: one that creates a rectangle starting from its four vertices and another
Create a rectangle starting from the base and the height, so that its bottom left vertex is
at (0,0). The class will also include a method to calculate the surface area and another that moves the
rectangle in the plane.
4. Define a Line class with two attributes: _pointA and _pointB. They are two points.
those who pass the line in a two-dimensional space. The class will have the following methods:
Line()
Default constructor that creates a line with its two points as (0,0) and (0,0).
Line(Point, Point)
Constructor that receives as parameters two objects of the class Punto,
that are used to initialize the attributes.
moveRight(double)
Shift the line to the right the indicated distance.
moveLeft(double)
Move the line to the left by the specified distance.
moveUp(double)
Move the line upwards the indicated distance.
moveDown(double)
Move the line down the indicated distance.
Accidents and mutators.
Method that allows us to display the information of the line in the following way:
[pointA,pointB]. For example: [(0.0,0.0),(1.0,1.0)].
5. Create a bankAccount class with attributes for the account number (a long integer), the ID card
of the client (another long integer), the current balance and the annual interest that applies to the account (percentage).
Define the following methods in the class:
Programming with Java (Course for IZAR) Exercises of Topic 4 — Page 1
Default constructor and constructor with DNI, balance, and interest
Accidents and mutators. There will be no mutator for the account number.
updateBalance(): will update the account balance by applying the daily interest
(annual interest divided by 365 applied to the current balance).
deposit(double): will allow entering an amount into the account.
withdraw(double): will allow withdrawing an amount from the account (if there is a balance).
Method that allows us to show all account data.
The account number will be assigned sequentially starting from 100001.
the following number to the last assigned.
Constructor with the maximum capacity of the coffee maker; initializes the current amount of coffee equal to the
maximum capacity.
Constructor with the maximum capacity and the current amount. If the current amount is greater than the
maximum capacity of the coffee maker, it will be adjusted to the maximum.
7. Create a NIF class that will be used to maintain IDs with their corresponding letter.
The attributes will be the DNI number (long integer) and the corresponding letter.
The class will have the following methods:
Default constructor that initializes the DNI number to 0 and the letter to a blank space (it will be a
Invalid NIF.
Constructor that receives the DNI number and automatically sets the letter that corresponds to it.
corresponds.
Accidentals and mutator for the DNI number (that automatically adjusts the letter).
read(): that asks for the DNI number (automatically adjusting the letter)
Method that allows us to show the NIF (eight digits, a hyphen and the uppercase letter; for
example:00395469-F)
The letter will be calculated using an auxiliary (private) method as follows:
the remainder of the integer division of the DNI number by 23 is obtained and the following table is used for
get the corresponding letter:
T
F
8. Create a Date class with attributes for the day, the month, and the year of the date.
Include at least the following methods:
Default constructor with 1-1-1900 as the default date.
Parameterized constructor with day, month, and year.
read(): will ask the user for the day (1 to 31), the month (1 to 12), and the year (1900 to 2050).
leapYear(): will indicate whether the year of the date is a leap year or not.
daysInMonth(int): will return the number of days in the specified month
(for the year of the date).
validate(): will check if the date is correct (between 1-1-1900 and 31-12-2050);
if the day is not correct, it will set it to 1; if the month is not correct, it will set it to 1;
and if the year is not correct, it will set it to 1900. It will be a helper method (private).
This method will be called in the parameterized constructor and in read().
Accidents and mutators.
short(): will display the date in short format (02-09-2003).
daysPassed(): will return the number of days that have passed
from 1-1-1900 to the present date.
dayOfWeek(): will return the day of the week of the date
(0 for Sunday, ..., 6 for Saturday). January 1, 1900 was Sunday.
long(): will display the date in long format, starting with the day of the week (Tuesday 2
September 2003.
fechaTras(long): will make the date corresponding to the days that have passed.
indicated from 1-1-1900.
daysBetween(Date): will return the number of days between the date and the one provided.
next(): will go to the next day.
previous(): will go to the previous day.
copy(): will return a clone of the date.
equalTo(Date): indicates whether the date is the same as the provided one.
lessThan(Date): indicates if the date is earlier than the provided one.
greaterThan(Date): indicates if the date is later than the provided one.
Programming with Java (Course for IZAR) Exercises from topic 4 — Page 3
9. Create the following classes (each in its own file):
Engine: with methods to start the engine and turn it off.
Wheel: with methods to inflate and deflate the wheel.
Window: with methods to open and close it.
Door: with a window and methods to open the door and close the door.
Car: with an engine, four wheels, and two doors;
with the methods that you find appropriate
10. Create a Time class with attributes for hours, minutes, and seconds of the time.
Include at least the following methods:
Default constructor with 00:00:00 as the default time.
Parameterized constructor with hours, minutes, and seconds.
read(): will ask the user for hours, minutes, and seconds.
validate(): will check if the time is correct; if it is not, it will adjust it. It will be a helper method.
(private) that will be called in the parameterized constructor and in read().
Accidents and mutators.
print(): will display the time (07:03:21).
aSeconds(): will return the number of seconds elapsed since midnight.
fromSeconds(int): it will make the time correspond to having elapsed
since midnight the seconds that are indicated.
secondsSince(Time): will return the number of seconds between the time and the one provided.
next(): will move to the second next.
previous(): will go to the second previous.
copy(): will return a clone of the hour.
indicates whether the hour is the same as the provided one.
lessThan(Hour): indicates if the hour is earlier than the one provided.
greaterThan(Time): indicates if the time is later than the one provided.
11. Create an Employee class that models the information that a company maintains about each
NIF
of personal income tax, whether married or not and number of children.
The class must include accessors and mutators for all attributes. When creating the objects,
you can provide, if desired, the ID number. The other services that must be provided
the objects of the class will be the following:
Calculation and refund of the additional payment corresponding to the extra hours worked.
Calculation and refund of gross salary.
Calculation and refund of withholdings (IRPF) based on the rate, taking into account that the
The retention percentage to apply is the rate minus 2 points if the employee is married.
and minus 1 point for each child they have; the percentage is applied to the entire gross salary.
println(): visualization of the basic employee information.
Java Programming (Course for IZAR) Exercises from topic 4 — Page 4
printAll(): visualization of all employee information. The basics plus the base salary,
the extra hour allowance, the gross salary, the IRPF withholding, and the net salary.
copy(): object cloning.
12. Create a DateTime class that is used to model historical moments (specific day moments).
The class must be built based on the previous Date and Time classes (an attribute from each class)
and you must provide it with all the functionality you deem appropriate.
Programming with Java (Course for IZAR) Exercises from topic 4 — Page 5