Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

_____________________________________________________________________________ Ar

ID: 3878540 • Letter: #

Question

_____________________________________________________________________________

ArrayPlay.java :

_____________________________________________________________________________

Desired outcome in plain text:

Specification Copy the program ArrayPlay.iava to your computer and implement the methods named checklfSorted() and reverse(). The TBI (To Be Implemented) method comment blocks document what the methods do The output of the program must match the following [7,7,3,9,0,5,6] (not sorted) [0,3,5,6,7,7,91 [9,8,8,5,3,0] (descending sorted order) [0,3,5,8,8,9] [1,5,6,6,6,7,9 (ascending sorted order) [1,9,3,5,2,7,3,5] (not sorted) [1,2,3,3,5,5,7,9] [0 (ascending sorted order) [1,2 (ascending sorted order) [3,2,1] (descending sorted order) [1,2,3] [5,5,5,5,5] (ascending sorted order)

Explanation / Answer

// Code

import java.util.*;

/*
* This program checks if an array is sorted.
* An unsorted array is sorted in ascending order.
* An array in ascending order is left unchanged.
* An array in descending order is reversed.
*
* @creator gdt
* @created 02017.12.15
* @assignment implement the checkIfSorted()
* and reverseArray() methods
*/

public class ArrayPlay {

// checkIfSorted() return values...
private static final int UNSORTED = 0;
private static final int ASCENDING = 1;
private static final int DESCENDING = 2;

public static void main(String[] argv) {
int[][] data = {
{ 7, 7, 3, 9, 0, 5, 6 },
{ 9, 8, 8, 5, 3, 0 },
{ 1, 5, 6, 6, 6, 7, 9 },
{ 1, 9, 3, 5, 2, 7, 3, 5 },
{ 0 },
{ 1, 2 },
{ 3, 2, 1 },
{ 5, 5, 5, 5, 5 },
};
for (int i = 0; i < data.length; i++) {
printArray(data[i], false);
switch (checkIfSorted(data[i])) {
case ArrayPlay.UNSORTED:
System.out.print(" (not sorted) ");
Arrays.sort(data[i]);
printArray(data[i], true);
break;
case ArrayPlay.DESCENDING:
System.out.print(" (descending sorted order) ");
reverseArray(data[i]);
printArray(data[i], true);
break;
case ArrayPlay.ASCENDING:
System.out.println(" (ascending sorted order)");
break;
}
}
}

private static void printArray(int[] x, boolean nl) {
System.out.print("[");
for (int i = 0, j = x.length - 1; i < j; i++)
System.out.print(x[i] + ",");
System.out.print(x[x.length - 1] + "]");
if (nl) System.out.println();
}

/*
* TBI (To Be Implemented)...
*
* Reverse the contents of the int[] parameter.
*
* @param data an array of integers
*/
private static void reverseArray(int[] data) {
// swapping elements at indices i AND j to get reverse
for (int i = 0, j = data.length - 1; i < j; i++,j--)
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}

}

/*
* TBI (To Be Implemented)...
*
* Checks if the contents of the int[] parameter is sorted.
*
* @param data an array of integers
* @return UNSORTED or ASCENDING or DESCENDING
*/
private static int checkIfSorted(int[] data) {
int asc = 1, desc = 1, i, j;
if(data.length == 0 || data.length == 1)
return ArrayPlay.ASCENDING;
  
// checking if it is not descending order
for(i=0; i<data.length; i++)
{
for(j=i+1; j<data.length; j++)
{
if(data[i]!=data[j])
if(data[i] < data[j])
{
desc = 0;
break;
}
}
}
  
// checking if it is not ascending order
for(i=0; i<data.length; i++)
{
for(j=i+1; j<data.length; j++)
{
if(data[i]!=data[j])
if(data[i] > data[j])
{
asc = 0;
break;
}
}
}
  
// based on those 2 flags returning
if(asc == 0 && desc == 0)
return ArrayPlay.UNSORTED;
else if(desc == 0)
return ArrayPlay.ASCENDING;
else
return ArrayPlay.DESCENDING;
}

}

OUTPUT

Hit thumbs up if you are happy with answer

// Code

import java.util.*;

/*
* This program checks if an array is sorted.
* An unsorted array is sorted in ascending order.
* An array in ascending order is left unchanged.
* An array in descending order is reversed.
*
* @creator gdt
* @created 02017.12.15
* @assignment implement the checkIfSorted()
* and reverseArray() methods
*/

public class ArrayPlay {

// checkIfSorted() return values...
private static final int UNSORTED = 0;
private static final int ASCENDING = 1;
private static final int DESCENDING = 2;

public static void main(String[] argv) {
int[][] data = {
{ 7, 7, 3, 9, 0, 5, 6 },
{ 9, 8, 8, 5, 3, 0 },
{ 1, 5, 6, 6, 6, 7, 9 },
{ 1, 9, 3, 5, 2, 7, 3, 5 },
{ 0 },
{ 1, 2 },
{ 3, 2, 1 },
{ 5, 5, 5, 5, 5 },
};
for (int i = 0; i < data.length; i++) {
printArray(data[i], false);
switch (checkIfSorted(data[i])) {
case ArrayPlay.UNSORTED:
System.out.print(" (not sorted) ");
Arrays.sort(data[i]);
printArray(data[i], true);
break;
case ArrayPlay.DESCENDING:
System.out.print(" (descending sorted order) ");
reverseArray(data[i]);
printArray(data[i], true);
break;
case ArrayPlay.ASCENDING:
System.out.println(" (ascending sorted order)");
break;
}
}
}

private static void printArray(int[] x, boolean nl) {
System.out.print("[");
for (int i = 0, j = x.length - 1; i < j; i++)
System.out.print(x[i] + ",");
System.out.print(x[x.length - 1] + "]");
if (nl) System.out.println();
}

/*
* TBI (To Be Implemented)...
*
* Reverse the contents of the int[] parameter.
*
* @param data an array of integers
*/
private static void reverseArray(int[] data) {
// swapping elements at indices i AND j to get reverse
for (int i = 0, j = data.length - 1; i < j; i++,j--)
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}

}

/*
* TBI (To Be Implemented)...
*
* Checks if the contents of the int[] parameter is sorted.
*
* @param data an array of integers
* @return UNSORTED or ASCENDING or DESCENDING
*/
private static int checkIfSorted(int[] data) {
int asc = 1, desc = 1, i, j;
if(data.length == 0 || data.length == 1)
return ArrayPlay.ASCENDING;
  
// checking if it is not descending order
for(i=0; i<data.length; i++)
{
for(j=i+1; j<data.length; j++)
{
if(data[i]!=data[j])
if(data[i] < data[j])
{
desc = 0;
break;
}
}
}
  
// checking if it is not ascending order
for(i=0; i<data.length; i++)
{
for(j=i+1; j<data.length; j++)
{
if(data[i]!=data[j])
if(data[i] > data[j])
{
asc = 0;
break;
}
}
}
  
// based on those 2 flags returning
if(asc == 0 && desc == 0)
return ArrayPlay.UNSORTED;
else if(desc == 0)
return ArrayPlay.ASCENDING;
else
return ArrayPlay.DESCENDING;
}

}

OUTPUT

  [7,7,3,9,0,5,6] (not sorted) [0,3,5,6,7,7,9]  [9,8,8,5,3,0] (descending sorted order) [0,3,5,8,8,9]  [1,5,6,6,6,7,9] (ascending sorted order)  [1,9,3,5,2,7,3,5] (not sorted) [1,2,3,3,5,5,7,9]  [0] (ascending sorted order)  [1,2] (ascending sorted order)  [3,2,1] (descending sorted order) [1,2,3]  [5,5,5,5,5] (descending sorted order) [5,5,5,5,5]