Write a for loop to iterate through all the elements

Assignment Help Computer Engineering
Reference no: EM132088459

Part 1: Instance Variables for Arrays

The first thing to do for the Arrays class is to set up its instance variables. Declare the following (private) instance variables:

• An int array called array  this will be the array we will be writing methods for.

• An int called count - this represents the number of valid elements in the array.

Part 2: Constructors for Arrays

The Arrays class will have two constructors. The first constructor takes the maximum size of the array as input as a parameter and initializes the array instance variable appropriately. It also sets count to size.

Finally, it will initialize all of the array elements to some values between 0 and 10, inclusive. To create this constructor, follow these steps:

Import java.util.Random to make use of the random number generator.

Create a constructor with the following header: public Arrays(int size)

Initialize your array variable and set its size to size (see the chart on page 252 for reference on initializing arrays). Be very careful that you are setting the value of your array instance variable, as opposed to creating a new variable called array.

Set the value of the count variable to size because we will be populating the entire array.

Copy the following code to the constructor in order to generate random values between 0 and 10,

inclusive:

Random rand = new Random(); for (int i = 0; i < count; i++) { array[i] = (rand.nextInt(10)); }
Next, create another constructor with the following header: public Arrays(int[] arr). This constructor will initialize the class by using the passed arr argument in order to fill its instance variables. The following things need to be done inside of this constructor:

• Set the array variable equal to arr.
• Set the count variable equal to the length of the array.

Part 3: Displaying the Output findMin()

The first method of this class will search the array for the minimum element. Copy the following code for the findMin method. Note how the count instance variable is used instead of array.length. This is just in case the entire array is not being used (it will be in our case, though).

public int findMin() { int min = array[0]; // Set min to the first element for (int i = 1; i < count; i++) { // Reassign min if there is a smaller element if (array[i] < min) { min = array[i]; }
}

return min; // Return the smallest element }
findMax()

Using the above code as reference, write a method which finds the maximum element within the array. You can refer to page 259 if you are stuck.

calcSum()

The calcSum() method will be a private method (for clarity, it is the only private method in this class). It will be used later on within the class as a helper method, but never outside of the class. This method will return the sum of all of the elements in the array. You can use the following steps as guidelines for how to complete this method, if you choose:

Use the following as the header: private int calcSum()

Declare an int variable for the sum - initialize it to 0

Write a for loop to iterate through all the elements in the array (remember to use the count instance variable instead of the length of the array).

Add the value of each element to the sum.

Return the sum after the for loop You can refer to page 259 if you are stuck.

calcAverage()

This method will return the average of all of the elements in the array. Use the following for the header of this method: public double calcAverage(). IMPORTANT: Use the calcSum() private method in your computation of the average for full credit. Hints:

• You will need to make use of the total elements in the array.
• You will need to cast the result to a double at the appropriate time to achieve an accurate average.

toString()

The toString() method is called whenever an object is passed into a print statement. This particular toString() method will print the following, assuming the array consists of elements {1, 2, 3, 4}:

[ 1, 2, 3, 4 ]

Copy the following code to use for this method:

public String toString() { String output = "[ "; for (int i = 0; i < count; i++) { output += array[i]; if (i != count - 1) { output += ", "; }
}

return output + " ]"; }
Part 4: Test Class for Arrays

At this point, the Arrays class is completed. The next step is to create a driver class to test it. This is the Lab8.java file that you created at the beginning of the lab. Copy the following code into the main method of the Lab8.java file. This code tests the first constructor of the Arrays class along with all its methods.

// Create an Arrays object using the first constructor Arrays arr1 = new Arrays(5); // Print the contents of the array in arr1 System.out.println(arr1); // Call findMin, findMax, and calcAverage on arr1 and print their values System.out.println("Min: " + arr1.findMin()); System.out.println("Max: " + arr1.findMax()); System.out.println("Average: " + arr1.calcAverage()); System.out.println();
The next step is to add code which tests the second constructor of the Arrays class. To do this, complete the following tasks by adding code to the end of the code you just copied into the main method.

Create an int array of length 3 - explicitly set its values to any 3 ints by using an array initialization list. See the chart on the bottom of page 252 for a reference on how to set an array with initial values.

Create an Arrays object using the second constructor. Note that this involves passing the array variable you just created. Call this object arr2.

Print the arr2 object by passing it into a println statement.

Print the min, max, and average of the arr2 object, as what was done with the arr1 object.

Reference no: EM132088459

Questions Cloud

Provide an environment for immune responses to take place : What are the structures that filter lymph and provide an environment for immune responses to take place? couldn't find answer anywhere for question
How does the amount of oxygen bond : How does the amount of oxygen bond to HB vary with the amount of oxygen in human blood, such as in the partial pressure exerted by oxygen?
Create a java program that interviews the users : Create a Java program that interviews the users about their personal information, how many jobs they have and what the income is from each of their jobs.
Vessel that receives lymph from the thoracic duct : What is the blood vessel that receives lymph from the Thoracic Duct? and the Right Lymphatic Duct?
Write a for loop to iterate through all the elements : The first method of this class will search the array for the minimum element. Copy the following code for the findMin method.
What are the structures that filter lymph : What are the structures that filter lymph and provide an environment for immune responses to take place? Couldn't find answer anywhere for question
Immune cells designed to remove foreign materials : What are immune cells designed to remove foreign materials from the lymph before being returned to the cardiovascular system?
What is parallel computing : Describe what is essential; cite sources, where you found your information; qualify the type of problem that lends itself to parallel computing;
Immune response involving the nonspecific : So with immune response involving the nonspecific response and immune response. So I need help in order to describe how the three levels of nonspecific

Reviews

Write a Review

Computer Engineering Questions & Answers

  Problem on human computer interface

You are booking a flight from Port of Spain Trinidad to VC Bird airport in Antigua. You will be using LIAT and Caribbean Airlines websites to do so and noting a few comparisons along the way.

  Show a letter grade based upon the average

In this script, convert pseudocode that uses a decision structure into a shell script. The script then will accept three grades, calculate an average, and then display a letter grade based upon the average.You will need to use either the if statem..

  Predict the future role of the communication technology you

select one 1 of the following topics in which you will base your responses in the form of a term papernetwork

  How common are trojans on computer systems

How does a Trojan enable malware to propagate? How common are Trojans on computer systems? Or on mobile platforms? What is a logic bomb?

  Questiontvbca has just unavailable and old historic

questiontvbca has just unavailable and old historic building in downtown pittsburgh in which 15 employees will work.

  Develop procedures for generating the description

Develop procedures for generating the description of I three-dimensional object using input paramekrs that define the object in terms of a rotational sweep.

  Write a recursive function called sumdigits

Write a recursive function called sumDigits with the following signature: public static long sumDigits(long n).

  What is the largest positive number

What is the largest positive number one can repent in an 8-bit 2's complement code ? write your result in binary and decimal.

  Develop the natural shape functions for a linear element

Using the natural coordinate system shown in the accompanying figure. Develop the natural shape functions for a linear element.

  Create a three-column chart

reate a three-column chart in which the first column lists nonmanagement skills you have. Are you good at travel? Do you know how to build furniture?

  Coordinates of two points

For each of the following sets, give the coordinates of two points where Pand Qare in the set, but the line from Pto Qgoes outside the set. For example, if the points are (1,2)and (3,4),enter in the format (1,2),(3,4)

  Discuss the header elements of the different messages

Find the "Received:" field in the headers and write down as many DNS names or IP addresses as you can

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