CmSc150 Fundamentals of Computing I
Homework 08 name_____________
Part 1: Problems
1. What does the following code do? Assume list is an array of int values, temp is some previously initialized int value, and c is an int initialized to 0.
for (int j = 0; j < list.length; j++)
if (list[j] < temp) c++;
a) It finds the smallest value and stores it in temp
b) It finds the largest value and stores it in temp
c) It counts the number of elements equal to the smallest value in list
d) It counts the number of elements in list that are less than temp
e) It sorts the values in list to be in ascending order
2. Assume an int array candy stores the number of candy bars sold by a group of children where candy[j] is the number of candy bars sold by child j. Assume there are 12 children in all.
What does the following method do?
public int question15( )
{
int value1 = 0;
int value2 = 0;
for (int j = 0; j < 12; j++)
if (candy[j] > value1)
{
value1 = candy[j];
value2 = j;
}
return value2;
}
a) It returns the total number of candy bars sold
b) It returns the total number of children who sold 0 candy bars
c) It returns the total number of children who sold more than 0 candy bars
d) It returns the number of candy bars sold by the child who sold the most candy bars
e) It returns the index of the child who sold the most candy bars
Part 2: Programming assignment
1. The problem
Given an array a with size N, and an element item, find the sum of all elements smaller than item. Print the array and the sum of the elements smaller than item.
In particular, the program has to:
a. Prompt the user to enter the array size and read an integer number N.
b. Declare an array a of integers of size N.
c. Prompt the user to enter the upper limit M of the numbers in array a and read it.
d. Fill array a with random non-negative integers less than M.
e. Prompt the user to enter the value of item and read it.
f. In a for loop, compute the sum of all elements smaller than item.
g. Print the elements of a[] with a title “Elements of array a”, ten elements per row (if the number of elements is greater than 10).
h. Print the computed sum
The program output should be like this (user input is underlined):
Please enter size of arrays: 55
Please enter upper limit: 100
Please enter item to be compared: 5
Elements of array a
59 43 81 56 3 32 29 10 89 78
35 96 56 53 53 88 82 18 56 61
12 51 30 64 72 6 86 90 30 55
49 34 4 86 94 53 63 85 42 68
50 34 2 25 54 37 87 83 53 62
92 60 32 98 19
The sum of elements less than 5 is 9
Another run (y/n)?
2. Implementation
2.1. Create a class Arrays. This class will be used in further assignments.
2.1. 1. Within the Arrays class write a method
public static void createArray(int[] array, int size, int upper)
This method will fill an array of the specified size with random numbers less than the specified upper limit.
The body of the method has to contain:
Declaration of an object of type Random
A for loop that stores a random number less than upper in each subsequent element of the array
2.1. 2. Within the Arrays class write a method
public static void printArray(int [] array, int perRow)
This method will print the elements of the array in rows, separated by a space, each row except possibly the last row contains perRow elements.
The print can be done in a for loop running up to the size of the array, with inside check whether perRow elements have been already printed (you have to figure out how to accomplish this check). If not, use System.out.print(…) statement. If yes, print a new line with System.out.println();
2.1. 3. Within the Arrays class write a method
public static int partialSum(int[] array, int item)
This method computes and returns the sum of all elements smaller than item.
2.2. Create an ArrayDriver class that will contain the main method. The main method does the following:
a. prints a welcome message
b. prompts the user to enter the size of the array and reads it.
c. prompts the user to enter the upper limit for the array elements and reads it.
d. prompts the user to enter the item for comparison and reads it
e. declares an array with size as given in step (b)
f. calls the createArray method to create the array.
g. calls the partialSum method to compute the required sum
h. calls the printArray method with second actual parameter equal to 10
i. prints the computed sum
j. repeats steps (b) to (i) upon user request.
3. Programming hints
3.1. Usually the for loops will run up to the size of the array. When the size is not given as a parameter, it can be found using the expression array.length, where “array” is the name of the array .
If a given method has array as a formal parameter, to get the size we use the following:
int size;
size = array.length;
or int size = array.length;
Note that there are no ( ) at the end of the expression.
3.2. To create a random number less than upper:
Declare
Random rnum = new Random();
Use the method int rnum.nextInt(int upper) in an assignment statement, with the left side being an array element.
e.g. temp = rnum.nextInt(upper);
Note that this example does not store the random number in an array element.
3.3. To repeat steps upon user request
use do-while loop with loop variable answer
String answer;
do
{
// ask for another run and read the answer in answer
}
while(answer.equals(“y”));
3.4. To call a method defined in another class, which does not create objects
The Arrays class does not have private variables, and hence we do not create objects of type Arrays. Instead we use the Arrays class as a collection of array methods. To call a method defined in the Arrays class, we need to write the name of the class in front of the name of the method. For example the statement:
Arrays.createArray(arrayA, size, upper);
will call the method to fill arrayA with random numbers less than upper
The statement:
sum = Arrays.partialSum(arrayA, item);
will call the method partialSum and will store the returned value in the variable sum. Note that sum has to be declared as int, because the type of the returned value of the method partialSum is int.
Turn in: the answers to the questions as a Word file (the name has to start with your name), the programming assignment as zipped project (do not forget to change the extension to .zi). The project name has to start with your name.
3