Java help please!! this is a review questionhaving trouble thanks! class IntArra
ID: 669150 • Letter: J
Question
Java help please!! this is a review questionhaving trouble thanks!
class IntArray{
private int size;
private int [ ] numbers;
public IntArray(int size){
this.size=size;.
numbers=new int[size];
}
public void setElement(int index, int n){
numbers[index]=n;
}
public int getElement(int index){
return numbers[index];
}
}
Please design a solution that can record how much memory is allocated for active IntArray objects. Hints:
a)each integer takes 4 bytes.
b)use static instance variable
Explanation / Answer
import java.lang.instrument.*;
class IntArray{
private int size;
private int [ ] numbers;
public IntArray(int size){
this.size=size;
numbers=new int[size];
}
public void setElement(int index, int n){
numbers[index]=n;
}
public int getElement(int index){
return numbers[index];
}
Public static void main(String args[])
{
IntArray obj = new IntArray (10);
long objSize = inst.getObjectSize(obj);
System.out.println("Bytes used by object: " + objSize+" Bytes");
}
}
Output:
Bytes used by object: 40 Bytes
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.