Objectives

On completion of this lab you should:

  • understand int and float data types and be able to use them in non-complex problems and calculations.

Variables (int)

  • In Java, a variable with an int data type holds a whole number.

Using the int data type

  • Create a new Sketchbook in your PDE and save it as lab04_step01.

  • Enter the following code:

Using int data type to set the coordinates of lines

  • Run the code. Your window should now have three blue lines similar to the screen shot below:

Using int data type to set the coordinates of lines

  • Update the code to produce the following output. Note: you must use the a,b,c and d variables to generate these lines:

Using int data type to set the coordinates of lines

A syntax error

  • Update the code by changing int to Int (uppercase I). Run your code again. What happened? Can you explain what happened? Ask your teacher to explain it if you are not sure.

  • Change Int back to int.

Another syntax error

  • Update the code by changing the amount assigned to variable a from 70 to 70.56. Run your code again. What happened? Can you explain what happened? Ask your teacher to explain it if you are not sure.

  • Change 70.56 back to 70.

  • Save and close the sketchbook.

Variables (float)

  • In Java, a variable with a float data type holds a decimal point number.

Using the float data type

  • Create a new Sketchbook in your PDE and save it as lab04_step02.

  • Enter the following code:

Using the float data type to set the coordinates of a square

  • Run the code. You should see a grey window with a white square drawn in it.

Not a syntax error!

  • Update the code by changing the amount assigned to the xCoordinate variable from 50.67 to just 50.

  • Run the code. Why did it work? Can you explain what happened? Ask your teacher to explain it if you are not sure.

  • Change the amount assigned to the xCoordinate variable back to 50.67.

A syntax error

  • Update the code by renaming the yCoordinate variable name to xCoordinate. Run your code again. What happened? Can you explain what happened? Ask your teacher to explain it if you are not sure.

  • Change the code back.

  • Save and close the sketchbook.

Exercises

  • The exercises are typically based on the material we covered in the previous steps in this lab.

  • For each exercise listed below, open a new sketchbook.

Exercise 1 (static drawing)

  • Type the following code from your lecture into your sketchbook. When you run it, does the output look like this screenshot:

Slide from your lecture

  • Using this code as a starting point, make the following changes so that your output looks like this:

Desired output

  • NOTE: the object of this exercise is to use the appropriate arithmetic operators e.g.

    • Addition (+)
    • Subtraction (-)
    • Division (/)
    • Multiplication (*)
  • on the variables (a,b,c).

Exercise 2 (static drawing)

  • The object of this exercise is to produce the same output as Exercise 1, but instead of using variables (a,b,c) use appropriate system variables (i.e. width, height) and perform artihmetic operations on them to produce the desired output.

Challenge Exercise 1 (static drawing)

  • The object of this exercise is to produce the same output as shown in the image below:

Desired output

  • The rules for drawing this image are as follows:

    • The size of the display window is 300x300.
    • The variables of type int that you will need are:
      • xCoord initialised to 0.
      • yCoord initialised to 0.
      • offset initialised to 20.
    • There are 15 rectanges drawn and you must use the variables listed above and the system variables e.g. height and width. You cannot hardcode the coordinates and size of the rectangles. This means that you must use mathematical calculations (e.g. +, -, /, *) on the variables to position the rectangles and determine their size. Note: the only time you are permitted to use a number is when you are multiplying variables!
    • NOTE: you many need to review the Order of Evaluation notes for this exercise.

Solutions

  • The solutions for this lab can be found here.

  • The file is in Zip format, so when you download it, you will need to unzip it. If you don't have unzipping software installed on your computer, 7Zip is a good choice.