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
emmasim [6.3K]
3 years ago
10

Write the following generic method that sorts an ArrayList of Comparable items. The sort method must use the compareTo method.

Computers and Technology
1 answer:
Inessa05 [86]3 years ago
6 0

Answer:

Explanation:

The following code is written in Java, it prompts the user to enter 10 integers and saves them to an ArrayList. Then it prompts for 5 strings and saves them to another ArrayList. Then, it calls the sort method and adds the lists as parameters. Finally, it prints out both lists completely sorted in ascending order.

import java.util.ArrayList;

import java.util.Scanner;

class Division{

   public static double division(double a, double b) throws Exception {

       if(b == 0)

           //throw new Exception("Invalid number.");

           return (a / b);

       return a / b;

   }

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       ArrayList<Integer> mylist2 = new ArrayList<>();

       for (int x = 0; x < 10; x++) {

           System.out.println("Please Enter a Number: ");

           int number = in.nextInt();

           mylist2.add(number);

       }

       ArrayList<String> mylist = new ArrayList<>();

       for (int x = 0; x < 5; x++) {

           System.out.println("Please Enter a Word: ");

           String word = in.nextLine();

           mylist.add(word);

       }

       sort(mylist);

       sort(mylist2);

       for (String x: mylist) {

           System.out.print(x + ", ");

       }

       System.out.println("");

       for (int x: mylist2) {

           System.out.print(x + ", ");

       }

   }

   public static <E extends Comparable<E>> ArrayList<E> sort(ArrayList<E> list) {

       E temp;

       if (list.size()>1) // check if the number of orders is larger than 1

       {

           for (int x=0; x<list.size(); x++) // bubble sort outer loop

           {

               for (int i=0; i < list.size() - x - 1; i++) {

                   if (list.get(i).compareTo(list.get(i+1)) > 0)

                   {

                       temp = list.get(i);

                       list.set(i,list.get(i+1) );

                       list.set(i+1, temp);

                   }

               }

           }

       }

       return list;

}}

You might be interested in
Emma has used copyright works of a number of artists in her photo album and cited all of them except one. She may be caught for
Goryan [66]
Is this a k12 question?
6 0
3 years ago
Read 2 more answers
Including quotes in your paper can bring additional meaning to the content True are false
kobusy [5.1K]
True if the quote is related 

3 0
3 years ago
Read 2 more answers
To track conversions in an app, you can add the firebase sdk to your app. sdk stands for:
Setler [38]
<span>To track conversions in an app, you can add the firebase SDK to your app. SDK stands for: Software Development Kit.
</span>SDK includes <span>value-add tools, utilities, and framework components that are intended to provide a productivity boost when implementing and also licences </span><span>
that make them unsuitable for building software intended to be developed under an incompatible license.
</span>

8 0
4 years ago
What is the use of folders in the computer system ?​
shusha [124]

Answer:

Folders help you keep your files organized and separate. Your documents, programs, and operating system files would all be located in the same place if you had no folders on your computer. Folders also allow you to have more than one file with the same file name.

Explanation:

Googoo said so.

4 0
3 years ago
Which processor is better for madvr
nadya68 [22]

Answer:

The i5.

Explanation:

It has a lower clock speed, but you do note that you have more cores, same with the cache memory.

8 0
4 years ago
Other questions:
  • Enter a formula in cell b7 to calculate the average value of cells b2:b6
    13·1 answer
  • What does Mishler mean by the foreground-background interaction? How can this affect the photographs, based on the examples in t
    14·2 answers
  • After a conditional formatting rule is created using the ____ dialog box, the rule is then available for use in the database fro
    11·1 answer
  • Python
    5·1 answer
  • Which of the following is false? A. The last element of an array has position number one less than the array size. B. The positi
    9·1 answer
  • Describe the effects technology, particularly computers, has had on mechanical labor.
    8·1 answer
  • What kind of charger can i use for this
    10·2 answers
  • Write the pseudocode for this flowchart
    6·1 answer
  • Explain with examples what is software​
    6·1 answer
  • Jeff wants to create a website with interactive and dynamic content. Which programming language will he use?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!