Spis treści

Classes and objects


Introduction

The skills developed in this section constitute the basis for further exercises. Some exercises are illustrated with code fragments, which are usually used to develop them within a given exercise. If a library class is included in the exercise, its first occurrence will also be a link to the Java API documentation, where you can find the information needed to use it.

Example I

Below you will find the source code of the Rectangle class - a complex data structure that we will use to create Rectangle objects. The class contains two fields (member variables) of the double type, two constructors and a method (member function) for determining the area of ​​the rectangle. Next you will also find the source code of the Program class, which contains a method called main(), which will be called when the execution of the Program class is initiated. This method will perform the following actions - declaration of an object of the Rectangle class, creation of an object of the Rectangle class, calling the field() method for the created object and printing to the standard output the variable x storing the value returned by this method.

class Prostokat
{
   double dlugosc;
   double szerokosc;
 
   Prostokat()            
   {                      
      this.dlugosc=0.0;   
      this.szerokosc=0.0; 
   }                      
 
   Prostokat(double dlugosc,double szerokosc)  
   {                                          
      this.dlugosc=dlugosc;                   
      this.szerokosc=szerokosc;               
   }                                          
 
   double pole()                
   {                            
      return dlugosc*szerokosc; 
   }                            
}
public class Program
{
   public static void main(String[] args)         
   {                                             
      Prostokat obj;                             
      obj=new Prostokat(3,4);                    
      double x=obj.pole();                       
 
      System.out.println("Pole prostokata: "+x); 
   }                                             
}

exercise 1.1
Based on the information from the lecture, identify these elements, analyze the purpose of their use and their syntax.

exercise 1.2
Using the information from the lecture, set the appropriate compilation unit (you can save both class definitions in one file with the same name as the name of the public class and with the extension „.java”). Compile the project and analyze the compilation effects - the number and naming of files containing bytecodes for the Java virtual machine. Use any editor (vim, JCreator, NetBeans) and Java compiler (javac) that is part of the Java SE Development Kit (JDK) provided by Oracle. Then run the program using the interpreter (java) that is part of the Java SE Runtime Environment JRE (which is also part of the JDK). You can also find instructions on how to install and use the above tools on the websites.

exercise 1.3
To the Rectangle class, add an implementation of the perimeter() method that returns the perimeter of the rectangle and an appropriate call to this method in the program, which will enable you to test your implementation. Compile and test the example.

Example II

Write an implementation of the Point class to represent points on a plane. To the Rectangle class, add the center field, which is an object of the Point class. Add an appropriate constructor that allows you to initialize an additional field with information specifying the location of the center of a given figure on the plane. You can use the example below. Identify class components, analyze their purpose and syntax. Set the appropriate compilation unit and test the example.

class Punkt
{
   double x;
   double y;
 
   Punkt(double x,double y) 
   {                        
      this.x=x;             
      this.y=y;             
   }                        
 
   public String toString()          
   {                                 
      return "[x: "+x+", y: "+y+"]"; 
   }                                 
}
class Prostokat
{
   double dlugosc;
   double szerokosc;
   Punkt wierzcholek;
 
   Prostokat(double dlugosc,double szerokosc) 
   {                                          
      this.dlugosc=dlugosc;                   
      this.szerokosc=szerokosc;               
      this.wierzcholek=new Punkt(0,0);        
   }                                          
 
   Prostokat(double dlugosc,double szerokosc, Punkt wierzcholek) 
   {                                                             
      this.dlugosc=dlugosc;                                      
      this.szerokosc=szerokosc;                                  
      this.wierzcholek=wierzcholek;                              
   }                                                             
 
   public String toString()                                                   
   {                                                                          
      return "[dl: "+dlugosc+", sz: "+szerokosc+"]" + wierzcholek.toString(); 
   }                                                                          
 
   double pole()                
   {                            
      return dlugosc*szerokosc; 
   }                            
}
public class Program
{
   public static void main(String[] args)     
   {                                          
      Punkt obj1;                             
      obj1=new Punkt(-1,1);                   
      System.out.println("punkt: "+obj1);     
 
      Prostokat obj2;                         
      obj2=new Prostokat(3,4,obj1);           
      System.out.println("prostokat: "+obj2); 
 
      double p=obj2.pole();                   
      System.out.println("pole: "+p);         
   }                                          
}

exercise 1.4
Answer the question what the public String toString() method is used for.

exercise 1.5
In the 'Point' class, implement the void shift(double dx,double dy) method, which will translate the point by the vector (dx,dy). In your program, test the call to this method on any point.

Punkt obj;
obj = new Punkt(0,0);
System.out.println(obj);
 
obj.przesun(-1,3);
System.out.println(obj);

exercise 1.6
Also in the Rectangle class, implement the void shift(double u,double v) method, which will translate the figure (move the vertex of the rectangle) by the vector (dx,dy). In the definition of this method, use the move() method call from the Point class. In your program, test the call to this method using any rectangle as an example.

Punkt obj;
obj = new Punkt(0,0);
 
Prostokat obj2;
obj2=new Prostokat(obj,3,4);
System.out.println(obj2);
 
obj2.przesun(7,-3);
System.out.println(obj2);

exercise 1.7
Add a definition of the Circle class to your project containing the radius and center fields (of the circle). Write implementations of appropriate constructors and methods, similarly to the Rectangle class. Test the example.

exercise 1.8
Add implementations of the boolean contains(Point obj) method to the Rectangle and Circle classes. In the program, test with an example of several different rectangles and points.

exercise 1.9
Add the implementation of the boolean intersects(Circle obj) method to the Circle class. In the program, test on an example of several different districts.

exercise 1/10
Add an implementation of the boolean intersects(Circle obj) method to the Rectangle class. In the program, test on an example of several different rectangles and circles. Hint - consider the projections of a circle and a rectangle on the X and Y axes.

exercise 1/11
Using javadoc (a standard tool shipped with the JDK), create standard HTML documentation for the Rectangle, Circle and Point classes, containing descriptions of all the fields and methods of these classes.



Z. Dendzik, 2024