Available: Tue, Oct 11,
2011
Due: Sat, Nov 5, 22:00
This assignment is to be done in INDIVIDUALLY. Follow the instructions in the lab
manual for submitting assignments through the Virtual campus. Question 1
requires an algorithm in a file A3Q1.doc. Question 2 requires you to implement
a Java program in a file A3Q2.java and compile it in A3Q2.class. Paste the
content of the .java file into a file A3Q2.doc. Question 3 requires you to fix
a program, in the file A3Q3.java (also submit .class file, and A3Q3.doc with
the Java cod). Zip all the .doc, .java, and .class files in a3_xxxxxx.zip,
where xxxxxx is your student number, and submit it through the Virtual Campus).
a) Design an algorithm that takes as input an
array L1 of length n, where L1= a1, a2,…aN and
an array L2 of length m, where L2 = b1, b2, … bM
and returns a new array L3 of length n x m whose elements have the following
values: L3= a1+b1, a1+b2…,a1+bM,
a2+b1, a2+b2,…, aN+bM.
For example, if the input to your algorithm is L1 = [1, 2, 4] and L2 = [1, 3,
5, 7], then the result should be [2, 4, 6, 8, 3, 5, 7, 9, 5, 7, 9, 11]. The
number of givens is 4 (the 2 arrays and their lengths). You only have to show
the algorithm that solves the problem. You do not have to show the algorithm
for the main method that reads the two arrays, calls the problem solving
algorithm, and displays the result.
b) Trace the algorithm
for the following inputs: L1= [1, 2] L2
= [1, 3] N = M = 2
Implement in Java the
following algorithm from Question 1. The program should include a main method
and a method that implements the algorithm. The main method that reads the two
arrays from the keyboard, calls the problem solving method, and prints the
elements of the resulting array. To read an array from the keyboard, you can
ask the user for the length, allocate memory, then read each element, in a
loop; alternatively you can use ITI1120.readIntLine() that reads all the array
at once and allocates memory for it. The problem solving method can have 4
parameters (as the algorithm), or only two, since in Java the length of an
array can be passed together with the array.
The following Java program contains errors
that you must fix. These errors can be in the syntax (which may cause compiling
errors) or logical (which may cause the program not to do what it is supposed
to do). After fixing all errors, save your revised program and include
them in your submission. When copying the java code in the doc file, underline
all the modifications you have made from the original versions of the files.
See A3Q3.java