** don\'t send the answer in a picture, I need it A text- the subject in JAVA**A
ID: 3718050 • Letter: #
Question
** don't send the answer in a picture, I need it A text- the subject in JAVA**Assume the following intValue class definition: class intValue{ int x; public void setX(int i){x=i;} public int getX(){return x;} } Write the code needed to create an array list of intValue elements, add 5 intValue objects to the array list, and set the value of x of each element to the index of the element. Also print the values of x of all elements on one line with no spaces. Note this means the output of this code is: 01234.
Explanation / Answer
IntValueList.java
import java.util.ArrayList;
public class IntValueList {
public static void main(String[] args) {
ArrayList<intValue> list = new ArrayList<intValue>();
for(int i=0;i<5;i++) {
intValue obj = new intValue();
obj.setX(i);
list.add(obj);
}
for(int i=0;i<5;i++) {
intValue obj=list.get(i);
System.out.print(obj.getX());
}
System.out.println();
}
}
intValue.java
class intValue {
int x;
public void setX(int i) {
x = i;
}
public int getX() {
return x;
}
}
Output:
01234
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.