Classes and Objects – Basic Tasks (Java)

Page 1 of 4 · Object-oriented programming – examples
Before you start: read the lessons Classes and objects in Java, Objects and methods.
How to practise: write the class and the program on your own, run it on the example from the task, and only then open the hint, and finally the solution. All solutions are in Java; the helper classes are in the same file (without the word public), so the program runs as one file.

The first tasks in object-oriented programming in Java: making a class with attributes, creating objects with the statement new, accessing attributes with a dot (frame1.a) and the first methods that calculate an area, a volume or a distance. The last tasks introduce private attributes, getters, setters, constructors and an array of objects – these are practised in more detail on the next pages.

Tasks 2 and 5 are also solved in video lessons (in Serbian).

TASK 01Difficulty: easy

Two people

Create a class that describes a person. Use the following data as the attributes of the class: first name, last name, gender, personal ID number.
In the main class make two objects that represent people and give them a first name, last name, gender and ID number.
Print the data about the people on the screen.

TASK 02Difficulty: easy

Squares

Make a program that creates two objects that represent squares. For the entered side lengths calculate their areas and show the results on the screen.

You can watch the solution of the task in the following video (in Serbian):

TASK 03Difficulty: easy

Two circles and a triangle

Create two classes, one that describes a triangle and another that describes a circle. Use the following data as the attributes of the class triangle: side, height and area.
Use the radius and the area as the attributes of the class circle.
In the main class make three objects, two circles and 1 triangle; give the radius for each circle, and the side and the height for the triangle.
Calculate the areas of the given shapes.
Print the areas of all the shapes on the screen.

TASK 04Difficulty: easy

Cars

Make a class that describes a car. Choose 5 attributes (e.g. colour, model, price etc.).
In the main (test) class create two objects that represent cars and give them data (e.g. model: Fiat Punto, colour: red, price 5000).
Print the data about the cars on the screen.

TASK 05Difficulty: easy

Points in a plane

Create 3 objects that represent three points in a plane, A, B and C. Move the point A to a new position A1.
Show the coordinates of the points after the move. The coordinates of the points are:
A(2,4)
B(5,-2)
C(-4,2)
A1(1,3)

You can watch the solution of the example in the following video (in Serbian):

TASK 06Difficulty: easy

A cube and a sphere

Create two classes that describe a cube and a sphere.
In the main class make three objects, two cubes and 1 sphere; give the edge for the cubes and the radius for the sphere.
Calculate the surface areas of the given solids and their volumes. Solve the task by using methods.
Make the following methods for all the objects: area(), volume(), print().
Print the surface areas and volumes of all the solids on the screen.

TASK 07Difficulty: medium

A prism and methods

Make a class that describes a regular square prism and in it make the following methods: for entering the data, for calculating the apothem, for calculating the base, the lateral surface, the surface area and the volume, and for printing the results. In the main class create a prism object and through it call these methods in a logical order.

TASK 08Difficulty: medium

Uniform motion of a car

A car moves uniformly towards a place b. Create an object that represents the car, if we know that its initial speed is 4 m/s and its initial position is 5 m from the starting point.
Show the speed and the position in the initial position.
Calculate the current position after 3 s and show the current data again.
How would the data change if the initial speed were 10 m/s?

TASK 09Difficulty: medium

Two spheres and a cuboid

Create 3 objects, two spheres and a cuboid. Make a program that calculates the surface areas and volumes of all 3 solids for the entered radii of the spheres and the edges of the cuboid.

TASK 10Difficulty: medium

The distance between two points

Create two objects that represent the points A and B in a plane. Make a method that calculates the distance between two points. Use the method you made to calculate the distance between the points.

TASK 11Difficulty: medium

Uniformly accelerated motion of a car

A car moves with uniform acceleration a (entered by the user) towards a place b. Create an object that represents the car, if we know that its initial speed is 4 m/s and its initial position is 5 m from the starting point.
Show the speed and the position in the initial position.
Calculate the current position after 3 s and show the current data again. How would the data change if the initial speed were 10 m/s?

TASK 12Difficulty: easy

Areas of trapezoids

Make a class that describes a trapezoid. List the needed attributes of a trapezoid in the class. In the main class create two objects that represent trapezoids. For the first trapezoid let the user enter the bases a and b and the height h. For the second trapezoid the values are known and should be set in the program: a=10, b=15, h=5. Calculate the areas of both trapezoids and show them on the screen.

TASK 13Difficulty: easy

Free fall

Make an object that represents a body that falls freely from some height H. Make a class that describes the body and list the needed data (the attributes of the class) in it. Make a method that calculates the speed of the body at the end of the fall, for some given initial height. Use that method to calculate the speed of the body at the end of the fall for the entered initial height.

TASK 14Difficulty: medium

Employees in a company

Make a class that describes an employee. Use these attributes: first name, last name, gross salary, net salary, deductions. Make the data private, so you need to add the matching getters and setters. Also add constructors, with and without parameters, and a method toString that prepares the data about the employee. Also make a method that calculates the net salary (net = gross − deductions, and the user enters the deductions).
In the test class create the data for n employees, calculate the net salaries of all the employees and show them on the screen.

TASK 15Difficulty: harder

A casino

Represent a player in a casino with a class that has the following attributes: the player's name, the name of the game they are playing at the moment ("poker", "blackjack" or "roulette" – other values are not allowed) and the account balance. The attributes should be private.
Make a class Casino that simulates the players' games of chance. As its field it should contain an array of the players in the casino.

  • toString() calls toString() of all the players in the array.
  • The method String mostProfitableGame() returns the name of the game played by the largest number of players whose account is in the minus. If there are several such games, it returns the first one it found.
  • In the main program make an object of the class Casino with an array of players and print the results of the methods toString() and mostProfitableGame().

An example of the result:

Toma plays poker and has 75000 in the account.
Nikola plays blackjack and has 23000 in the account.
...
Common mistakes and tips
  • A class is a "template", and an object is a concrete thing: Square s1 = new Square(); makes an object; without new the variable is null.
  • Every object has its own attributes: s1.a and s2.a are different values. A common mistake is copying a line and forgetting to change s1 into s2.
  • In Java 4 / 3 equals 1 (integer division); for the volume of a sphere write 4.0 / 3.
  • After nextInt() or nextDouble() the next nextLine() returns an empty line – add one empty nextLine() or read words with next().
  • If one file has several classes, only one may be public – the one with the method main, whose name matches the name of the file.

Check your knowledge

Open the quiz

Mini quiz: classes and objects

1. What does the statement Circle c = new Circle(); make?




2. How do you access the attribute r of the object c?




3. What is 4 / 3 * Math.PI in Java?




Next