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.

Update Spot

We will now look at updating the stored values in the Spot fields.

Recall that:

  • fields in Spot are private.
  • “get” methods were provided to view the contents of these fields.
  • other methods were provided to calculate the area, radius and circumference.

However, there is no mechanism / methods provided to update the instance fields. If we want to provide this functionality, we have to add the following methods ‘set’ methods to Spot, one for each instance field:

public void setxCoord(float xCoord) {
    this.xCoord = xCoord;
}

public void setyCoord(float yCoord) {
    this.yCoord = yCoord;
}

public void setDiameter(float diameter) {
    this.diameter = diameter;
}

Now Spot is ready to update the contents of each of it’s fields; we just need to ask the user for the new value and update the contents with it.

Driver changes

In Driver, we have several methods that allow us to:

  • add a spot (user provides the information)
  • draw spot (print out the contents of the fields - using ‘get’ methods)
  • print area
  • print circumference
  • print radius

We now need to add a method that will allow us to update the contents of the Spot fields using the new ‘set’ methods we just added above.

Add the following method:

void updateSpotDetails(){
    System.out.print("Enter new xCoord value: ");
    float enteredXCoord = input.nextFloat();
    System.out.print("Enter new yCoord value: ");
    float enteredYCoord = input.nextFloat();
    System.out.print("Enter new diameter value: ");
    float enteredDiameter = input.nextFloat();
    spot.setxCoord(enteredXCoord);
    spot.setyCoord(enteredYCoord);
    spot.setDiameter(enteredDiameter);
}

And then update the Driver() constructor to call this new method and then redraw the spot (so we can see the new values):

    Driver(){
        addSpotDetails();
        drawSpot();
        printRadius();
        printArea();
        printCircumference();
        //update spot details and redraw spot
        updateSpotDetails();
        drawSpot();
    }

Run the app

When you run the app, you should have output similar to this:

Save your work.