CS1026 Computer Science Fundamentals I (Java)

Lab 6: Arrays


Objectives
Preparation

Exercise 1, warm-up: arrays of integers

In this exercise, you will practice initializing and working with an array of integers.

  1. In the Interactions pane, type the following:

  2. int[] nums = {1,2,3,4,5};
    System.out.println(nums[0]);
    

  3. How would you now print the rest of the items in the array nums?

  4. Type the following statement: System.out.println(nums[nums.length]); What happens? Why?


Exercise 2: changing the colors of pixels, without using arrays

In the Interactions pane, make a variable pictureObj refer to a picture that contains the image in the file caterpillar.jpg that is in your mediaSources folder.

We have seen the following methods:

We will use these methods to change the color of the following pixels of the caterpillar picture to black:

(100,100), (101,101), (102,102), (103,103), (104,104), (105,105), (106,106)

One way to do this is to type the following sequence of Java statements in the Interactions pane (note that you can repeat the previous line in the Interactions pane by using the up-arrow key, and then edit the line before hitting Enter for that line - this saves a lot of typing)

import java.awt.Color;
pictureObj.getPixel(100,100).setColor(Color.BLACK);
pictureObj.getPixel(101,101).setColor(Color.BLACK);
pictureObj.getPixel(102,102).setColor(Color.BLACK);
pictureObj.getPixel(103,103).setColor(Color.BLACK);
pictureObj.getPixel(104,104).setColor(Color.BLACK);
pictureObj.getPixel(105,105).setColor(Color.BLACK);
pictureObj.getPixel(106,106).setColor(Color.BLACK);
pictureObj.repaint();
  1. In the Definitions pane, create a new class Drawing and a static method drawLine that takes as input a Picture, returns nothing, and performs the same drawing as above. Compile it, and test it in the Interactions pane on a new picture.

  2. Modify your drawLine method, so that it takes two extra int parameters, that specify the starting point for the line (here, that was (100,100)).


Exercise 3: changing the colors of pixels using arrays

We have seen the method getPixels that returns an array containing all pixels from an image, arranged row after row. It is possible to use it to draw the same line as in the previous exercise, without using getPixel (note the difference: getPixels vs. getPixel; the latter gives access to a single pixel).

In this exercise, you are asked to write (and compile, and test) a static method drawLineArray, that takes the same arguments as drawLine, and draws the same line, but using the array given by getPixels.

You will need the object method getWidth() of the Picture class, which gives you the width of a picture. Suppose that the variable width holds this value, and that arrayPixels is the array you obtained from getPixels, then arrayPixels[100+50*width] is the pixel at position (100,50).


Exercise 4: introduction to loops

Loops will be introduced in topic 6. For the moment, we will practice with a simple construction, the while loop.

  1. Type the following in the Interactions pane:

    int counter = 1;
    while (counter <= 10)
    {
       System.out.print("*");
       counter = counter + 1;
    }
    System.out.println();
    
    The while construction ensures that the body of the loop (the two statements in the middle) are executed while the condition counter <= 10 holds.

  2. What happens if you remove the statement counter = counter + 1;? Why? To stop it, click on the Reset button.

  3. Write a static method printAsterisks that takes an int n as input, return nothing, and prints n asterisks. This method should be in a new class Asterisks. Compile; test.

  4. Modify the drawLine method, by adding a new int parameter to it, say n, so that it draws a line made of n pixels.