Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class myClass {
  2.  
  3. /** this method tests if each two consecutive components
  4. * of the array are sorted. And once at least one set
  5. * of two consecutive components don't fit the condition
  6. * the computer gets that the array isn't sorted
  7. */
  8.  
  9. public static boolean isSorted(double[] array) {
  10. boolean sort = false;
  11. for (int i=0; i<array.length; i++){
  12. if (array[i]<= array[i+1]){
  13. sort = true;
  14. } else {
  15. sort = false;
  16. break;
  17. }
  18. }
  19. return sort;
  20. }
  21.  
  22.  
  23. public static double[] append(double[] param1, double[] param2) {
  24.  
  25. double[] newArray = new double[param1.length + param2.length];
  26. for (int i = 0; i < newArray.length; i++) {
  27. if (i < 3) {
  28. newArray[i] = param1[i];
  29. } else {
  30. newArray[i] = param2[i-3];
  31. }
  32. }
  33. return newArray;
  34. }
  35.  
  36.  
  37. public static void printValues(double[] values) {
  38.  
  39. System.out.print("[");
  40. for (int j=0; j<values.length-1; j++) {
  41. System.out.print(values[j]);
  42. System.out.print(", ");
  43. }
  44. System.out.print(values[values.length-1]);
  45. System.out.println("]");
  46. }
  47.  
  48.  
  49. public static void main(String[] args) {
  50.  
  51. double[] array1 = {2,3,7};
  52. double[] array2 = {4,2,6};
  53. double[] appended = append(array1, array2);
  54. printValues(appended);
  55. boolean appendedSort = isSorted(appended);
  56. if (appendedSort) {
  57. System.out.print("The appended array is sorted!");
  58. } else {
  59. System.out.print("The appended array isn't sorted!");
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment