You are going to write a direction() method that calculates whether the given ar
ID: 3909646 • Letter: Y
Question
You are going to write a direction() method that calculates whether the given array is sorted. Your method should return a 1 if it’s sorted in increasing order, 2 if it’s sorted in decreasing order, or a 0 if it’s neither.
Write the body of the program The body should call the method direction() and based on the input print out one of the following:
“Array is in increasing order.”
“Array is in decreasing order.”
“Array is neither in increasing or decreasing order.”
import java.util.*;
import java.util.regex.Pattern;
public class Problem1 {
// PLEASE START YOUR METHOD HERE
// *********************************************************
// *********************************************************
//PLEASE END YOUR METHOD HERE
public static void main( String [] args ) {
Scanner in = new Scanner( System.in );
ArrayList<Integer> order = new ArrayList<Integer>();
while(in.hasNext()){
order.add(in.nextInt());
}
// PLEASE START YOUR TESTS HERE
// *********************************************************
// *********************************************************
//PLEASE END YOUR METHOD HERE
in.close();
System.out.print("END OF OUTPUT");
}
}
Input The direction() method will take the following parameters an ArrayList of type Integer Processing ArrayLists a are a little different than Arrays. They expand automatically. The syntax is also a little different. Instead of doing array[i] to find the result of a position, you use array.get(i) Process the given array of inputs to determine whether it's in increasing, decreasing, or neither order Output The direction) will return a 0, 1, or 2 depending on which order it detects The main method will output one of the following cases Array is in increasing order." "Array is in decreasing order "Array is neither in increasing or decreasing order."Explanation / Answer
if you have any doubts, please give me comment...
import java.util.*;
public class Problem1 {
// PLEASE START YOUR METHOD HERE
// *********************************************************
public static int direction(ArrayList<Integer> list) {
int i, len = list.size();
for (i = 1; i < len; i++) {
if (list.get(i - 1) > list.get(i))
break;
}
if (i == len)
return 1;
else {
for (i = 1; i < len; i++) {
if (list.get(i - 1) < list.get(i))
break;
}
if (i == len)
return 2;
}
return 0;
}
// *********************************************************
// PLEASE END YOUR METHOD HERE
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Integer> order = new ArrayList<Integer>();
while (in.hasNext()) {
order.add(in.nextInt());
}
// PLEASE START YOUR TESTS HERE
// *********************************************************
if (direction(order) == 1)
System.out.println("Array is increasing order.");
else if (direction(order) == 2)
System.out.println("Array is decreasing order.");
else
System.out.println("Array is neither in increasing or decreasing order.");
// *********************************************************
// PLEASE END YOUR METHOD HERE
in.close();
System.out.print("END OF OUTPUT");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.