SlideShare a Scribd company logo
1 of 11
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
http://www.asu.edu/studentlife/judicial/integrity.html
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};
http://www.asu.edu/studentlife/judicial/integrity.html
Turki Saad Alshehri
Turki Saad Alshehri
Turki Saad Alshehri
Turki Saad Alshehri
http://www.eas.asu.edu/~csedept/
2. Consider the following method (5 pts)
public static int mystery(int[] list)
{ int x =0;
for (int i=1; i< list.length; i++)
{ int y = list[i] -list[0];
if (y > 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.
40
40 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.
10
10 successfully added.
Please enter a command or type ?
a
Please enter an integer to add.
12
12 successfully added.
Please enter a command or type ?
a
Please enter an integer to add.
14
14 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
http://my.asu.edu/

More Related Content

Similar to CSE 110 - ASSIGNMENT # 7 Due Wednesday April 13 by .docx

C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
maxinesmith73660
 

Similar to CSE 110 - ASSIGNMENT # 7 Due Wednesday April 13 by .docx (20)

object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Arrays
ArraysArrays
Arrays
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Java Lab Manual
Java Lab ManualJava Lab Manual
Java Lab Manual
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
Arrays
ArraysArrays
Arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Lazy Java
Lazy JavaLazy Java
Lazy Java
 
Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018Mario Fusco - Lazy Java - Codemotion Milan 2018
Mario Fusco - Lazy Java - Codemotion Milan 2018
 
Lazy java
Lazy javaLazy java
Lazy java
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Ch08
Ch08Ch08
Ch08
 
Compiler lab final report writing
Compiler lab final report writingCompiler lab final report writing
Compiler lab final report writing
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
CS301-lec01.ppt
CS301-lec01.pptCS301-lec01.ppt
CS301-lec01.ppt
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docxConsider this code using the ArrayBag of Section 5.2 and the Locat.docx
Consider this code using the ArrayBag of Section 5.2 and the Locat.docx
 

More from aryan532920

According to the NASW Code of Ethics section 6.04 (NASW, 2008), .docx
According to the NASW Code of Ethics section 6.04 (NASW, 2008), .docxAccording to the NASW Code of Ethics section 6.04 (NASW, 2008), .docx
According to the NASW Code of Ethics section 6.04 (NASW, 2008), .docx
aryan532920
 
According to the text, crime has been part of the human condition si.docx
According to the text, crime has been part of the human condition si.docxAccording to the text, crime has been part of the human condition si.docx
According to the text, crime has been part of the human condition si.docx
aryan532920
 
According to Ronald Story and Bruce Laurie, The dozen years between.docx
According to Ronald Story and Bruce Laurie, The dozen years between.docxAccording to Ronald Story and Bruce Laurie, The dozen years between.docx
According to Ronald Story and Bruce Laurie, The dozen years between.docx
aryan532920
 
According to the Council on Social Work Education, Competency 5 Eng.docx
According to the Council on Social Work Education, Competency 5 Eng.docxAccording to the Council on Social Work Education, Competency 5 Eng.docx
According to the Council on Social Work Education, Competency 5 Eng.docx
aryan532920
 
According to the Council on Social Work Education, Competency 5.docx
According to the Council on Social Work Education, Competency 5.docxAccording to the Council on Social Work Education, Competency 5.docx
According to the Council on Social Work Education, Competency 5.docx
aryan532920
 

More from aryan532920 (20)

According to the NASW Code of Ethics section 6.04 (NASW, 2008), .docx
According to the NASW Code of Ethics section 6.04 (NASW, 2008), .docxAccording to the NASW Code of Ethics section 6.04 (NASW, 2008), .docx
According to the NASW Code of Ethics section 6.04 (NASW, 2008), .docx
 
According to the text, crime has been part of the human condition si.docx
According to the text, crime has been part of the human condition si.docxAccording to the text, crime has been part of the human condition si.docx
According to the text, crime has been part of the human condition si.docx
 
According to Ronald Story and Bruce Laurie, The dozen years between.docx
According to Ronald Story and Bruce Laurie, The dozen years between.docxAccording to Ronald Story and Bruce Laurie, The dozen years between.docx
According to Ronald Story and Bruce Laurie, The dozen years between.docx
 
According to Kirk (2016), most of your time will be spent work with .docx
According to Kirk (2016), most of your time will be spent work with .docxAccording to Kirk (2016), most of your time will be spent work with .docx
According to Kirk (2016), most of your time will be spent work with .docx
 
According to the Council on Social Work Education, Competency 5 Eng.docx
According to the Council on Social Work Education, Competency 5 Eng.docxAccording to the Council on Social Work Education, Competency 5 Eng.docx
According to the Council on Social Work Education, Competency 5 Eng.docx
 
According to Kirk (2016), most of our time will be spent working.docx
According to Kirk (2016), most of our time will be spent working.docxAccording to Kirk (2016), most of our time will be spent working.docx
According to Kirk (2016), most of our time will be spent working.docx
 
According to Kirk (2016), most of your time will be spent working wi.docx
According to Kirk (2016), most of your time will be spent working wi.docxAccording to Kirk (2016), most of your time will be spent working wi.docx
According to Kirk (2016), most of your time will be spent working wi.docx
 
According to Davenport (2014) the organizational value of healthcare.docx
According to Davenport (2014) the organizational value of healthcare.docxAccording to Davenport (2014) the organizational value of healthcare.docx
According to Davenport (2014) the organizational value of healthcare.docx
 
According to the authors, privacy and security go hand in hand; .docx
According to the authors, privacy and security go hand in hand; .docxAccording to the authors, privacy and security go hand in hand; .docx
According to the authors, privacy and security go hand in hand; .docx
 
According to Gilbert and Troitzsch (2005), Foundations of Simula.docx
According to Gilbert and Troitzsch (2005), Foundations of Simula.docxAccording to Gilbert and Troitzsch (2005), Foundations of Simula.docx
According to Gilbert and Troitzsch (2005), Foundations of Simula.docx
 
According to Klein (2016), using ethical absolutism and ethical .docx
According to Klein (2016), using ethical absolutism and ethical .docxAccording to Klein (2016), using ethical absolutism and ethical .docx
According to Klein (2016), using ethical absolutism and ethical .docx
 
According to Franks and Smallwood (2013), information has become.docx
According to Franks and Smallwood (2013), information has become.docxAccording to Franks and Smallwood (2013), information has become.docx
According to Franks and Smallwood (2013), information has become.docx
 
According to the Council on Social Work Education, Competency 5.docx
According to the Council on Social Work Education, Competency 5.docxAccording to the Council on Social Work Education, Competency 5.docx
According to the Council on Social Work Education, Competency 5.docx
 
According to the authors, privacy and security go hand in hand; and .docx
According to the authors, privacy and security go hand in hand; and .docxAccording to the authors, privacy and security go hand in hand; and .docx
According to the authors, privacy and security go hand in hand; and .docx
 
According to recent surveys, China, India, and the Philippines are t.docx
According to recent surveys, China, India, and the Philippines are t.docxAccording to recent surveys, China, India, and the Philippines are t.docx
According to recent surveys, China, India, and the Philippines are t.docx
 
According to the authors, countries that lag behind the rest of the .docx
According to the authors, countries that lag behind the rest of the .docxAccording to the authors, countries that lag behind the rest of the .docx
According to the authors, countries that lag behind the rest of the .docx
 
According to Peskin et al. (2013) in our course reader, Studies on .docx
According to Peskin et al. (2013) in our course reader, Studies on .docxAccording to Peskin et al. (2013) in our course reader, Studies on .docx
According to Peskin et al. (2013) in our course reader, Studies on .docx
 
According to Franks and Smallwood (2013), information has become the.docx
According to Franks and Smallwood (2013), information has become the.docxAccording to Franks and Smallwood (2013), information has become the.docx
According to Franks and Smallwood (2013), information has become the.docx
 
According to Ang (2011), how is Social Media management differen.docx
According to Ang (2011), how is Social Media management differen.docxAccording to Ang (2011), how is Social Media management differen.docx
According to Ang (2011), how is Social Media management differen.docx
 
According to (Alsaidi & Kausar (2018), It is expected that by 2020,.docx
According to (Alsaidi & Kausar (2018), It is expected that by 2020,.docxAccording to (Alsaidi & Kausar (2018), It is expected that by 2020,.docx
According to (Alsaidi & Kausar (2018), It is expected that by 2020,.docx
 

Recently uploaded

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 

Recently uploaded (20)

B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 

CSE 110 - ASSIGNMENT # 7 Due Wednesday April 13 by .docx

  • 1. 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 http://www.asu.edu/studentlife/judicial/integrity.html Assignments Documentation: At the beginning of each programming assignment you must have a comment block with the following information: /*------------------------------------------------------------------------ -
  • 2. // 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}; http://www.asu.edu/studentlife/judicial/integrity.html Turki Saad Alshehri
  • 3. Turki Saad Alshehri Turki Saad Alshehri Turki Saad Alshehri http://www.eas.asu.edu/~csedept/ 2. Consider the following method (5 pts) public static int mystery(int[] list) { int x =0; for (int i=1; i< list.length; i++) { int y = list[i] -list[0]; if (y > 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
  • 4. 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)
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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. 40 40 is not in the array. 40 was not removed. Please enter a command or type ? c {1, 2, 4, 6}
  • 10. Please enter a command or type ? a Please enter an integer to add. 10 10 successfully added. Please enter a command or type ? a Please enter an integer to add. 12 12 successfully added. Please enter a command or type ? a Please enter an integer to add. 14 14 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
  • 11. 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 http://my.asu.edu/