In: Computer Science
Java Programming Problem:
Define a generic method called checkOrder() that checks if four items are in ascending, neither, or descending order. The method should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order.
The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, Strings, characters, or doubles.
Ex. If the input is:
bat hat mat sat 63.2 96.5 100.1 123.5
the output is:
Order: -1 Order: -1
The following is the current code for the problem:
import java.util.Scanner;
public class WhatOrder {
// TODO: Define a generic method called checkOrder() that
// takes in four variables of generic type as arguments.
// The return type of the method is integer
// Check the order of the input: return -1 for ascending,
// 0 for neither, 1 for descending
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// Check order of four strings
System.out.println("Order: " + checkOrder(scnr.next(), scnr.next(),
scnr.next(), scnr.next()));
// Check order of four doubles
System.out.println("Order: " + checkOrder(scnr.nextDouble(),
scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble()));
}
}
import java.util.Scanner; public class WhatOrder { public static <T extends Comparable<T>> int checkOrder(T n1, T n2, T n3, T n4) { if (n1.compareTo(n2) > 0 && n2.compareTo(n3) > 0 && n3.compareTo(n4) > 0) { return 1; } else if (n1.compareTo(n2) < 0 && n2.compareTo(n3) < 0 && n3.compareTo(n4) < 0) { return -1; } else { return 0; } } // Check the order of the input: return -1 for ascending, // 0 for neither, 1 for descending public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Check order of four strings System.out.println("Order: " + checkOrder(scnr.next(), scnr.next(), scnr.next(), scnr.next())); // Check order of four doubles System.out.println("Order: " + checkOrder(scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble(), scnr.nextDouble())); } }