CODE:-
import java.util.LinkedList;
import java.util.Queue;
public class HomeworkLib {
public static void main(String[] args) {
Queue<Integer> inputQ = new LinkedList<>(); // HERE I AM CREATING A QUEUE IN JAVA
inputQ.add(2); // ADDING FEW INTEGERS IN THE QUEUE
inputQ.add(0);
inputQ.add(5);
inputQ.add(4);
inputQ.add(7);
inputQ.add(9);
inputQ.add(8);
inputQ.add(1);
Queue<Integer>[] result = split(inputQ); // CALLING SPLIT METHOD AND SAVING IT'S RESULT IN RESULT
System.out.println("EVEN QUEUE!");
while (result[0].peek() != null) { // PRINTING THE RESULTS OF THE FIRST INDEX QUEUE
System.out.println(result[0].poll());
}
System.out.println("ODD QUEUE!"); // PRINTING THE SECOND INDEX QUEUE
while (result[1].peek() != null) {
System.out.println(result[1].poll());
}
}
public static Queue<Integer>[] split(Queue<Integer> inputQ) { // THIS IS QUEUE THAT RETURNS QUEUE ARRAY
Queue<Integer> even = new LinkedList<>(); // CREATING 2 QUEUES
Queue<Integer> odd = new LinkedList<>();
while (inputQ.peek() != null) { // NOW ITERATING THE INPUTQ AND CHECKIN IF THAT NUMBER IS EVEN OR ODD
int temp = inputQ.poll(); // AND RESPECTIVELY ADD INTO THE QUEUES.
if (temp % 2 == 0 || temp == 0) {
even.add(temp);
} else {
odd.add(temp);
}
}
Queue<Integer>[] arr = new LinkedList[2]; // CREATING A ARRAY OF 2 QUEUES
arr[0] = even; // ADDING EVEN AND ODD QUEUES INTO THE ARRAY
arr[1] = odd;
return arr; // RETURNING ARRAY
}
}
OUTPUT:-
FOR ANY OTHER QUERY PLEASE COMMENT.
write in java split() A static method that takes as input parameter a ThingArrayQueue called inputQ,...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
In Java* Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...
Write a method that takes an array of ints and that returns true if there are more odd numbers than even numbers. However, as for a casino, 0 is not counted as an odd or an even number. If the array is empty or contains only 0's the method should return false.
Write a method named avgLength which takes an array of Strings as an input parameter and returns a double. The method should calculate and return the average length of all the strings in the array (in java langauge)
java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.
JAVA Code: Complete the method, sumOdds(), below. The method takes in an array of integers, numbers, and returns the sum of all the odd numbers in the array. The method should return 0 if the array contains no odd numbers. For example, if the given array is [12, 10, 5, 8, 13, 9] then the method should return 27 (5+13+9=27). Starter Code: public class Odds { public static int sumOdds(int[] numbers) { //TODO: complete this method } }
A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...
11) Write a method (including header and body) that takes as an input an array of doubles and f three doubles containing the average, the maximum and the minimunm values of the input array. Test the program by calling the method from the main method. For example Input: 1 2 3 45 Output: 3 5 1 12) Write a Java method that takes a string as an argument and returns the reverse the string. Test the program by calling the...
JAVA PLEASE! Write a method called displayPets that takes an array of strings as input. The method should display the contents of the pets array on one line. Each name should be separated by a space. Use an enhanced for loop to iterate the array. Make sure to declare modifier, parameters and return values. Given an array String[] pets = {"Lucky", "Slinger", "Beast", "Trumpet", "Lulu", "Shadow", "Daisy"} A sample run when calling displayPets(pets) from main() would look like: Pets names:...