25. Write a static Java method that takes an array of integers as its argument a
ID: 3536304 • Letter: 2
Question
25. Write a static Java method that takes an array of integers as its argument and returns true if the array is
sorted in descending order, false otherwise. The method should not have any side effects.
26. Write a complete Java program that first prompts the user first for the size of an input list. If this value
is less than 1 the program ends. If the size is 1 or more, the program then prompts for a list of that many
integers. The program counts how many of the integers in the list have values strictly between the value of the
first integer in the list and the value of the last integer in the list. Note that it is possible that the last integer is
smaller than the first. Here are three possible runs of the program, user input is underlined and italicized:
How many integers: 5
Enter integer: 17
Enter integer: 2
Enter integer: 11
Enter integer: 45
Enter integer: 6
1 values were between 17 and 6
How many integers: 3
Enter integer: 4
Enter integer: 10
Enter integer: 2
0 values were between 4 and 2
How many integers: 6
Enter integer: 11
Enter integer: 4
Enter integer: 17
Enter integer: 88
Enter integer: 16
Enter integer: 171
3 values were between 11 and 171
Hint: store the list of numbers in an array. You may continue your code on the back of the previous page.
Explanation / Answer
// Working code a)
import java.io.*;
public class JavaApplication13 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.println("enter an array of integers");
int a[]=new int[5];
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<5; i++)
{
a[i]=Integer.parseInt(br.readLine());
}
if(isSorted(a)==true)
{
System.out.println("correct");
}
else
{
System.out.println("incorrect");
}
}
public static boolean isSorted(int[] a){
if (a.length == 1){
return true;
}
boolean apples = false;
int i = 1;
while (i <= a.length-1){
if (a[i] < a[i-1]){
apples = true;
} else {
apples = false;
break;
}
i++;
}
return apples;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.