Question

Why is the answer 5058? Again, what is printed is 5058. I want to know -...

Why is the answer 5058? Again, what is printed is 5058. I want to know - in the least complicated way possible - why this is the answer. I need to know for a test.

Consider the following class

public class IntegerArrayList {
   
    private int[] array;
    private int lastUnusedIndex;
    public final static int DEFAULT_SIZE = 1000;
   
    public IntegerArrayList() {
        this(DEFAULT_SIZE);
    }

    public IntegerArrayList(int size) {
        if (size < 1)
            size = DEFAULT_SIZE;
       
        this.array = new int[size];
        this.lastUnusedIndex = 0;
    }
   
    public int get(int index) {
        if (index < 0 || index >= this.lastUnusedIndex)
            return 0;
       
        return this.array[index];
    }
   
    public void add(int item) {
       
        this.array[this.lastUnusedIndex] = item;
        this.lastUnusedIndex ++;
       
        if (this.lastUnusedIndex >= this.array.length)
            makeArrayTwiceAsBig();
       
    }
   
    private void makeArrayTwiceAsBig() {
        int[] temp = new int[this.array.length * 2];
       
        for (int i = 0; i < this.array.length; i++)
            temp[i] = this.array[i];
       
        this.array = temp;
    }
   
    public int size() {
        return this.lastUnusedIndex;
    }
   
    public int getInternalArraySize() {
        return this.array.length;
    }
}

What is printed on the console after the following lines execute?

IntegerArrayList ial = new IntegerArrayList(2);

ial.add(5);
ial.add(3);
ial.add(9);
ial.add(12);
ial.add(0);

System.out.print(ial.get(0));
System.out.print(ial.get(-5));
System.out.print(ial.size());
System.out.print(ial.getInternalArraySize());

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Explanation:

IntegerArrayList ial = new IntegerArrayList(2);
creates an array with size 2 and lastUnusedIndex=0

after the following lines execute
ial.add(5);
ial.add(3);
ial.add(9);
ial.add(12);
ial.add(0);

ial has array with length 8 and the content of array is [5, 3, 9, 12, 0, 0, 0, 0]
0 1 2 3 4 5 6 7 --> indices of array

                                                      
System.out.print(ial.get(0)); it prints 5 as output because 5 is present at 0th index of array.

System.out.print(ial.get(-5)); it prints 0 as output because if index < 0, get(int index) method return 0.

System.out.print(ial.size()); it prints 5 because size() method returns lastUnusedIndex which is 5.

System.out.print(ial.getInternalArraySize()); it prints 8 because getInternalArraySize() method returns array.length which is 8.

So the output is 5058

Add a comment
Know the answer?
Add Answer to:
Why is the answer 5058? Again, what is printed is 5058. I want to know -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Why is the answer 10 and 0 (respectively - two different answers, one is 10 and...

    Why is the answer 10 and 0 (respectively - two different answers, one is 10 and the second one is 0)? I don't know how to trace this. Please don't be too complex in your explanation. Consider the following class public class Node {     private int item;     private Node next;         public Node() {         this(0);     }     public Node(int item) {         this(item, null);     }     public Node(int item, Node next) {...

  • please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1...

    please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1 colum like the one bellow, first box should have the class name, second box is for class attributes, and third box should have the class operations/methods and please put a (+) for public and a (-) for private example: class +-attributes +-Operations Also make a JAVADOCs, but not generated by a compiler, to explain what is going on in the code import java.util.Random; public...

  • Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data...

    Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> {    // Data fields    /** The default initial capacity */    private static final int INITIAL_CAPACITY = 10;       /** The underlying data array */    private E[] theData;       /** The current size */    private int size = 0;       /** The current capacity */    private int capacity = 0;       @SuppressWarnings("unchecked")    public KWArrayList() {        capacity...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think...

    I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item)       Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private...

    public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count;   } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT