Pages

Sunday, 22 January 2017

Chapter 23 Exercise 6, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

23.6 (Check order) Write the following overloaded methods that check whether an array is ordered in ascending order or descending order. By default, the method checks ascending order. To check descending order, pass false to the ascending argument in the method.

public static boolean ordered(int[] list)
public static boolean ordered(int[] list, boolean ascending)
public static boolean ordered(double[] list)
public static boolean ordered
(double[] list, boolean ascending)
public static <E extends Comparable<E>>
boolean ordered(E[] list)
public static <E extends Comparable<E>> boolean ordered
(E[] list, boolean ascending)
public static <E> boolean ordered(E[] list,Comparator<? super E> comparator)
public static <E> boolean ordered(E[] list,
Comparator<? super E> comparator, boolean ascending)


import java.util.Comparator;

public class Exercise06 {

 public static void main(String[] args) {
  System.out.println(ordered(new double[]{-6.0, 12, 433, 7123}));
 }
 
 
 public static boolean ordered(int[] list) {
  return ordered(list, true);
 }
 
 public static boolean ordered(int[] list, boolean ascending) {
  if(ascending) {
   for (int i = 0; i < list.length - 1; i++) {
    if(list[i] > list[i + 1]) {
     return false;
    }
   }
   return true;
  } else {
   for (int i = 0; i < list.length - 1; i++) {
    if(list[i] < list[i + 1]) {
     return false;
    }
   }
   return true;   
  }
 }
 
 public static boolean ordered(double[] list) {
  return ordered(list, true);
 }
 
 
 public static boolean ordered(double[] list, boolean ascending) {
  if(ascending) {
   for (int i = 0; i < list.length - 1; i++) {
    if(list[i] > list[i + 1]) {
     return false;
    }
   }
   return true;
  } else {
   for (int i = 0; i < list.length - 1; i++) {
    if(list[i] < list[i + 1]) {
     return false;
    }
   }
   return true;   
  }
 }
 
 
 public static <E extends Comparable<E>> boolean ordered(E[] list) {
  return ordered(list, true);
 }
 
 
 public static <E extends Comparable<E>> boolean ordered (E[] list, boolean ascending) {
  if(ascending) {
   for (int i = 0; i < list.length - 1; i++) {
    if(list[i].compareTo(list[i + 1]) > 0) {
     return false;
    }
   }
   return true;
  } else {
   for (int i = 0; i < list.length - 1; i++) {
    if(list[i].compareTo(list[i + 1]) < 0) {
     return false;
    }
   }
   return true;   
  }
 }
 
 
 public static <E> boolean ordered(E[] list, Comparator<? super E> comparator) {
  return ordered(list, comparator, true);
 }
 
 
 public static <E> boolean ordered(E[] list, Comparator<? super E> comparator, boolean ascending) {
  if(ascending) {
   for (int i = 0; i < list.length - 1; i++) {
    if(comparator.compare(list[i], list[i + 1]) > 0) {
     return false;
    }
   }
   return true;
  } else {
   for (int i = 0; i < list.length - 1; i++) {
    if(comparator.compare(list[i], list[i + 1]) < 0) {
     return false;
    }
   }
   return true;   
  }
 }
 
 

}

No comments :

Post a Comment