Question

In: Computer Science

Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must...

Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must delete all even numbers in the chain pointed to by aList, while preserving the original of the remaining numbers. For example, if aList is this list: 3, 9, 2, 10, 5, 6; the list becomes after calling the method as follows: 3, 9, 5.

useing netbeans

Solutions

Expert Solution

import java.util.*;
public class Main
{
    public static void deleteEvenNumbers(List<Integer>numbers)
    {

        // Iterator = An iterator over a collection/List, hasNext()=Returns true if iteration has more elements, next()=Returns next element in a collection/List


        for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext();){
            Integer number = iterator.next();

       // Checks if number is even or not
                if (number % 2 == 0) {

      //remove the number if its even
                    iterator.remove();
                }
            }
    }

  
   public static void main(String[] args)
   {
          

//Creating an empty List to test our method

List<Integer> numbers = new ArrayList<Integer>();

//Adding the elements inside the List
           numbers.add(11);
           numbers.add(45);
           numbers.add(12);
           numbers.add(32);
           numbers.add(36);  

//Printing the List before calling the method deleteEvenNumbers(List<Integer>numbers)
           System.out.println(numbers);
           deleteEvenNumbers(numbers);

//Printing the List after calling the method deleteEvenNumbers(List<Integer>numbers)
           System.out.println(numbers);

  
   }
}


Related Solutions

Re-do Problem 1, but now implement the following method: i must use this code :- public...
Re-do Problem 1, but now implement the following method: i must use this code :- public static BagInterface intersection (BagInterface bagA, BagInterface bagB) and here how my java program must work This method must return a new bag which is the intersection of the two bags: bagA and bagB. An element appears in the intersection of two bags the minimum of the number of times it appears in either. For example, {1,1,2} ∩{1,1,2,2,3}= {1,1,2}. Do not forget to state the...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is the subclass of ‘A’. Is it possible to define the same method public static void show( ) in the Derived class. Why give reasons? Is it run time polymorphism? (0.5 Marks) Is it possible to define the method public void show( ) (non static method) in the Derived class. What will happen and why give reasons?
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT