Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote