Results 1 to 4 of 4
  1. #1
    samtbryant is offline Sam032
    Join Date
    Feb 2017
    Location
    Indiana
    Posts
    2
    Rep Power
    0

    Default Incorrect Second Constructor with Array class.

    I just started in my java class and I am struggling with one section of the assignment. The instructions say:

    "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."

    However, I am not sure what this means. I have the rest of the work done but unsure what is expected here. My code is below, any help is appreciated. Thank you in advance.


    Java Code:
    import java.util.Random;
    public class Arrays
    {
    	//Instance Variables
    	private int array;
    	private int count;
    
    	//Constructors
    	public Arrays(int size)
    	{
    		int[] array = new int[size];
    		count = size;
    
    
    		Random rand = new Random();
    		for(int i = 0; i < count; i++)
    		{
    			array[i] = (rand.nextInt(10));
    		}
    	}
    
    	public Arrays(int[] arr)
    	{
                    //This is where I am having issues. It says set array variable equal to arr.
    		Arrays arr = new Arrays[array];
    		count = arr.length;
    
    	}
    
    	//findMin
    	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
    	public int findMax()
    	{
    		int max = array[0]; //Set max to the first element.
    		for(int i = 1; i < count; i++)
    		{
    			// Reassign max if there is a larger element.
    			if(array[i] > max)
    			{
    				max = array[i];
    			}
    		}
    		return max; // Return the largest element.
    	}
    
    	//calcSum
    	private int calcSum()
    	{
    		int sum = 0;
    		for(int i = 0; i < count; i++)
    		{
    			sum = array[i] + sum;
    		}
    		return sum;
    	}
    
    	//calcAverage
    	//toString
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Incorrect Second Constructor with Array class.

    First, you're array declaration is incorrect on line 5. It should be an empty array.
    Second, on line 11 don't re-declare the array, just use line 5.

    Now to answer your question, you're already passing an array to your constructor. So just
    assign it to the array variable in your class.


    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Incorrect Second Constructor with Array class.

    I don't understand line 25. Shouldn't it be more like "the property "array" for this instance of the class represents whatever has been passed into the constructor"? I would also change line 26 to array.length.

    Edit: Jim gave you the answer already. Sorry for the duplicate. Being able to read clearly helps.
    Last edited by benji2505; 02-24-2017 at 10:55 PM.

  4. #4
    samtbryant is offline Sam032
    Join Date
    Feb 2017
    Location
    Indiana
    Posts
    2
    Rep Power
    0

    Default Re: Incorrect Second Constructor with Array class.

    Thanks for the assist everyone. Much appreciated.

Similar Threads

  1. array copy in class constructor.
    By Juukamen in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 12:07 AM
  2. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  3. Class problem: getting incorrect output
    By mgm2010 in forum New To Java
    Replies: 9
    Last Post: 04-30-2009, 07:17 AM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •