Tasks studies - laboratory
Create a project named DaneOsobowe
containing two classes: DaneOsobowe
and Osoba
(see the diagram below).
Complete the classes with the following code:
Osoba:
DaneOsobowe:
What results will be displayed in the console?
Important (Read This!!!):
Constructors are used to create objects. In Java, a constructor has the following characteristics: its name is identical to the class name it belongs to; it can have parameters, which are typically used to set field values in the class; unlike methods, it does not return any value. If a programmer does not define any constructor within a class, the compiler will create a default constructor during compilation. This default constructor is equivalent to an empty constructor, with the difference being that it will not appear in the program’s code. Java creates default constructors only if no other constructors are defined in the class.
Constructors and methods in Java support overloading. This means they can exist in multiple versions (forms). Overloaded methods share the same name but may differ in the number of arguments, argument types, return types, and method implementation (code within the method). The same applies to constructors, which inherently have the same name and, unlike methods, do not return values. Therefore, constructors cannot be overloaded based on the return type. Similarly, methods cannot be overloaded based on the return type, but this does not mean all methods must have the same return type. In conclusion, the variability of overloaded methods is achieved through differences in the number and type of arguments they accept. Overloading is part of polymorphism in programming languages.
Based on Task 1, create an application containing a class Student
with the following fields: name
, surname
, student_id
, specialization
, year_of_study
. Choose appropriate data types for the fields. Create four overloaded constructors for this class (each version of the constructor should accept at least one parameter—do not create an empty constructor). Create a method to display the student’s information. Create four objects of the Student
class, each using a different constructor overload during object creation. For each object, run the method that displays the student’s information.
Create an application that allows entering student data from the console. Use the Scanner
object and the Student
class from Task 2 for this purpose.
Create a project ObliczanieFigur
(Calculating Figures). Add classes to the project describing the following geometric shapes: Circle
, Square
, Rectangle
, Cube
, Cuboid
, Sphere
, Cone
. For each class, choose appropriate fields. Also, create methods to calculate the areas, perimeters (for 2D shapes), and volumes (for 3D shapes). For each class, create a method to display information about the shape, such as its name, parameters, area, and perimeter or volume. Create objects for these shapes and show the calculation results using the display method.
Create a calculator for geometric shapes with a menu allowing: selecting a geometric shape and entering parameters for that shape from the console. Then display the results using the data display method.
Create a project named WprowadzDane
(EnterData) containing a 100-element array of the Student
class (use the Student
class from Task 2). Create a method that creates an object for each array element and sets default values for it, such as 0
for numeric types and an empty string ""
for string types.
Create methods enabling:
Optional (non-mandatory) tasks or elements of tasks.