Home > other >  function execution in java program
function execution in java program

Time:05-05

In this code, we have to add a method set() which will take two parameters, an (int) index, and an Integer value, which will set the array element at the specified index to the specified value and the array will grow if an attempt is made to set a value beyond the end of the array.

public class MyVector {
    protected Integer[] internalArray;
    public MyVector( int initialSize )
    {
        internalArray = new Integer[initialSize];
    }
    public void resize( int newSize )
    {
        if ( newSize != internalArray.length ) {
        Integer[] newArray = new Integer[newSize];
        for (int i=0; i < Math.min(newSize,internalArray.length); i  )
            newArray[i] = internalArray[i];
        internalArray = newArray;
        }
    }
    public Integer get( int index )
    {
        if ( index < internalArray.length )
            return internalArray[index];
        return null;
    }
    public static void main(String[] args)
    {
        MyVector vec = new MyVector(5);
        for ( int i = 0 ; i < 20 ; i   )
            vec.set(i, i*i );
        for ( int i = 0 ; i < 20 ; i   )
            System.out.println( "Element"   i   "="   vec.get(i) );
    }
}

I'm confused about how to execute the set() method, can anyone help?

public int set(int a, int b){
        a = b;
        if (b>19){
            resize(b);
        }
        return a;
    }

CodePudding user response:

Your vector class contains an internal array. There you have to modify the value at the given position. If the given position is outside of the array then you can use the resize method to enlarge the internal array:

public class MyVector {
    protected Integer[] internalArray;
    public MyVector( int initialSize )
    {
        internalArray = new Integer[initialSize];
    }
    public void resize( int newSize )
    {
        if ( newSize != internalArray.length ) {
            Integer[] newArray = new Integer[newSize];
            for (int i=0; i < Math.min(newSize, internalArray.length); i  )
                newArray[i] = internalArray[i];
            internalArray = newArray;
        }
    }
    public Integer get( int index )
    {
        if ( index < internalArray.length )
            return internalArray[index];
        return null;
    }
    public void set( int index, int value )
    {
        if (index > internalArray.length -1) {
            resize(index   1);
        }
       
        internalArray[index] = value;
    }    
    public static void main(String[] args)
    {
        MyVector vec = new MyVector(5);
        for ( int i = 0 ; i < 20 ; i   )
            vec.set(i, i*i );
        for ( int i = 0 ; i < 20 ; i   )
            System.out.println( "Element"   i   "="   vec.get(i) );
    }
}
$ java MyVector.java
Element0=0
Element1=1
Element2=4
Element3=9
Element4=16
...
Element17=289
Element18=324
Element19=361

CodePudding user response:

Welcome to stackoverflow! I have a feeling this is a homework question, if it is - please try to do your homework on your own since it will greatly assist you in the future.

As you can see in the code you have provided there is currently no function set() for your MyVector class.

In order to make a set() function you have to declare it. This w3schools guide will most likely explain the basics for you: https://www.w3schools.com/java/java_encapsulation.asp

For your code that could be:

public void set(int index, int value) {}

Assuming you don't want a return value from the function.

Then you can just change the value of the array to what you want it to be:

public void set( int index, int value ) {
    internalArray[index] = value;
}   

More information if you are interested: What is an interesting point is that many people discuss the reasoning behind why we in OOP should need a use for setters and getters. In your example it seems like you only want your list to be of size = initialSize which you set to 5. What if someone inputs an index higher than 5? What if they input 10 instead? What will your list look like? If you want to limit those things you can in your set() function add limits to the values and indexes acceptable (which from an OOP standpoint) might be a better idea.

As I tried to explain there might be occasions where you would want to handle events when the index goes beyond. This is an example of what set() can look like if you want to increase the array size if the index is out of bounds. I really suggest you look into array and deep copying though.

public void set( int index, int value ) {
    if (index > internalArray.length - 1) {
        Integer[] newInternalArray = new Integer[internalArray.length   1];
        for(int i = 0; i < internalArray.length; i  ) {
            newInternalArray[i] = internalArray[i];
        }
        newInternalArray[newInternalArray.length-1] = value; 
        internalArray = newInternalArray;
    } else {
        internalArray[index] = value;
    }
}   
  •  Tags:  
  • java
  • Related