I am creating a class named Arrays Here I have to set a mutator that set the integer array nums(the attribute) values to the aray argument(parameter(.The mutator will store values of the array argument one at a time into the same element of nums array using a for loop.If array argument is shorter store only the element into nums and leave the remaining elements of the array nums unchanged.If the argument array is longer,store only as many elements as are present in the array nums so that outofboundarrayexception error doesn't occur.....
The one integer array attribute is created named nums and constructor has one integer parameter for the size of the array that value is used to instantiate the interger array called nums and then initializes the array and counting up one for each element....The accessor getNums returns the entire array nums
I dont see any error with this code that I wrote....but somehow the Testarrays class doesn't use the mutator to store the values of array ary1 and print it
Here is the code
import java.lang.*;
public class Arrys
{
int[] nums;
int[] args;
public Arrys(int size)//constructor
{
int[] nums=new int[size];
for (int n=0;n<nums.length;n++)
{
nums[n]=n;
}
}
public int[] getNums(int n) //accessor
{
return nums;
}
public int[] setNums(int[] temp) //mutator
{
for (int k=0;k<temp.length;k++)
{
if (nums[k]>temp[k])
{
nums[k]=temp[k];
}
else
{
nums[k]=temp.length;
}
}
return nums;
}
}//end class
TestArrys.java
public class TestArrys
{
public static void main(String[] args)
{
int[] ary1 = new int[5];
System.out.print("Length="+ary1.length+"\n");
for (int j=0;j<=40;j=j+10)
{
ary1[j/10]=j;
System.out.print(" " +ary1[j/10]);
}
int[] ary2= {3,5,9,11,15,18,22,23,30,31,35,39};
System.out.print("\n\nLength="+ary2.length+"\n");
for (int element:ary2)
{
System.out.print(" " +element);
}
System.out.print("\n");
System.out.println("\nStep 4");
Arrys[] myArrayObject=new Arrys[10];
for(int i=0;i<10;i++)
{
myArrayObject[i]=new Arrys(10);
myArrayObject[i].getNums(i);
}
for(int i=0;i<myArrayObject.length;i++)
{
if (myArrayObject[i]!=null)
{
System.out.print(myArrayObject[i].getNums(i));
}
}
myArratObject.setNums(ary1);
for (int k=0;k<=myArrayObject.length;k=k+10)
{
myArrayObject[k/10]=k;
System.out.print(" "+myArrayObject[k/10]);
}
}
}