This lesson is being piloted (Beta version)

Objects in Java

Overview

Teaching: 30 min
Exercises: 60 min
Questions
  • How do we create and use objects in Java?

Objectives
  • Explain the constructs that enable us to use objects.

  • Build on the example Rectangle class with objects and a RectangleApp.

Pi j2.3 objects from mcollison

Shapes worksheet.pdf

Rectangle.java

RectangleApp.java

Rectangle.java and RectangleApp.java

  1. Create a new projecrt in IntelliJ following the menu options: File > New > Project. Then select Java and name the project ‘ShapesTutorial’.
  2. In the project right-click ‘src’. Select ‘New’ > ‘Java Class’.
    • Create a class for Rectangle.java from the lesson which can also be downloaded on the link at the top.
    • Create a class for RectangleApp.java the for which is also linked at the top.
  3. Compile and run the RectangleApp.java code using either the right-click “Run ‘RectangleApp.main()’” or Run > Run from the menu options.
    • Read the code and make sure you understand what is happening. Discuss the code structure with your neighbour.

The this keyword

  1. In the Rectangle class, replace the second constructor with arguments as follows:

public Rectangle(double width, double height)

What changes should you make for the constructor’s body to set the local object attributes?

Hint: Use the this keyword to resolve the conflict between instance attributes and constructors’ arguments.

  1. In the third constructor’s body, currently the attributes (width and height) are initialized with two assignment statements:

width = w; height = h;

Could you replace the above statements with one statement which calls the second constructor?

Hint: Use the this keyword again, this time to call the constructor.

Adding methods

  1. Add two methods, both are for zooming the rectangle, therefore, they share the same method name (zoom) but with different arguments, which is known as method overloading.
    • The rectangle’s width and height are zoomed with the same factor, i.e., only one argument is required for the zoom() method.
    • The rectangle’s width and height are zoomed with different factors, i.e., two arguments are needed for the zoom(int vertical, int horizontal) method.

In the RectangleApp class’s main() method, create one more rectangle (name it rect1) by calling one of the three constructors with any initial values. Then call the two methods you have created, test if the two methods are correct or not.

  1. Add a method for determining if two rectangles are overlapped or not:

public boolean isOverlappedWith(Rectangle r){...}

As discussed in the lesson, this must be an instance method, as we need create one rectangle instance to call this method whose argument is another rectangle instance. In the RectangleApp class’s main() method, test if the method is correct or not by using the two existing rectangle objects (myRect and rect1). You may either

boolean b1 = myRect.isOverlappedWith(rect1); Or boolean b2 = rect1.isOverlappedWith(myRect);

The output values (b1 and b2) for the above two statements must always equal, otherwise, your method must be wrong. Create examples Rectangle objects to check if your method is functional.

Access modifiers

  1. As mentioned in the lesson, a well-encapsulated class always hide their attributes to avoid the object’s state (or attributes) been directly changed outside of the class. To achieve this principle, we need set all the instance attributes private. Then provide public setter and getter methods to modify and view the attributes. Change the Rectangle class to a well-encapsulated class and note, you’ll need make corresponding changes to solve issues in RectangleApp.java.

Hint: Select ‘Code’ > ‘Generate’ > ‘Getters and setters’ from the IntelliJ menu options to auto generate the code.

private double width; // private attribute public double getWidth(){ // getter method return width; } public void setWidth(double width){// setter method this.width = width; } // do similar changes for the other attributes ...

Circle.java and CircleApp.java

We previously learnt how to calculate the area and circumference of a circle by putting everything in the main() method in the CircleComputation.java file. Now let’s re-implement it in the object-oriented way. Similar to what you have done with the rectangles, you need first define a Circle class to encapsulate all relevant attributes and methods about a circle. Then add a test class CircleApp with a main() method to create circles and call all circles’ methods.

You may consider the following attributes, methods and constructors in the Circle class.

Finally, compile and run your code creating some example circles to test if it works as expected.

Key Points

  • Object-oriented programming is a programming paradigm that uses abstraction (in the form of classes and objects) to create models based on the real world environment