Question

In: Computer Science

JAVA Write a test program that prompts the user to enter a list and displays whether...

JAVA

Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the program first prompts the user to enter the size of the list. Using Array

My output should look like this

Enter the size of the list: 8

Enter the contents of the list: 10 1 5 16 61 9 11 1

The list has 8 integers 10 1 5 16 61 9 11 1

The list is not sorted

Solutions

Expert Solution

import java.util.*;

public class Main
{
   public static void main(String[] args) {
      
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the size of the list : ");
       int n = s.nextInt();
       System.out.println("Enter the contents in the list : ");
       int a[] = new int[n];
       for(int i = 0; i < n; i++)
       {
       a[i] = s.nextInt();
       }
       int c1 = 0, c2 = 0;
       for(int i = 0; i < n - 1; i ++)
       {
       if(a[i] > a[i + 1])
       {
       c1++;
       }
       if(a[i] < a[i + 1])
       {
       c2++;
       }
       }
      
       if(c1 + 1 == n || c2 + 1 == n)
       {
       System.out.println("The list is sorted");
       }
       else{
       System.out.println("The list is not sorted");
       }

   }
}


Related Solutions

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...
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
Problem Description: Write a program that prompts the user to enter a directory and displays the...
Problem Description: Write a program that prompts the user to enter a directory and displays the number of the files in the directory. Analysis: (Describe the problem including input and output in your own words.) Design: (Describe the major steps for solving the problem. How do you use recursion to solve this problem.) Coding: (Copy and Paste Source Code here. Format your code using Courier 10pts) Name the public class Exercise18_29 Testing: (Describe how you test this program)
JAVA Write a program that will compare two names. The program prompts the user to enter...
JAVA Write a program that will compare two names. The program prompts the user to enter two names for a comparison. If the names are same, the program states that. If the names are different, the program converts both names to UPPERCASE, and compares then again. If they are equal, the programs displays a message stating that names are equal if CASE is ignored. Otherwise, the program prints names with a message that names are not equal.  Check also if there...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
Write a Python program that prompts the user to enter a list of words and stores...
Write a Python program that prompts the user to enter a list of words and stores in a list only those words whose first letter occurs again within the word (for example, 'Baboon'). The program should display the resulting list..................please explain step by step
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT