I have a few questions. I am working on a homework assignment but came across a few confusing things. (I am taking an introductory class, sorry for the mistakes.)
Implement a class that takes an integer array and an int x as its size. Create a method inside the class that creates a new array whose length is one greater than data’s length. Then create a method to copy all data’s elements into the new array and add the value of x into the last element of the array. Create a method to return all the integers in the new array.
Here's what I have
package taskone;
import java.util.*;
class Arrayplus1 {
int x;
int data[];
void example(int x) {
this.x = x+1;
this.data= new int[x];
}
void increaseSizeOfArray(int incrementSize) {
int copiedArray[] = Arrays.copyOf(data, data.length + incrementSize);
data = copiedArray;
}
void printall() {
System.out.println(Arrays.toString(data));
}
}
public class TaskOne {
public static void main(String[] args) {
example task = new example();
task.printall();
}
}
This simply returns null.
My question is the last bit of the task above. "Create a method to return all the integers in the new array."
How is this possible when I did not include any elements in my array? I am trying to follow the assignment guidelines. Thanks.
printallmethod. But the requirement is to return all the integers and not to print them. Additionally, you even did not correctly implement the first requirement. - Seelenvirtuoseadd the value of x into the last element of the array- you are supposed to add x to the array, so it won't be empty. - Eranint[] data = { };- Peter Lawreynew example()creates an object of the classexample, and I don't see a class namedexample. - ajb