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 named noEven(values) that takes an array of integers as a parameter and returns true if all of the integers in the array are odd, and false otherwise. You can assume that the array is not empty and the method returns a boolean value.
For example:
Test | Result |
---|---|
System.out.println(noEven(new int[]{2, 3, 4, 5, 6})); |
false |
System.out.println(noEven(new int[]{1, 3, 5, 7})); |
true |
public static boolean
---------------------------------------------------------------------------------------
3.
Write a static method named repeats_exist(numbers) that takes an array of integers as a parameter and returns true if there are any repeated values in the array, and false if all of the values in the array are unique. Note: you can assume that the array is not empty and the method returns a boolean value.
For example:
Test | Result |
---|---|
System.out.println(repeats_exist(new int[]{2, 3, 4, 5, 6})); |
false |
System.out.println(repeats_exist(new int[]{2, 3, 4, 3, 6})); |
true |
public static boolean
------------------------------------------------------------------------------------------
These three are separate questions,please complete them one by one. Thx~
Answer 1) The code of this question is below:
public class Main
{
public static int getMaxEven(int [] arr){
int [] evenArray=new int[10];
int num;
for (int i = 0, j=0; i < arr.length; i++) {
if(arr[i] % 2 == 0){
evenArray[j] = arr[i];
j++;
}
}
int maxEven=0;
for (int i = 0; i < evenArray.length; i++){
if( evenArray[i] > maxEven )
maxEven=evenArray[i];
}
return maxEven;
}
public static void main(String[] args) {
int[] values1 = {1, 4, 5, 9};
System.out.println(getMaxEven(values1));
int[] values2 = {1, 3, 5, 9};
System.out.println(getMaxEven(values2));
}
}
CODE AND OUTPUT SCREENSHOT
Answer 2) The Code for this question is below:
public class Main
{
public static boolean noEven(int [] arr)
{
boolean odd=true;
for (int i = 0; i < arr.length; i++) {
if(arr[i] % 2 == 0){
odd=false;
}
}
return odd;
}
public static void main(String[] args) {
System.out.println(noEven(new
int[]{2, 3, 4, 5, 6}));
System.out.println(noEven(new int[]{1, 3, 5, 7}));
}
}
CODE AND OUTPUT SCREENSHOT
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...
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...
1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...
in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...
Ch. 09: Exclusive find in Array (Arrays, conditional, counter) Write a method that will receive an array of integers and an integer value as a parameter. The integer value received as the second parameter will be searched within the Array received as the other parameter. The method will return true if the value appears only once in the Array. Use the "documentation shown in the program as a guide". As an example, if the array received was the sequence of...
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 } }
Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...
1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...
Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...
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...
In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...