Home > Enterprise >  How would I create a method that stores the parameter value in an array at the index of the position
How would I create a method that stores the parameter value in an array at the index of the position

Time:05-28

I have an assignment to create an array class where there are 2 constructors where each constructor sets a different size for the array.

The array is already an instance variable along with another instance variable to keep track of the current position in the array.

I have to create a method called add with an integer parameter that will store the parameter value in the array at the index of the position variable, then add 1 to the position variable. If the incremented position variable is outside the bounds of the array, the method calls the addspace method.

The addspace method creates a new array 25% larger than the instance variable array, copies all the values of the instance array to the new array, and assigns the new array to the instance variable.

I also need a method called size that will return the value in the position variable and a method called get that with 1 parameter(an index), the method returns the value at the parameter index.

The last thing I need is a print method that uses a for loop to print the values in the array.

So far this is what I have

public class ArrayClass
{
    private int array[];
    private int x=0;

    public ArrayClass()
    {
        this.array= new int[10];
        add(1);
        getThat(0);
        print();
    }

    public ArrayClass(int y)
    {
        this.array= new int[y];
        add(2);
        getThat(0);
        print();
    }

    public void add(int a)
    {
        array[x]=a;
        x  ;
        if(x>array.length)
            addspace();
    }

    public void addspace()
    {
        double d=array.length (array.length*0.25);
        int v=(int)d;
        int newArray[]= new int[v];
        for(int i=0; i<array.length; i  )
        {
            newArray[i]=array[i];
            System.out.println(newArray[i]);
        }

    }
    public int size()
    {
        return x;
    }
    public int getThat(int index)
    {
        return array[index];
    }

    public void print()
    {
        for(int i=0; i<array.length; i  )
            System.out.println(array[i] " ");
    }
    public static void main(String[] args)
    {
        new ArrayClass();
        new ArrayClass(5);
    }
}

I know the title only asks for help with the first method but if someone would be kind enough to help with the other methods and the reason why my code won't run and print what I want it to that would be much appreciated.

CodePudding user response:

Use the ArrayClass for only for declaring your functionality.Call add method as obj.add(number) until and unless you need to add something inside ArrayClass constructor itself.

Modified these things as per my understanding

  1. In your add method you are assigning the value first and then adding space if the array is full, in this case, you are increasing the size even if it might not be needed (i.e not calling add method again). Instead of this increase the size only when you require it.
  2. In print function you are iterating through the whole array.Modified to-> it will iterate till the last index of value (i.e x)
package com.example;
public class ArrayClass
{
    private int array[];
    private int x=0;
    private final int DEFAULT_SIZE=4;

   public ArrayClass(){
    this.array = new int[DEFAULT_SIZE];
   }

   public ArrayClass(int size){
        this.array = new int[size];
   }

   public void add(int number){
       //check whether array have space or not .if not then increase the space.
       if(x > this.array.length-1){
           addSpace();
       }
       array[x] =number;
       x  ; 
   }

   private void addSpace(){
        double newSize = array.length   array.length * 0.25;
        int tempArray[] = new int[(int) newSize];
        for(int i=0; i<array.length; i  ){
            tempArray[i]=array[i];            
        }
        this.array = tempArray;
   }


   public int size()
    {
        return x;
    }
    public int getThat(int index)
    {
        return array[index];
    }

    public void print()
    {   
        //instead of of printing the whole array Printed till last value index.
        for(int i=0; i<x; i  )
            System.out.println(array[i] " ");
    }

}

From the main method

    ArrayClass ac1 = new ArrayClass();
    ac1.add(5);
    ac1.add(4);
    ac1.add(5);
    ac1.add(4);
    ac1.add(7);
    ac1.add(19);
    ac1.print();
    ArrayClass ac2 = new ArrayClass(5);
    ac2.add(1);
    //rest of your function call here 

  • Related