CSE 110 Assignment 7 Due Wednesday, April 13 By 10:00 202681
Cse 110 Assignment 7due Wednesday April 13 By 1000pm
CSE 110 - ASSIGNMENT # 7 Due: Wednesday April 13 by 10:00PM Maximum Points: 20 pts What This Assignment Is About: Arrays (chapter 6) Your programming assignments require individual work and effort to be of any benefit. Every student must work independently on his or her assignments. This means that every student must ensure that neither a soft copy nor a hard copy of their work gets into the hands of another student. Sharing your assignments with others in any way is NOT permitted. Violations of the University Academic Integrity policy will not be ignored.
The university academic integrity policy is found at Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /------------------------------------------------------------------------- // AUTHOR: your name // FILENAME: title of the source file // SPECIFICATION: description of the program // YOUR Lab Letter and Name of the TA for your Closed lab // FOR: CSE 110- homework #- days and time of your class // TIME SPENT: how long it took you to complete the assignment //----------------------------------------------------------------------/ Part 1: Written exercises (7 pts) Note: The answers to the following questions should be typed in the block of comments in the Assignemnt7.java file.
Please make sure they're commented out (green). Type them neatly and make them easy to read for the graders. You will not receive any partial points for an incorrect answer. 1. Which of the following are valid array declarations? Explain your answers. (2 pts) a. char[] charArray = new char[26]; b. int[] words = new words[10]; c. char[] charArray = "Computer Science"; d. double[3] nums = {3.5, 35.1, 32.0}; Turki Saad Alshehri Turki Saad Alshehri Turki Saad Alshehri Turki Saad Alshehri 2. Consider the following method (5 pts) public static int mystery(int[] list) { int x =0; for (int i=1; i x) x =y; } return x; } What value does the method return when passed each of the following arrays? a. {5} b. {3, 12} c. {4, 2, 10, 8} d. {1, 9, 3, 5, 7} e. {8, 2, 10, 4, 10, 9} Part 2: Programming (13 points): Your assignment is to create a class called NumberCollection in a file called NumberCollection.java. (there is no main method in this class).
A class NumberCollection has two private data members: 1. an array of integers. The variable name for the array of integers is numberArray. (Declare the array as a private data member, no need to reserve space at the time of declaration) 2. a count (integer) as instance variables. The variable count keeps track how many integers are stored in the array. Note: You need to distinguish the array size (capacity) and "count" that keeps track of numbers added to this array so far. The class NumberCollection must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.) Method Description of the Method public NumberCollection(int arraySize) It constructs a NumberCollection object with an array capacity specified by the integer parameter "arraySize", i.e., you will now be reserving space for the private integer array declared above.
Hint: Do not re-declare the array. private int indexOf(int searchingNum) It returns the index of the number specified by the parameter is located. If the number is not found, it returns -1. It is a service (helper) method. public boolean addNumber(int numberToAdd) The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index. If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true. If the number already exists in the array, the new Save the NumberCollection class in a file called NumberCollection.java and use the following program stored in Assignment7.java, which has the main method to create new NumberCollection objects and to test your class. You do NOT need to modify Assignment7.java. The program will ask a user to enter a size for the array. Then it will show the following menu to a user: Command Options ----------------------------------- a: add an integer in the array b: remove an integer from the array c: display the array d: compute and display the range e: compute and display the average ?: display the menu again q: quit Then it will ask a user to enter one of the above commands. Based on the user's choice, the program needs to perform corresponding operation. This will be done by using a method you define in the NumberCollection class. The program will terminate when a user enters 'q'. Sample Output: (the inputs entered by a user are shown in red) Please enter a size for the array. 5 Command Options ----------------------------------- a: add an integer in the array b: remove an integer from the array number will not be added, and the method returns false. public boolean remove(int numberToRemove) The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and if it does, it removes the number and then it shifts all the integers to the left and returns true. Otherwise, it returns false. private void doubleArrayCapacity() It is a service (helper) method and doubles the capacity of the numberArray. Please see the example in page 353, the method increaseSize() as a reference. public int findRange() It finds the range of values in the array. The range is defined as 1 more than the difference between the maximum and minimum in the array. Hint: Create two helper/service (private) methods for finding the maximum and minimum. public int computeAvg() It computes and returns the average of numbers stored in the numberArray so far (at the time when this method is called.) If the array is empty, return 0. public String toString( ) Returns a String containing a list of numbers stored in the numberArray. An example of such string can be: {3, 6, -1, 3, 23, -50, 43} The string should start with a '{' and end with a '}'. Assignment7.java c: display the array d: compute the range e: compute and display the average ?: display the menu again q: quit this program Please enter a command or type ? a Please enter an integer to add. 1 1 successfully added. Please enter a command or type ? a Please enter an integer to add. 2 2 successfully added. Please enter a command or type ? a Please enter an integer to add. 3 3 successfully added. Please enter a command or type ? a Please enter an integer to add. 4 4 successfully added. Please enter a command or type ? a Please enter an integer to add. 6 6 successfully added. Please enter a command or type ? c {1, 2, 3, 4, 6} Please enter a command or type ? d The range is:6 Please enter a command or type ? f The average is: 3.2 Please enter a command or type ? r Invalid input! Please enter a command or type ? b Please enter an integer to remove. 3 3 successfully removed. Please enter a command or type ? c {1, 2, 4, 6} Please enter a command or type ? b Please enter an integer to remove. is not in the array. 40 was not removed. Please enter a command or type ? c {1, 2, 4, 6} Please enter a command or type ? a Please enter an integer to add. successfully added. Please enter a command or type ? a Please enter an integer to add. successfully added. Please enter a command or type ? a Please enter an integer to add. successfully added. Please enter a command or type ? c {1, 2, 4, 6, 10, 12, 14} Please enter a command or type ? q Helpful hints for doing this assignment: · work on it in steps – write one method, test it with a test driver and make sure it works before going on to the next method · always make sure your code compiles before you add another method · your methods should be able to be called in any order Submit your homework by following the instructions below: * Go to the course web site (my.asu.edu), and then click on the on-line Submission tab. Submit all two files: Assignment7.java, and NumberCollection.java on-line. Make sure to choose HW7 from drop-down box. Important Note: You may resubmit as many times as you like until the deadline, but we will only mark your last submission. NO LATE ASSIGNMENTS WILL BE ACCEPTED