Test: Classes, Objects and Methods (Java)

Test 8 of 8 · Tests

A test on object-oriented programming in Java: a class with attributes, objects created with the statement new, a constructor with parameters, methods that compute something (area, speed, distance) and a simple graphical window.

Before you start: revise the lessons Classes and objects in Java, Objects and methods, Introduction to graphics in Java, and then do the tasks on the pages Object-oriented programming – examples, Additional examples: classes and objects.
How to practise: choose one group and solve it in 45 minutes, without looking at the solutions or your notes. Only after that compare your solution with the solution of the practice test.

What you need to know for the test

  • creating a class with attributes and objects of that class
  • a constructor with parameters and the keyword this
  • methods that return a value (area, volume, speed, distance) and calling them through an object
  • private attributes and getters/setters
  • the window JFrame: size, position, colour and components

What the test looks like

  • Duration: one school lesson (45 minutes).
  • Tasks: 3 (sometimes 4) tasks; every group has tasks of the same difficulty, but with different data.
  • What is graded: whether the program works correctly for the example and for edge cases, whether the input and output are as in the text and whether the code is clear.
Suggested scoring (3 tasks of 10 points each)
Points0–1112–1617–2122–2627–30
Grade12345

The scale is a suggestion for self-assessment; the teacher can adapt it. (In Serbian schools grades go from 1 to 5, where 5 is the best.)

Tasks by groups (PDF)

Every card opens the PDF of one group (the PDF files are in Serbian). Below each title the tasks are listed briefly, to make it easier to choose a group.

Test – object-oriented programming, 5 groups

Every group has 3–4 tasks: two objects with a formula, a class with a constructor, a class with a method for motion and a graphical window.

Practice test with solutions

The tasks from "Group 1" (PDF) are written out here as text, with an example, a hint and a solution. Set a timer for 45 minutes and try to solve them on your own; open the solutions only at the end.

TASK 01Group 1

Two circles

Create two objects that represent two circles. For the first circle the user enters the radius, and for the second it is known: r = 23 cm. Compute the areas of both circles and show them on the screen.

Example

Input

10

Output

Circle 1: A = 314.16 cm2
Circle 2: A = 1661.90 cm2
TASK 02Group 1

A favourite drink (constructor)

a) In the main class create 2 objects that represent your favourite drink. The class should have at least 5 attributes.
b) Make a constructor with parameters.
c) Show the data of both objects.

Example

Output

Lemonade, soft drink, 0.33 l, 150.0 RSD, sparkling: no
Yogurt, milk drink, 1.0 l, 180.0 RSD, sparkling: no
TASK 03Group 1

The speed of a car (method)

Make a class that describes a car moving uniformly. In the class make a method that computes the speed from the distance travelled and the time. In the main class create an object, enter the distance (m) and the time (s), compute the speed and show it in m/s and km/h.

Example

Input

120 8

Output

Speed: 15.0 m/s = 54.0 km/h
TASK 04Group 1

A graphical window

Make an orange window, 800 wide and 600 high, moved from the top left corner of the screen by 12 along the x axis and 34 along the y axis. (In the original test the task comes with a picture; every correctly shown component is worth 1 point, and the window 2 points.)

Common mistakes and tips
  • The name of the public class must be the same as the name of the file; helper classes in the same file are written without public.
  • A constructor has no return type and has the same name as the class. If you write it with parameters, new MyClass() without arguments no longer works.
  • A method that "computes" should return the result (return), and the main program does the printing.
  • Math.PI, Math.sqrt and Math.pow are ready to use – do not write 3.14.
  • For the window: setBounds(x, y, width, height) and always setVisible(true) at the end.

Check your knowledge

Open the quiz

Mini quiz: classes and objects

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




2. Which method should have the return type double?




3. How do you set the position (12, 34) and the size 800×600 of the window f?