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
A(n) ____ if function is one in which the action to be taken for the true or false case includes yet another if function.
spin [16.1K]
<span>A nested IF function is one in which the action to be taken for the true or false case includes yet another if function.
</span>Nested function is a function which is defined within another function, in this case it is defined within the IF function, which <span>returns one value if a certain predefined condition is true and another value if the condition is false. </span> 
6 0
3 years ago
Write a function so that the main program below can be replaced by the simpler code that calls function mph_and_minutes_to_miles
Blababa [14]

Answer:

Here is  code in python .

#function to calculate miles traveled

# pass two parameter in the function "miles_per_hour" and "minutes_traveled"

def mph_and_minutes_to_miles(miles_per_hour, minutes_traveled):

# convert minutes to hours

   hours_traveled = minutes_traveled / 60.0

   #calculate total miles traveled

   miles_traveled = hours_traveled * miles_per_hour

   #return the value

   return miles_traveled

#read input from user

miles_per_hour = float(input())

minutes_traveled = float(input())

#call the function and print the output

print('Miles: %f' % mph_and_minutes_to_miles(miles_per_hour, minutes_traveled))

Explanation:

Read the value of "miles_per_hour" and "minutes_traveled" from user. call the function mph_and_minutes_to_miles() with those two parameters.calculate total hours from the minutes_traveled by dividing it with 60. then calculate the total miles by multiply hours_traveled * miles_per_hour. then return the miles travelled.

Output:

20                                                                                                                        

150                                                                                                                        

Miles: 50.000000

4 0
3 years ago
People are more affected by technology than science because technology _____.
erik [133]
Has more of a direct influence in our everyday life because of the use of cellphones, technology, and cameras. Where else science is the idea that we should know affects us but often isn't clearly understood by the average person.

7 0
3 years ago
Read 2 more answers
The following system is ideal for representing large, sparsely-populated tables that have a need for real-time, read-write rando
SVETLANKA909090 [29]

Answer:

The correct answer is C. HBase.

Explanation:

HBase is a database system written in the Java programming language. It is open-source and can store enormous amounts of data in tables in billions of rows and columns. It enables random read and write operations in real time. In HBase, the query fetch time is less. It is designed to store denormalized data in wide and sparsely populated tables in contrast to the relational database management system.

4 0
4 years ago
When you're formatting in excel 2013 there are basically three levels of formatting, put the
RideAnS [48]

Answer:

The answer is "themes, styles, and direct formatting".

Explanation:

  • The selection of regular colors, font color, or type effects is also known as the Themes. There are many built-on Excel categories accessible mostly on page layout menu: an overview of its impact on the workbook could be accessed via a topic. You may find which various fonts will affect the design of your workbook.  
  • The style of a column is indeed a defined collection of layout tools like font and font color, numbers, cell borders, and cell shading. The range of cell styles incorporated, that can be added or modified. It could also alter or double a cell template to build your customizable cell model.
  • To use a customized format it includes the font, color, borders, etc. It is the direct formatting for the document at a certain time and anywhere. It selects the cell and starts changing the font from the font drop list to modify the font of its current document.
5 0
3 years ago
Other questions:
  • 1. You are creating a database for a web hosting service. Which of the following data types
    10·2 answers
  • An electronic resume is a plain-looking document. True of False?
    6·2 answers
  • Use this illustration to answer the question.
    15·1 answer
  • Operating systems that have windows and icons have which type of user interface?
    15·1 answer
  • To select a new theme for a slideshow use the blank tile in the ribbon
    9·2 answers
  • Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that man
    15·1 answer
  • Help...! Why might you trace an image<br> A. To edit an image<br> B. To make drawing easier
    14·1 answer
  • Describe how sharing and collaborating a spreadsheet can help the author of the spreadsheet.
    11·1 answer
  • Which comparison operator means ‘is not equal to’?
    9·1 answer
  • For python how do I ask the user how many numbers they want to enter and then at the ending add those numbers up together?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!