CT 229 Lab Assignment 5

Notes:

  • Submit Completed .java files @
  • 80% of marks in this assignment are for fully-functional and logically correct programs (40% for Q1 and 40% for Q2). The other 20% is for the use of correct programming style.
  • Remember to include your student name and number at the beginning of each java file in a header comment.
  • Assignment is due on Friday Nov 17th.

Q1.

Write a program that will ask information from a number of users:

(i)When the program is run it should be passed the total number of users as a command line argument. If the number of usersis not providedas a command line argument then print out an error and exit the program.

(ii)For each user it should ask the user for the number of children they have(if a user doesn’t have children they can enter 0).

(iii)It should then ask for the age of each child (this value will be an int)

(iv)The program should store this data in a ragged 2D array so that each row of the 2D array contains the age-related information provided by single user

(Remember if a the first user has two children then the first row will have two columns, if the second user has five children then the second row in the array will have five columns)

(v)The program should then call a print method, which takes in the 2D array as a parameter. It should print out a table of all the information contained in the 2D array

[Call this java program RaggedArray.java]

Q2.

(The Sieve of Eratosthenes) A prime number is any integer that is evenly divisible only byitself and one. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:

a) Create a primitive type boolean array with all elements initialized to true. Array elementswith prime indices will remain true. All other array elements will eventually be

set to false.

b) Starting with array index 2, determine whether a given element is true. If so, loop

through the remainder of the array and set to false every element whose index is a multipleof the index for the element with value true. Then continue the process with the

next element with value true. For array index 2, all elements beyond element 2 in the

array that have indices which are multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to

false; for array index 3, all elements beyond element 3 in the array that have indices

which are multiples of 3 (indices 6, 9, 12, 15, etc.) will be set to false; and so on.

When this process is complete, the array elements that are still true indicate that the index is aprime number. These indices can be displayed. Write a program that uses an array of 1000 elementsto determine and print the prime numbers between 2 and 999. Ignore elements 0 and 1 of the array.

[Call this java file PrimeNumbers.java]