Week02

Introduction

This tutorial covers fundamental concepts of Object Oriented Programming (OOP) in Java, including class implementation, instance variables and methods, and UML notation for classes. This guide is structured over a week, from 02/10/2023 to 06/10/2023, designed to allow learners to first attempt answers independently before consulting provided solutions.

Creating a Class

To create a class in Java, the following steps are essential:

  1. Project Setup: Open NetBeans and create a new project named TutorialWeek02.

  2. Define a Class: A simple Java class called Person can be created. This class needs to define three instance variables: name, surname, and age. The creation of this class involves right-clicking on the package of TutorialWeek02, selecting New -> Java Class, and naming the class Person before completing the setup by clicking Finish.

Implementing Constructor and Methods

Defining Constructors and Methods

  • Following the class definition, a constructor that initializes the name variable is added:

public Person(String n) {
    name = n;
}
  • Additionally, a method named displayName() is established to print the name:

public void displayName() {
    System.out.println(name);
}

Utilizing the Person Class in Main

In the main method of TutorialWeek02, an instance of Person is created as follows:

Person p = new Person("Ben");
p.displayName();

Additional Methods

Furthermore, additional methods are implemented for setting surname and age:

  • void setSurname(String s);

  • void setAge(int num);Accessory methods are also created to retrieve the surname and the age:

  • String getSurname();

  • int getAge();

Working with Classes and Instances

Creating the Circle Class

Throughout the tutorial, a new class called Circle is implemented, which involves:

  1. Creating a project named TutorialCircle in NetBeans.

  2. Define the class Circle with the following properties and methods:

    • Two private instance variables: radius (double) and colour (String).

    • Two overloaded constructors:

      • A default constructor:

      public Circle() {
          radius = 1;
          colour = "Blue";
      }
      • A constructor that accepts a double for radius:

      public Circle(double r) {
          radius = r;
          colour = "Blue";
      }
    • Public methods to retrieve the radius and compute area:

    public double getRadius() {
        return radius;
    }
    public double getArea() {
        return radius * radius * Math.PI;
    }

Compiling and Testing the Circle Class

It is important to note that while the class can be compiled, it does not have a main method and therefore cannot be executed independently. Testing occurs in TutorialCircle by instantiating instances of the Circle class:

Circle circle1 = new Circle();
System.out.println("The circle has radius of " + circle1.getRadius() + " and area of " + circle1.getArea());  
Circle circle2 = new Circle(8);
System.out.println("The circle has radius of " + circle2.getRadius() + " and area of " + circle2.getArea());

Understanding Access Modifiers (Private and Public)

Private Variables

When accessing private variables like radius, direct access using circle1.radius is prohibited. To retrieve or modify this variable, public getter and setter methods should be used instead:

  • To retrieve:

System.out.println(circle1.getRadius());
  • To set:

circle1.setRadius(10);

Constructors in the Circle Class

Adding a Third Constructor

To enhance functionality, a third constructor is proposed that accepts both radius and colour as parameters:

public Circle(double r, String c) {
    this.radius = r;
    this.colour = c;
}

Modifying the Program

The main program TutorialCircle must be adjusted to utilize this new constructor and include methods returning the colour, printing the circle's attributes alongside their colours.

Setting and Updating Values

Setter Methods

New setter methods allow changing these private instance variables post-instantiation:

  • public void setRadius(double newRadius) { radius = newRadius; }

  • An additional method for setting colour should follow a similar structure.

toString() Method

Implementing toString()

Adding a toString() method is recommended for better object representation. This method should return a string describing the object's properties, such as radius and colour:

public String toString() {
    return "Radius: " + radius + ", Colour: " + colour;
}

Conclusion

In summary, the tutorial covers the essential aspects of creating and utilizing classes and objects in Java. Through understanding constructors, access modifiers, and methods, learners will grasp key OOP principles that facilitate effective programming.