Question

In: Computer Science

Problem 3: The IsSorted class [30’] Description: This Java program will create an integer array object...

Problem 3: The IsSorted class [30’]

Description:

This Java program will create an integer array object with arbitrary elements, and judge whether this array is sorted (non-decreasing). For example, array1 {2,3,1,0} is not sorted, while array2 {5, 8,10,12,15} is sorted.

You may assume the array is non-empty.

Specifications:

In addition to the main method, you need to create another method

  • isSorted()

main Method

  1. The main method will create an array of integers, and initialize its elements with arbitrary values.
  2. The main method will then call the isSorted() method by passing this array. The isSorted() method will judge whether the array is sorted or not and return true or false.
  3. Based upon the results of the isSorted() method, the main method will print out a corresponding message.

isSorted Method

The isSorted() method takes in an array of integers as a parameter. It checks whether all elements in this array are sorted (arranged in non- decreasing order) or not, and return true or false.

Output

Your output should look something like follows:

//Suppose the array is [5, 0, 2, 3, 8]

$java IsSorted

The array is not sorted.

//Suppose the array is [1, 1]

$java IsSorted

The array is sorted.

//Suppose the array is [2, 6, 10, 15, 20]

$java IsSorted

The array is sorted.

//Suppose the array is [7, 4, 3, 0]

$java IsSorted

The array is not sorted.

Solutions

Expert Solution

thanks for the question, here is the fully implemented class in Java

=====================================================================================

import java.util.Arrays;
import java.util.Random;

public class IsSorted {

    private static final int SIZE = 4;

    public static void main(String[] args) {

        int numbers[] = new int[SIZE];
        Random random = new Random();
        for (int index = 0; index < SIZE; index++) {
            numbers[index] = random.nextInt(10);
        }
        System.out.println(Arrays.toString(numbers));
        boolean isSorted = isSorted(numbers);
        if(isSorted){
            System.out.println("The array is sorted");
        }else {
            System.out.println("The array is not sorted");
        }

    }

    public static boolean isSorted(int[] numbers) {

        for (int index = 0; index < numbers.length - 1; index++) {
            if (numbers[index] > numbers[index + 1]) {
                return false;
            }
        }
        return true;
    }
}


Related Solutions

Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Create a simple Java class for a Month object with the following requirements:  This program...
Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem....
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. Recursion may appear in various contexts and in different forms. For fast implementation, we should always aim at transforming recursions into a simpler form of computation. In this assignment, the task is to evaluate X(·), which is defined as follows:               |0,if m = 0 or n = 0               | X(m,n−1),if n is odd and m is even X(m,n) = | X(m−1,n),if m...
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class...
In Java...create a class Realestate.java with the following fields: location, price, and description. Create a class RealestateFinder.java that reads in real estate data from a CSV file (RealestateList.csv). Then provide the user with the following options: Sort per Price, Sort per Location, and Exit. Use ArrayList and Arrays.sort to perform the sorts and display the data to the user.
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Java Collatz Conjecture (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following...
Java Collatz Conjecture (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. The Collatz conjecture is about a sequence: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what...
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT