1. Classes, Objects, Packages

In the previous chapters, we used classes as containers for static methods. We did not intentionally build new objects with new. In this chapter, the following exercises deal with creating new objects, object references, and the special null reference.

Prerequisites

  • Know the difference between object type and reference type

  • be able to use new

  • know the purpose and basic operation of the automatic garbage collector

  • be able to pass objects to methods and return them

  • be able to build packages

  • be able to import types

  • be able to separate equivalence and identity

  • understand the problem with null reference

Data types used in this chapter:

1.1. Creating objects

For the following examples, we use the classes Point and Polygon from the package java.awt. The java.awt package contains various classes, many for graphical interfaces. However, the point and polygon are not graphical, and we will not program graphical interfaces. The Java types for points and polygons, however, are nice simple data types that have publicly accessible instance variables as well as well-understood object methods, so they are quite suitable for our first exercises. We will not use any other data types from the java.awt package in the exercise book.

1.1.1. Draw polygons ⭐

A polygon is a closed set of lines. The Java library provides a class java.awt.Polygon for polygons, which we can “feed” with points.

Captain CiaoCiao goes by ship to the vicinity of the Bermuda Triangle and looks for the water spirit Undine. But the Bermuda Triangle is dangerous and full of killer squids. The sailors must avoid the area at all costs. What would be good now is a map …​

Draw polygons

Task:

  1. Create a new class BermudaTriangle with a main(…​) method.

  2. Declare three constants in the method:

    final int DIMENSION = 50;
    final String RAINBOW = "\uD83C\uDF08";
    final String FOG = "\uD83C\uDF2B";
    final String OCTOPUS = "\uD83D\uDC19";
  3. Create a java.awt.Polygon object.

  4. A polygon consists of points that are added using a method. What is the name of this method?

  5. Create a triangle for the mysterious Bermuda triangle. Keep the coordinates in the range from 0 to 50.

  6. If the ship’s position is a point, how can you find out if a chosen point is inside the triangle?

  7. Create two nested loops for 0 <= x < 50 and 0 <= y < 50, creating a rectangular grid. In the body, test:

    • If the x-y coordinate hits the edge of the virtual screen, write a rainbow character (RAINBOW).

    • If the x-y coordinate is a point inside the polygon, output an octopus (OCTOPUS), otherwise a fog character (FOG).

Starting with Java 9, there is the module system. By default, only the types from java.base module are included, and this does not contain GUI types. AWT types are part of the desktop module java.desktop. If you use modules (a file module-info.java exists in the main directory of the application), you have to include the java.desktop module:

module com.tutego.bermuda {
  requires java.desktop;
}

1.2. Working with references

After creating an object with new we get back a reference to the instance. This reference can be passed to other methods, and methods can also return references.

1.2.1. Build triangles ⭐

Captain CiaoCiao is sure that the dangerous places in the Bermuda Triangle can be random — he must be prepared for anything.

Build triangles

Task:

  • Create a new static method in the existing BermudaTriangle class:

    static Polygon resetWithRandomTriangle( Polygon polygon ) {
      // return initialized triangle
    }

    This method should first clear the passed java.awt.Polygon, which could still contain points, then fill it with a random triangle and return it at the end.

  • Write another static method that returns a new random triangle:

    static Polygon createRandomTriangle() {
      // return random triangle
    }