Implementing an Interface

The Procedure

Real Lab Procedure

  • First, click on the class diagram then the below diagrams will be shown. 
  • There are two classes in the program, and it has three parts [class name, variables, function]. 

 

Fig 1 

Fig 2 

  • Fig 1 shows the class diagram of class Point. 
  • Fig 2 shows the class diagram of class Driver.  
  • Then click on the sequence diagram which will show how the program that on the left side is executed. 

             Sequence diagram 

Following steps are explaining sequence diagram. 

  • The diagram defines a class called Point with two private instance variables: x and y, which represent the coordinates of a point in a 2D plane. 
  • The Point class has four private methods.
  • setX(int xCoord) sets the value of the x coordinate. 
  • setY(int yCoord) sets the value of the y coordinate. 
  • getX() returns the value of the x coordinate. 
  • getY() returns the value of the y coordinate. 
  • Next, an interface named Shape is defined. It includes the following.
  • A public static final constant pi with a value of 3.14. 
  • Two abstract methods: area() and perimeter(), which any class implementing this interface must provide concrete implementations for. 
  • The code then defines a class named Circle which implements the Shape interface. 
  • The Circle class has a protected instance variable radius to store the radius of the circle. 
  • The constructor Circle(double r) initializes the radius with the given value r. 
  • The area() method calculates and returns the area of the circle using the formula: pi * radius * radius. 
  • The perimeter() method is not defined properly in the code. It seems like there's a typo with the variable p, and it's missing a return statement. It should be fixed to properly calculate and return the perimeter of the circle using the formula: 2 * pi * radius. 
  • Inside the main method, an instance of the Circle class is created with a radius of 5. 
  • The area() method of the Circle class is called to calculate the area of the circle. 
  • The perimeter() method of the Circle class is called to calculate the perimeter of the circle. 
  • The results are then printed to the console using System.out.println().

Simulator Procedure

  • There are two buttons [Class Diagram and Sequence Diagram] on the right side of the simulation.
  • First, click on the Class Diagram then the class diagrams will be shown.
  • Then click on the Sequence Diagram, it will display two buttons – Previous and Next.
  • The Next button shows the next sequence, and the Previous button shows the previous sequence.
  • After all the sequence diagrams the result box will be shown.
  • Click on the Reset button to redo the simulation.