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
telo118 [61]
3 years ago
9

Write a Java program HW2.java that asks the user to enter an array of integers in the main method. The program should prompt the

user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of 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, 30, 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 element. A one-element array is considered to be sorted.
Computers and Technology
1 answer:
tino4ka555 [31]3 years ago
7 0

Answer:

The java program for the given scenario is given below.

This program can be saved as HW2.java.

import java.util.Scanner;

public class HW2 {

   public static void main(String args[]) {

      HW2 ob = new HW2();  

     Scanner sc = new Scanner(System.in);

     int len;      

     System.out.println( "Enter the number of elements to be entered in the array");

     len = sc.nextInt();

     int[] arr = new int[len];

     System.out.println( "Enter the elements in the array");

     for( int k=0; k<len; k++ )

     {

         arr[k] = sc.nextInt();

     }

     boolean sort = ob.isSorted(arr, len);

     System.out.println( "The elements of the array are sorted: " + sort );

   }    

   public boolean isSorted( int a[], int l )

   {

       int sort = 0;

       if( l==1 )

           return true;

       else

       {        

           for( int k=1; k<l; k++ )

           {

               if(a[k-1] < a[k])

                   continue;

               else

               {

                   sort = sort + 1;

                   break;

               }

           }

           if(sort == 0)

               return true;

           else

               return false;

       }

   }

}

OUTPUT

Enter the number of elements to be entered in the array

3

Enter the elements in the array

1

2

1

The elements of the array are sorted: false

Explanation:

1. First, integer variable, len, is declared for length of array. User input is taken for this variable.

2. Next, integer array is declared having the size, len. The user is prompted to enter the equivalent number of values for the array.

3. These values are directly stored in the array.

4. Next, the function isSorted() is called.

5. This method uses an integer variable sort which is declared and initialized to 0.

6. Inside for loop, all elements of the array are tested for ascending order. If the 1st and 2nd elements are sorted, the loop will continue else the variable sort is incremented by 1 and the loop is terminated.

7. Other values of the variable sort indicates array is not sorted.

8. A Boolean variable, sort, is declared to store the result returned by isSorted method.

9. Lastly, the message is displayed on the output whether the array is sorted in ascending order or not.

You might be interested in
If a storm is 7.5 kilometers away, how much time is expected between observations of lightning and thunder? Round your answer to
Svetlanka [38]

Answer:

22.5 s is the correct answer to the given question .

Explanation:

Given that

d=7.5km=7.5\times 10^3 m

For light:

We know that speed of light c

c=3\times 10^8 m/s

We know that

Distance = Speed \times timed=C\time tt=\dfrac{D}{t}t=\dfrac{7.5\times 10^3}{3\times 10^8}\ st=2.5\times 10^{-5}st=0.025 ms

For thunder :

Speed of sound ,v=332 m/s

Time\  taken\  by\  sound \\t\’=\dfrac{7500}{332}=22.59 s

Therefore the difference between time is given as follows

\Delta t= t' - \ t=22.59-2.5\times 10^{-5}=22.59 s

22.5 is the answer

4 0
3 years ago
Henry bought a new card for capturing television on his computer. When he finished going through the packaging, though, he found
viva [34]

Answer:

reclamar

Explanation:

entonces tiens q ir a la misma tienda

6 0
3 years ago
A type of cpu socket, used with modern intel processors, where the cpu itself has no pins but the contact pads of the cpu line u
yawa3891 [41]
<span>A type of cpu socket, used with modern intel processors, where the cpu itself has no pins but the contact pads of the cpu line up with socket pins on the motherboard, is known as LGA.  </span>The Land Grid Array (LGA<span>) is a type of surface-mount packaging for ICs (</span> integrated circuits)<span> that is notable for having the pins on the </span>socket<span> (when a </span>socket is used) rather than the integrated circuit.
3 0
4 years ago
Read 2 more answers
All of the following are ways to limit what is tracked about you online, except:
Veseljchak [2.6K]

Answer:b

Explanation: turning off your screen does nothing on the cyber side and people and companies can still access your internet history even while your screen is off

5 0
3 years ago
¿Qué es lo contrario a frágil?
Pepsi [2]

Answer:

frágil

Explanation:

7 0
3 years ago
Other questions:
  • What does the hard disk drive do? It stores all of the information on a computer. It controls a computer’s operating system. It
    12·2 answers
  • Is spread spectrum transmission done for security reasons in commercial WLANs?
    13·1 answer
  • Is it safe to take apart a computer monitor?
    13·1 answer
  • What are interpersonal skills?
    12·2 answers
  • According the Keown book, which Web-based personal financial planning website is known as "the best free way to manage your mone
    6·1 answer
  • Ayudaa, 5 ejes de sistemas tecnológicos
    5·1 answer
  • If you are a mathematics student and want to study the secret of writing cryptographic codes, algorithms, encryption and decrypt
    14·1 answer
  • A network administrator determines who may access network resources by assigning users
    10·1 answer
  • What is a word processing program? Give examples of word processing programs.
    10·1 answer
  • . A binary search has a right subtree but no left subtree. What noes contains that least element in the tree
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!