I need help with this inlap and l need the solution to be as simple as possible.
ID: 3808247 • Letter: I
Question
I need help with this inlap and l need the solution to be as simple as possible.
Create a project called Inlab9 and paste the IntegerStorer class from March 24th in Build the following methods without adding any new instance variables void set (index, val). This method will set the value of the specified index to be the specified value. If the index is not in range of the valid data in the IntegerStorer instance, print an error message. int get index). This method will return the value at the specified index. If the index is not in range of the valid data in the IntegerStorer instance, print an error message and return -1 int [] toArray(). This method will return an array "tight" to the valid data in the IntegerStorer. I. e. There should be no extra 0's at the end of the array. void add(index, val). This method will add a value at a specified spot. All entries after that spot need to be shifted down to make space for the new value. If the index is not in range of the valid data in the IntegerStorer instance, print an error message. If the y is not big enough to fit the new value, resize like we did in class. Make a Driver that does some simple tests of your new methodsExplanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
/**
*
* @author Namburi Ramesh
*/
public class IntegerStorer
{
private int[] data;
private int end;
public IntegerStorer()
{
data = new int[2];
end = 0;
}
public void add(int val)
{
if (end < data.length)
{
data[end] = val;
end++;
}
else
{
int[] newData = new int[data.length + 2];
for (int i = 0; i < data.length; i++)
{
newData[i] = data[i];
}
newData[end] = val;
end++;
data = newData;
}
}
public void set(int index,int val){
if(index<end){ //set the value to spcified value if index is in valid range
data[index]=val;
}else{ //else print error
System.out.println("Specified index is not in the range of valid data");
}
}
public int get(int index){
if(index<end){ //return the value if index is in valid range
return data[index];
}else{ //else print error
System.out.println("Specified index is not in the range of valid data");
return -1;
}
}
public int[] toArray(){
int[] newData=new int[end]; //new array of the exact length to be returned
for(int i=0;i<end;i++){
newData[i]=data[i]; //store elements in new array
}
return newData;
}
public void add(int index,int val){
if(end<data.length && index<=end){ //if there is place to add one more element in the array add the value at specified index and shift remaining values after index
int[] newData=new int[data.length];
int counter=0;
for(int i=0;i<=end;i++){
if(i==index){
newData[i]=val;
}else{
newData[i]=data[counter];
counter++;
}
}
data=newData;
end++;
}else if(index<=end){ //if there is no place to add one more element to array resize the array and add
int[] newData = new int[data.length + 2];
int counter=0;
for(int i=0;i<=end;i++){
if(i==index){
newData[i]=val;
}else{
newData[i]=data[counter];
counter++;
}
}
end++;
data=newData;
}else{ //else print error
System.out.println("Specified index is not in the range of valid data");
}
}
}