Write a collection of little functions

Assignment Help Other Subject
Reference no: EM132239177

Assignment 5: Integer Array Class

This assignment asks you to write a collection of little functions that all operate on an array of integers.

In your test class, declare and initialize two literal integer arrays in main(), called sampleArray1 and sampleArray2 with the following values: int[] sampleArray1 = {4,7,9,3,2,8,15,6,8,7}; int[] sampleArray2 = {12,6,4,8,3,7,11,1,6}; You will use these arrays for testing your class later on.

Implement a class called IntegerArray, that has only one variable in the class which is an array of ints, called arr.

In your main function, create one or more instances of your IntegerArray class and call each of the following methods as you implement them. For methods that return a value, the code in main() should print the result of the call. In the IntegerArray class, only the printLiteral method should include calls to System.out.

1. Implement a constructor that takes the length and create an array of that length, but does not fill it. (See 2, below, for how to fill it.)

2. Implement a second constructor that takes an array of ints as an argument and uses it to initialize the array of the class (kind of like a setter). The kind of copying to use here is called a "shallow copy". Use the two literals, sampleArray1 and sampleArray2, described in the first paragraph above to test this constructor.

3. Create a function called printLiteral() that prints the array to the console in a form that looks exactly like the literal strings use above for initialization. The literal starts with a "{", then lists the numbers separated by commas but no spaces, and ending with "}". The output must even work if the length of the array is 0 ("{}") or 1 (no commas).

4. Create a function called sumOfArray that returns the sum of the values in the array.

5. Create a function called maxInArray that returns the maximum value in the array.

6. Create a function called minInArray that returns the minimum value in the array.

When looking for min or max, compare each new value to the min or max value that you have seen so far. Do not initialize that value to an arbitrary extreme (like 0). Observe that when you check the first value of the array, that value should become the min or max you have seen so far, since you haven't seen anything else. Assuming that you are using a variable called minSeen, the correct solution is to initialize minSeen to the first value right away, before starting the loop. That way you don't have to know anything about the range of possible values (which could be all negative, making 0 a bad choice for the initial value.)

7. Create a function called rangeInArray that returns the range of the values in the array. In math, the range is the magnitude of the difference between the minimum and maximum (inclusive). You compute it by subtracting the minimum from the maximum. You already have methods that return the min and max. Use them and do not include any loops in this method.

8. Create a function called mean() that returns the average value in the array. Even though the array contains ints, return the average as a double. Note that you have a method for the sum, and you can get the length by using the array property of the array itself. So this method only has to do a divide. Test that you can get a result that includes a fractional part (not just whole numbers). You may need to look up "cast" to see how to make the division not do an integer divide.

In the average and range functions, use the sumOfArray, minInArray, and maxInArray functions to compute those parts, rather than repeating the work of computing the sum, or looking for min and max.

9. Create a function called clip that takes a maximum value as an argument and changes any value in the array that is higher than the specified maximum value to be the same as the maximum value. This function could also be called "haircut", in that it takes values that are too high, and cuts them down to the maximum allowable height. (Think of a scissor going through your hair and trimming the ones that are too long.) If the array is {2, 4, 6, 7, 3}, after clip(5) it should be {2, 4, 5, 5, 3}.

When calling the clip() function from main(), call the print() function before and after to show the effect of clipping. Do not print values inside the clip method.

If Array is empty, The min and max methods should just return 0.

Reference no: EM132239177

Questions Cloud

Discuss what is required to really make visualizations work : This week we will discuss what is required to really make visualizations work. Berinato (2016) notes that the nature and purpose of your visualizations.
Calculate the ph of a buffer solution : CHEM 2404 - Environmental Chemistry - Cape Breton University - what species dominates at the typical pH encountered in natural water sources
Briefly describe basic activities in the incident response : Chapter 12 in our text discusses incident response procedures. There are a number of key steps necessary to effectively handle an incident.
Briefly explain the genesis account of creation : Briefly explain the Genesis account of creation. Explain and support your interpretation of the origin of the universe.
Write a collection of little functions : This assignment asks you to write a collection of little functions that all operate on an array of integers. In your test class, declare and initialize two.
Was it an effective way to learn about the characters : Identify at least 1 or more literary tactic that the author used to open the book. Was it effective? Why or Why not?
Write a brief discussion describing the terrorism attack : Write a brief Discussion describing the terrorism attack and it's aftermath. Comment on ways the attack could have been prevented.
Why management opt for other than most economical choice : Teloxy Engineering has received a one-time contract to design and build 10,000 units of a new product. During the proposal process, management felt.
Reflect on different ways you can approach the issue : Write a 500-word essay in which you consider a communication goal that you hope to accomplish in the near future. How will you accomplish it?

Reviews

Write a Review

Other Subject Questions & Answers

  Define important characteristic of communities and biomes

Biodiversity is an important characteristic of communities and biomes. Efforts to preserve biodiversity, though intended to reduce human influence.

  What large- or small-scale revisions might improve the code

Identify a professional association that oversees people engaged in your chosen vocation, and locate the formal code of conduct statement .

  Identity an elderly person in your community or locality

What are the factors that contribute to the elderly facing the above-mentioned limitations?

  Discuss about the eriksons psychosocial theory

Choose one contemporary issue or problem related to developmental psychology that is prevalent in today's news and research a minimum of one peer-reviewed.

  Write a essay in which you compare and contrast influence

write a 350-400 word essay in which you compare and contrast the influence of heredity and hormones on human

  Leaders of a quantum healthcare organization

Describe the main tasks of leaders of a quantum healthcare organization. Describe how you can incorporate these criteria as a nursing leader in an Aids clinic and as a Nurse executive in the future.

  Describe how the concepts of leadership and management

discussion questionsbased on the scientific management theory what are some of the routines in health care that seem to

  Describe your feelings and attitudes about the meeting

Paper should describe your feelings, thoughts, and attitudes about the meeting (2-3 typed pages) 0 plagiarism, double space, the site where the meeting.

  How have intelligence-sharing systems changed

What are the intelligence-gathering tools being used in the United States to fight terrorism - intelligence sharing between different law enforcement agencies

  How positive collaborative relations promote social growth

In a 1,000-1,250-word paper, describe how positive, collaborative relationships with families promote the intellectual, social, emotional, and physical growth of children.

  Create a one-day training course highlighting

You have just completed 10 weeks of a computer and interaction design course. Imagine you have been asked to create a one-day training course highlighting the important elements of what you have just learned in the past 10 weeks

  Explain the each stage of the engineering design process

In an interview with a product design firm, the senior engineer asks you to briefly discuss each stage of the engineering design process and why each process is critical to the development of a finished product.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd