Download CT 229 Lab Assignment 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CT 229 Lab Assignment 5
Notes:

Submit Completed .java files @
http://ecrg-vlab01.it.nuigalway.ie/uploader/Submissions.html

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)
(ii)
(iii)
(iv)
(v)
When the program is run it should be passed the total number of users as a
command line argument. If the number of users is not provided as a command
line argument then print out an error and exit the program.
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).
It should then ask for the age of each child (this value will be an int)
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)
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
by itself 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
elements with 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
multiple of 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 a prime number. These indices can be displayed. Write a program that uses an array of
1000 elements to determine and print the prime numbers between 2 and 999. Ignore
elements 0 and 1 of the array.
[Call this java file PrimeNumbers.java]