Course Info

#Programming Fundamentals 1

This is an introductory Programming module and assumes no prior knowledge of programming.

In this module, we will introduce you to the Java programming language through the Processing Development Environment (PDE) and then IntelliJ.

First, we will work through non-complex problems that will introduce you to the basic constructs of programming languages i.e. Sequence, Selection and Loops. You will also learn to use variables, different data types, manipulate the data, logical operators and methods. This will be done using processing.org

Then, using IntelliJ, we will progress to more complex problems that will briefly introduce you to object-oriented programming and data structures. You will do a deeper dive into both of these areas in the semester 2 module, Programming Fundamentals 2.

Private Fields

In object oriented languages, there is a rule called Encapsulation (data hiding) that your classes should follow.

This means that in Spot, we should have declared these fields as private:

  • xCoord
  • yCoord
  • diameter

Make that change now and you will see that it generates problems:

The reason we are having a problem is that we have directly accessed each of these fields in the Driver class i.e.:

Because we have made these three fields private, the Driver can no longer see them. But we need them private.

To fix this problem, we need to make the following changes, so that we are following the rules of Encapsulation:

  • In Spot, add the following accessor methods so that Driver can now see the contents of xCoord, yCoord, and diameter:
    public float getxCoord() {
        return xCoord;
    }

    public float getyCoord() {
        return yCoord;
    }

    public float getDiameter() {
        return diameter;
    }
  • In Driver, update the drawSpot method to use these accessors instead of directly accessing the fields:
    void drawSpot(){
        System.out.println("xCoord:        " + spot.getxCoord());
        System.out.println("yCoord:        " + spot.getyCoord());
        System.out.println("diameter:      " + spot.getDiameter());
    }

Now the problem should have resolved itself.

Run the app again to make sure all is working as expected and save your work.