1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
schepotkina [342]
3 years ago
10

Write a Java program that asks the user to enter an array of integers in the main method. The program will ask the user for the

number of integer elements to be put in the array, and then ask the user for each element of the array. The program then calls a method named isSorted() that accepts an array of integers and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41, 56] and [2, 5, 3, 12, 10] respectively, the calls isSorted(arr1) and isSorted(arr2) should return true and false respectively. Assume the array has at least one integer element. A one-element array is considered to be sorted.
Computers and Technology
1 answer:
Alinara [238K]3 years ago
7 0

Answer:

The program is as follows:

import java.util.Scanner;

public class MyClass {

   public static Boolean isSorted(int [] arr){

       Boolean sorted = true;

       for(int i = 0;i<arr.length;i++){

       for(int j = i;j<arr.length;j++){

           if(arr[i]>arr[j]){

               sorted = false;

               break;

           }

       }    

       }

       return sorted;

   }

   public static void main(String args[]) {

       Scanner input = new Scanner(System.in);

       int n;

       System.out.print("Length of array: ");

       n = input.nextInt();

       int[] arr = new int[n];

       System.out.println("Enter array elements:");

       for(int i = 0; i<n;i++){

           arr[i] = input.nextInt();

       }

      Boolean chk = isSorted(arr);

     System.out.print(chk);

   }

}

Explanation:

The method begins here

This line defines a method named isSorted

   public static Boolean isSorted(int [] arr){

This line declares a boolean variable and initializes it to true

       Boolean sorted = true;

The next two iterations iterate through elements of the array

       for(int i = 0;i<arr.length;i++){

       for(int j = i;j<arr.length;j++){

This compares an array element with next elements (to the right)

           if(arr[i]>arr[j]){

If the next element is smaller than the previous array element, then the array is not sorted

               sorted = false; The boolean variable is updated to false

               break; This breaks the loop

           }

       }    

       }

This returns true or false, depending on the order of the array

       return sorted;

   }

The main method begins here

public static void main(String args[]) {

       Scanner input = new Scanner(System.in);

       int n;

This prompts user for length of array

       System.out.print("Length of array: ");

This gets length of array from user

       n = input.nextInt();

This declares an array

       int[] arr = new int[n];

This prompts user for array elements

       System.out.println("Enter array elements:");

The following iteration gets the array elements

<em>        for(int i = 0; i<n;i++){</em>

<em>            arr[i] = input.nextInt();</em>

<em>        }</em>

This calls the isSorted array      

     Boolean chk = isSorted(arr);

This prints true or false, depending if the array is sorted or not

     System.out.print(chk);      

   }

You might be interested in
Define Based Assessment ​
Oduvanchick [21]

Explanation:

In an educational setting, standards-based assessment is assessment that relies on the evaluation of student understanding with respect to agreed-upon standards, also known as "outcomes". The standards set the criteria for the successful demonstration of the understanding of a concept or skill -WIKIPEDIA

8 0
3 years ago
Samantha is in the beginning stages of OOP program development. What are the five steps she must follow for creating an OOP prog
lora16 [44]

Answer:

1. Classes and objects

2. Inheritance

3. Polymorphism

4. Data hiding/ encapsulation

5. Interfaces.

Explanation:

Classes and objects depict the major component of the OOP (object oriented programming). It explains the object like a ball in a soccer game development.

The inheritance is like the subclass of the object. Data hiding is a stage in oop where the codes or data are hidden from another users.

In the polymorphism stage, the object is given the ability to change to a sub-object, while in the interface stage a function or method signature is defined without implementing it.

3 0
4 years ago
Read 2 more answers
You want some points? whoever answers first gets 48 points. no cap. better be quick.
LekaFEV [45]

Answer:

4

Explanation:

5 0
3 years ago
Read 2 more answers
Kiara is using her software's graphic formats to create a line graph. As she
Ad libitum [116K]

Correct the data she entered , and the software will adjust the graph -apex

5 0
3 years ago
Read 2 more answers
Select the correct answer. Which statement is true about the guardian archetype?
kobusy [5.1K]

Answer:

They assist the hero in difficult situations

Explanation:

because its right

3 0
3 years ago
Other questions:
  • When an IRET instruction is executed, what actiontakes place?
    5·1 answer
  • Some files appear dimmed in one of the default folders on your computer. What would be the best course of action? A. Leave the f
    11·1 answer
  • Your bank contacts you asking you to phone a number supplied in the email.What do you?
    13·2 answers
  • Why should you thank the customer after resolving an issue?
    7·2 answers
  • Explain what all sensory receptors have in common.
    14·1 answer
  • A factory producing cables for personal computers finds that its current average
    10·1 answer
  • "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequen
    5·2 answers
  • The area of a square is stored in a double variable named area. Write an expression whose value is length of the diagonal of the
    6·1 answer
  • Write a loop to output the word EXAM 99 times
    8·1 answer
  • Write a paragraph about ICT in personal life?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!