Question

In: Computer Science

Write a Java program that prompts the user to enter a list of integer values and...

Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list.

<Output>

Enter list: 8 101516619111

The list is not sorted

<End Output

<Output>

Enter list: 10 11344579 11 21

The list is already sorted

<End Output

Create a complete class for your program named SortTest and paste the entire class into the text box provided. Your class must contain a main method, and you should create at least one more method in your class to carry out the task described. This additional method should use a returned value to tell the calling method whether the list is sorted or not, it is up to you to determine what data type you return to the caller for this purpose. Your program does not need to prompt the user for additional input beyond a single list of numbers to be sorted, it can terminate once the task has been completed.

Solutions

Expert Solution

/* Using Recursive approach for in this program */
import java.util.Scanner;
class SortTest{
static int array(int arr[], int n)
   {
       /* escape the recursion by checking Array has one or no element rest are checked already.*/
       if (n == 1 || n == 0)
           return 1;

       // Checking the values in increasing order
       if (arr[n - 1] < arr[n - 2])
           return 0;

       // untill the Last pair was sorted Keep on checking
       return array(arr, n - 1);
   }

  
   public static void main(String args[]){
  
   // initialize the variables.
   int n, i, j, temp;
   // declare the scanner class for reading input from the user.
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of integers:");
n = sc.nextInt();
   int array[] = new int[n];
   for (i = 0; i < n; i++)
array[i] = sc.nextInt();
  
  
       if (array(array, n) != 0)
           System.out.println("The list is already sorted");
       else
           System.out.println("The list is not sorted");
  
  
   }
}

************Request for positive feedback pls..************


Related Solutions

JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
Write a java program that asks user to enter a set of positive integer values. When...
Write a java program that asks user to enter a set of positive integer values. When the user stops (think of sentinel value to stop), the program display the maximum value entered by the user. Your program should recognize if no value is entered without using counter.
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
Write a program that prompts the user to enter an integer from 1 to 15 and...
Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid, as shown in the following sample run: here............THE PYRAMID HAS TO BE THIS SHAPE AND IT IS DONE IN JAVA PLEASE 7 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value....
Include<stdio.h> In C program Write a program that prompts the user to enter an integer value. The program should then output a message saying whether the number is positive, negative, or zero.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT