HW 09 due 03/24

advertisement
CmSc150 Fundamentals of Computing I
Homework 09 due 03/24
Programming assignment
1. Modify the ArrayDriver class so that
a. it prints a menu of options corresponding to the methods developed in the
Arrays class,
b. reads the user choice
c. performs the corresponding actions (use a switch statement to implement
each choice)
d. performs steps a, b, and c until the user enters option ‘exit’.
Do not include in the menu the tasks to create an array and to print an array.
These will be part of each of the other processing tasks.
Currently, your Arrays class should contain the methods createArray,
printArray, and partialSum.
The menu should look like this:
Choose an option
1. PartialSum
2. Exit
Each option (except Exit) will require specific dialog to enter the necessary
parameters. For example, the dialog in case of partialSum will ask the user to
enter the size of the array, the upper limit and the item for comparison.
In the switch statement provide for recognizing invalid input. This would be the
default case.
2. Modify the printArray method so that it takes as a parameter the number of the
elements to be printed:
void printArray(int[] array, int elements , int perRow)
If we want to print the entire array, the call statement will have elements to be the
same as the size of the array. In many tasks however the array is not completely full, so
we want to print only the relevant elements, e.g. when we copy in another array some of
the elements, then we want to print only the copied elements.
1
3. Write a method int sieveItem(int[] arrayA, int[] arrayB, int item)
that copies into arrayB all elements from arrayA that are less than item, and
returns the number of copied elements.
Include in the menu of options a new option “sieve”.
In the corresponding case of the switch statement implement the following:
a. Ask the user and read the size of the array, the upper limit of elements,
and the value of item.
b. Declare two arrays (arrayA and arrayB) and call createArray method
to create arrayA.
c. Call sieveItem method to perform the copying of all elements less than
item into arrayB.
d. Print arrayA 10 elements per row
e. If the number of copied elements Is greater than 0, print arrayB 10
elements per row; otherwise print a message – “no elements are less
than” and then print the value of item .
4. Write a method void reverse(int[] array) that reverses the elements of the
array.
Include in the menu of options a new option “reverse”.
In the corresponding case of the switch statement implement the following:
a. Ask the user and read the size of the array and the upper limit of
elements.
b. Declare the array and call createArray method to create it.
c. Print the created array10 elements per row
d. Call the reverse method.
e. Print the reversed 10 elements per row
5. Write a method int evenElementsSum(int[]array) that computes and returns
the sum of all even elements in the array.
Include in the menu of options a new option “sum of even elements”.
In the corresponding case of the switch statement implement the following:
a. Ask the user and read the size of the array and the upper limit of
elements.
b. Declare the array and call createArray method to create it.
c. Call the evenElementsSum method.
2
d. Print the array 10 elements per row
e. Print the sum of the even elements
6. Write a method void printLeftA(int n) that prints n rows on the following
way:
1 2 3 4 …. n-1
n
1 2 3 4 …. n-1
…..
1 2 3
1 2
1
For example the call printLeft(5) will output
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Include in the menu of options a new option “print left (version A)”.
In the corresponding case of the switch statement implement the following:
a. Ask the user and read the upper limit (the number n)of the elements to be
printed
b. Call the printLeftA method.
Programming hints
1. After each task compile and run, to make sure you have finished the task
successfully.
2. The do-while loop will run while the choice of option is not ‘exit’. Thus we don’t
need to ask the user for another run.
3. You may get an error message ‘array has already been declared in main”
3
This will show if two cases contain the statement:
int[] array = new array[size];
You would like to have the declaration of the array outside the do-while loop,
however at that point we don’t know the size of the array.
To solve this problem, we can split the statement
int[] array = new array[size];
in two statements:
a. int [] array; this will go before the do-while loop
b. array = new int[size]; this statement may be repeated as necessary in the cases
of the switch statement.
4. The exit option should be listed last. If you keep the choices as sequential
numbers, after implementing each new choice you will have to change the
number for the exit choice and also the condition of the do-while loop. It is wise
to have initially a larger number for the exit option, e.g. 10, and after everything is
implemented to change 10 to an appropriate number (for this assignment it would
be 6, because you have to implement 4 methods, one has already been
implemented, so all choices would be 6).
5. Make sure that all prompts come on a new line
Turn in: the zipped project (do not forget to change the extension to .zi). The project
name has to start with your name.
4
Download