COMP1400 – Programming for Designers

Class announcements for Programming for Designers

COMP1400 – Programming for Designers random header image

Tutorial – Wk4

Posted by on August 2nd, 2012 · Lab

This week, you will download the BlueJ project Week4.zip and write methods to manipulate arrays. Two methods are given to you complete. You don’t have to do anything to them.

The method readArray

public int[] readArray()

reads a list of numbers, one per line, ending with a blank line, and returns an array containing the numbers. Don’t try to understand the details of this method. It uses things that you’ll learn about later.

The method printArray takes an integer array parameter and prints it to the terminal

public void printArray(int[] R)

The project provides templates for other methods that you have to write. Your job is to work out the code that goes inside them.

The first method, arrayAdd, takes two arrays as parameters and returns a new array that is the sum of the first two.

public int[] arrayAdd(int[] R1, int[] R2)

The addition of two arrays yields an array whose elements are the sum of the corresponding elements in the parameter arrays, i.e.

R1[0] + R2[0], R1[1] + R2[1], R1[2] + R2[2], ....

The dotProduct of two arrays returns a single integer.

public int dotProduct(int[] R1, int[] R2)

It is found by multiplying corresponding elements and adding them all up, i.e.

R1[0] * R2[0] + R1[1] * R2[1] + R1[2] * R2[2], ....

Finally, you should write a method, main,

public void main()

that reads two arrays, then:

calls arrayAdd and calls printArray to print the resulting array
calls dotProduct and prints the result

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

You must log in to post a comment.