Knowee
Questions
Features
Study Tools

Write a class to sort the given set of integers in descending order. Include a try block tolocate the array index out of bounds exception and catch it.

Question

Write a class to sort the given set of integers in descending order. Include a try block tolocate the array index out of bounds exception and catch it.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a simple implementation in Java:

import java.util.Arrays;
import java.util.Collections;

public class SortDescending {
    private Integer[] array;

    public SortDescending(Integer[] array) {
        this.array = array;
    }

    public void sort() {
        try {
            Arrays.sort(array, Collections.reverseOrder());
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array Index is Out Of Bounds");
        }
    }

    public void printArray() {
        for (int i : array) {
            System.out.print(i + " ");
        }
    }

    public static void main(String[] args) {
        Integer[] array = {10, 20, 30, 40, 50};
        SortDescending sd = new SortDescending(array);
        sd.sort();
        sd.printArray();
    }
}

In this code:

  1. We import the necessary classes.
  2. We define a class SortDescending with a private array attribute.
  3. We define a constructor that takes an array as an argument and assigns it to the class attribute.
  4. We define a sort method that sorts the array in descending order. We wrap the sorting operation in a try block to catch any ArrayIndexOutOfBoundsException.
  5. We define a printArray method that prints the elements of the array.
  6. In the main method, we create an instance of SortDescending, call the sort method to sort the array, and then call the printArray method to print the sorted array.

This problem has been solved

Similar Questions

Develop a Java program for sorting an integer array, floating-point array and character array. Use Generic method for the implementation. Finally print the sorted array. Note: consider the size of the array to be 5.

answer this question in simple code for beginners using intellJ: 4. Write a Java program to sort an array of given integers using the Merge Sort Algorithm.

Write a program to implement the Selection Sort algorithm to sort an array of integers in ascending order.Input:91 3 5 7 9 2 4 6 8Output:1 2 3 4 5 6 7 8 9Explanation:Initially, the minimum element in the array is 1. It is swapped with the first element, resulting in the array [1, 3, 8, 2, 5, 4]. Next, the minimum element in the remaining unsorted part is 2. It is swapped with the second element, resulting in [1, 2, 8, 3, 5, 4]. This process continues, and the final sorted array is [1, 2, 3, 4, 5, 8].

Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900

You are tasked with implementing one of the classic sorting algorithms: Bubble Sort, Insertion Sort, or Selection Sort. Your goal is to sort an array of integers in non-decreasing order using the chosen algorithm.Input FormatThe input consists of two lines:The first line contains an integer n (1 ≤ n ≤ 100), representing the number of elements in the array.The second line contains n space-separated integers, representing the elements of the unsorted array. These integers are in the range -1000 to 1000.ConstraintsImplement the chosen sorting algorithm to sort the array in-place. Do not use any built-in sorting functions.The array elements are integers in the range -1000 to 1000.Output FormatYou should output a single line with n space-separated integers, representing the elements of the array in non-decreasing order after applying the selected sorting algorithm.Sample Input 0102 3 4 5 1 6 7 8 9 0Sample Output 00 1 2 3 4 5 6 7 8 9Explanation 0It appears you have two lines of input. The first line contains the number 10, which likely represents the total number of elements in the list. The second line contains ten space-separated integers: 2 3 4 5 1 6 7 8 9 0. These are the elements you want to sort in non-decreasing order using a sorting algorithm.So, the sorted version of the list you provided is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, in non-decreasing order.Sample Input 162 4 5 4 6 2Sample Output 12 2 4 4 5 6

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.