ListInterface<String> myList = new MyList();
myList.add("alpha");
myList.add(1, "beta");
myList.add("gamma");
myList.add(2, "delta");
myList.add(4, "theta");
myList.remove(2);
myList.remove(2);
myList.replace(3, "delta");
Ans:
/**
* Replaces an object at a given position in a list with a new object.
*
* @param nameList the list containing the object to be replaced
* @param location the position of the object to be replaced
* @param newObj the new object to replace the existing object
*
* @throws IndexOutOfBoundsException if the given location is invalid
*/
public static void myReplaceMethod(ListInterface<String> nameList, int location, String newObj) {
// Check if location is within valid range
if (location < 1 || location > nameList.getLength()) {
throw new IndexOutOfBoundsException("Invalid index");
}
// Remove existing element at location
nameList.remove(location);
// Add new element at same location
nameList.add(location, newObj);
}
ListInterface<Double> quizScores = new MyList<>();
Note:
You may put all the code in one method, or first remove lowest from the list and then call another method that takes a list and computes average of the list.
Ans: FYI I've also included the class in the attached files.
/**
* This will calculate the Average removing the lowest score from the list.
* @param quizScores is the list of the score.
* @return average score value.
*/
public static double computeAvgWithoutLowest(List<Double> quizScores){
//Remove the Lowest Score.
double lowestScore = Double.MAX_VALUE;
int lowestIndex = -1;
double totalScore = 0;
for (int i = 0; i < quizScores.size(); i++) {
double score = quizScores.get(i);
if(score < lowestScore)
{
lowestScore = score;
lowestIndex = i;
}
}
//Remove the lowest index.
if(lowestIndex > -1){
quizScores.remove(lowestIndex);
}
//Calculate the average of the quizScores now.
for(double score: quizScores){
totalScore = totalScore + score;
}
return totalScore/quizScores.size();
}
/**
* Finds the position of the specified object in this list.
*
* @param anObject the object to find the position of
* @return the position of the object in the list, or -1 if not found
*/
public int getPosition(T anObject){
checkIntegrtiy();
for(int i = 1; i <= numberOfEntries; i++){
if(list[i].equals(anObject)){
return i;
}
}
return -1; //if not found.
}
Ans:
//Replace method that will return boolean.
public boolean replace(int givenPosition, T newEntry) {
checkIntegrity();
if (givenPosition < 1 || givenPosition > numberOfEntries) {
return false; // invalid position
} else {
list[givenPosition] = newEntry;
return true; // replacement successful
}
}
Ans: No, you cannot modify the return type of the replace
method without revising ListInterface
. The interface specifies that the replace
method should return the replaced element, and any implementation of the interface must conform to that specification. Changing the return type would break the contract of the interface.
public void addAll(T[] items)
Ans:
/**
* Adds an array of items to the end of the list.
* @param items the array of items to be added
*/
public void addAll(T[] items) {
// Iterate over each item in the array
for (T item : items) {
// Add the item to the end of the list
add(item);
}
}
public ListInterface<T> getAllLessThan(Comparable<T> anObject)
Ans:
/**
* Returns a new list containing all items in the original list that are less than the specified object.
* The original list is not modified.
*
* @param anObject the object to compare items to
* @return a new list containing all items less than the specified object
*/
public ListInterface<T> getAllLessThan(Comparable<T> anObject) {
ListInterface<T> result = new LList<>();
for (Comparable<T> item : list) {
// compare the current item to the specified object
if (item.compareTo(anObject) < 0) {
// if the item is less than the specified object, add it to the new list
result.add(item);
}
}
return result;
}
public int aFunction(int n){
int result = 0;
if (n <= 4)
result = 1;
else
result = aFunction(n / 2) + aFunction(n / 4);
return result;
}
Ans: The first call is to aFunction(8)
, which is the original call. Since n
is greater than 4, it calls aFunction(8/2)
and aFunction(8/4)
recursively.
aFunction(8/2)
is equivalent to aFunction(4)
, which is called next. Since 4 is less than or equal to 4, the result is set to 1 and returned. This completes the first call to aFunction(8/2)
.
Next, aFunction(8/4)
is equivalent to aFunction(2)
, which is called. Since 2 is less than or equal to 4, the result is set to 1 and returned. This completes the first call to aFunction(8/4)
.
Now, the original call to aFunction(8)
waits for the results of the recursive calls. First, it gets the result of aFunction(8/2)
which is 1, then it gets the result of aFunction(8/4)
which is also 1. It adds them together and returns 2 as the final result. This completes the original call to aFunction(8)
.
Computer Science
Sulav Jung Hamal - 2024/08/20