Write a method called removeLast that removes and returns the last value from a list of integers. For example, if a variable called list stores [8, 17, 42, 3, 8], a call of list.removeLast(); should return 8 and change the list’s state to [8, 17, 42, 3]. The next call would return 3 and remove 3 from the list, and so on. If the list is empty, throw a NoSuchElementException.
The program needs to be in Java. Please also create a main Method to test the code.
CODE IN JAVA:
ListDemo.java file:
import java.util.*;
public class ListDemo {
public static int removeLast(ArrayList<Integer>
arr) {
int n = arr.size();
if (n == 0)
throw new
NoSuchElementException();
else {
int result =
arr.get(n - 1);
arr.remove(n -
1);
return
result;
}
}
public static void
displayArr(ArrayList<Integer> arr) {
if (arr.size() == 0) {
System.out.println("Your array is empty");
return;
}
for (int val : arr)
System.out.print(val + " ");
System.out.println("");
}
public static void main(String[] args) {
try {
ArrayList<Integer> arr = new
ArrayList<Integer>();
arr.add(8);
arr.add(17);
arr.add(42);
arr.add(3);
arr.add(8);
System.out.println("Now your array list is : ");
displayArr(arr);
removeLast(arr);
System.out.println("After calling removeLast method your array list
is : ");
displayArr(arr);
removeLast(arr);
System.out.println("After calling removeLast method your array list
is : ");
displayArr(arr);
removeLast(arr);
System.out.println("After calling removeLast method your array list
is : ");
displayArr(arr);
removeLast(arr);
System.out.println("After calling removeLast method your array list
is : ");
displayArr(arr);
removeLast(arr);
System.out.println("After calling removeLast method your array list
is : ");
displayArr(arr);
System.out.println("After calling removeLast method your array list
is : ");
removeLast(arr);
displayArr(arr);
} catch (NoSuchElementException e)
{
System.out.println(e);
}
}
}
OUTPUT:

Write a method called removeLast that removes and returns the last value from a list of...
In Java: Let's create a method that has parameters. Write a method, called returnHours that returns a string. Make the string from a string representing your name and a number representing the hours you spend sleeping, both are values that you pass in from the main. Create the variables to pass into the method call in main. Call the method and print the return value in main. Run and test it.
In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). Do NOT change your class name from "Main". The Main class should come first in your code. Your filename should be "Main.java". The Queue3503 class will contain:...
NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...
Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....
Create a method for a singlyLinked class public void remove( ) removes a randomly selected entry from the bag I don't know how to make it random I just know how to remove the first on the list this is what I have /** * * @return Removes and returns the first node of the bag */ @Override public E remove() { if(bagHead == null) { return null; } E element = bagHead.getElement(); bagHead = bagHead.getNext(); count--; return element; }...
Create a method for a singlyLinked class public void remove( ) removes a randomly selected entry from the bag I don't know how to make it random I just know how to remove the first on the list this is what I have /** * * @return Removes and returns the first node of the bag */ @Override public E remove() { if(bagHead == null) { return null; } E element = bagHead.getElement(); bagHead = bagHead.getNext(); count--; return element; }...
Create a method in Java that adds a task to a list and returns false if the task is already part of the list. Create another method to remove tasks from the list, and returns false if the task is not part of the list.
Write a Java program that has a method called arrayAverage that
accepts an arrary of numbers as an argument and returns the average
of the numbers in that array. Create an array to test the code with
and call the method from main to print the average to the screen.
The array size will be from a user's input (use Scanner).
- array size = int
- avergage, temperature = double
- only method is arrayAverage
Output:
Write a method in the HashIntSet class called addAll that accepts another hash set as a parameter and adds all of the elements from the other set into the current set. For example, if the set stores [-5, 1, 2, 3] and the method is passed [2, 3, 6, 44, 79], your set would store [-5, 1, 2, 3, 6, 44, 79]. Write a method in the HashIntSet class called containsAll that accepts another hash set as a parameter and...
Language: Java Topic: Doubly Linked Lists Write a method that removes and returns the last copy of the given data from the list. Do not return the same data that was passed in. Return the data that was stored in the list. Must be O(1) if data is in the tail and O(n) for all other cases. * @param data the data to be removed from the list * @return the data that was removed * @throws java.lang.IllegalArgumentException if...