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

Exercise 1: Finish the implementation of class ArrayScoreList1. You only need to

ID: 3745091 • Letter: E

Question

Exercise 1: Finish the implementation of class ArrayScoreList1. You only need to complete the implementation of the method to add a new score. Such method is completed by properly invoking the internal private method insert (you need to understand this method) discussed above on those places of the method to add a new score and which are marked with ***.

/**

*

*/

package javaClasses;

import interfaces.InvalidRankException;

import interfaces.ScoreList;

/**

* @author pirvos

*

*/

public class ArrayScoreList1 implements ScoreList {

private static final int DEFCAP = 5;

private Score[] element;

private int size;

public ArrayScoreList1(int cap) {

if (cap < DEFCAP)

cap = DEFCAP;

element = new Score[cap];

size = 0; // initially empty

}

public ArrayScoreList1() {

this(DEFCAP); // invoke the other constructor

}

/* (non-Javadoc)

* @see interfaces.ScoreList#addScore(javaClasses.Score)

*/

public void addScore(Score score) {

if (size < element.length) // the array is not full

{

//***

size++;

}

else if (element[size-1].compareTo(score) < 0) {

//***

}

}

private void insert(int index, Score score) {

int pos = index-1;

while (pos >= 0 && (element[pos].compareTo(score) < 0)) {

element[pos+1] = element[pos];

pos--;

}

element[pos+1] = score;

}

/* (non-Javadoc)

* @see interfaces.ScoreList#capacity()

*/

public int capacity() {

return element.length;

}

/* (non-Javadoc)

* @see interfaces.ScoreList#getScore(int)

*/

public Score getScore(int rank) throws InvalidRankException {

if (rank < 1 || rank > size)

throw new InvalidRankException("Method getScore: rank = " + rank);

return element[rank-1];

}

/* (non-Javadoc)

* @see interfaces.ScoreList#isEmpty()

*/

public boolean isEmpty() {

return size == 0;

}

/* (non-Javadoc)

* @see interfaces.ScoreList#size()

*/

public int size() {

return size;

}

///////////// THE FOLLOWING IS INCLUDED ONLY FOR TESTING PURPOSES

public void showInternalContent() {

System.out.println(" The ScoreListObjec internals are: ");

System.out.println("List capacity = " + element.length);

System.out.println("Size = " + size);

System.out.println("Array content:");

for (int i=0; i < element.length; i++)

System.out.println("element["+i+"] = " + element[i]);

}

}

Explanation / Answer

Hi as you asked for addScore() method implementation i am attaching the code .

Please remember the implementation of Comparable interface on Score to use compareTo() method.

//Please implement Comparable interface on Score class
//Otherwise you will get error
public void addScore(Score score) {
if (size < element.length) // the array is not full
{
//Insert the score at its proper rank
//To find proper rank call insert() method
insert(size,score);
size++;
}
  
//If array is full then compare it from last element
//If last element is less than rank of score then it can be inserted
else if (element[size-1].compareTo(score) < 0) {
  
//To find proper rank call insert() method
insert(size-1,score);
}
else
throw new InvalidRankException("Array is full and this rank can't be inserted");

}

//Attach this method implemetation in your code