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]
2 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]2 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
What term describes data actions being performed by endpoint devices, such as printing a report from a desktop computer?
Sloan [31]

Answer:

The correct answer to the following question will be "Data-in-use".

Explanation:

  • Data-in-use is an IT term referring to active information that is usually preserved in a semi-persistent physical state in RAM of computer, CPU registers or caches.
  • It might be created, modified or changed, deleted or accessed via different endpoints of the interface. This is indeed a useful term for IT departments to pursue institutional defense.

Therefore, it's the right answer.

4 0
2 years ago
Explain why the network administrator uses the hexadecimal
fomenos

Answer:

The main reason why we use hexadecimal numbers is because it provides a more human-friendly representation and is much easier to express binary number representations in hex than it is in any other base number system. Computers do not actually work in hex. Lets take an example, using a byte.

Hope this answer is right!!

5 0
3 years ago
What corrective action should be taken on a printer that prints unknown characters??
Lynna [10]
Reinstall the driver. 
7 0
3 years ago
What happens to energy as we move from step to step in a chain or web
scZoUnD [109]

Answer:Energy decreases as it moves up trophic levels because energy is lost as metabolic heat when the organisms from one trophic level are consumed by organisms from the next level.

Explanation:

6 0
3 years ago
Powerpoint provides only three picture effects. true or false.
scZoUnD [109]
False, there is a variety
4 0
3 years ago
Other questions:
  • Besides technical skill, what is the most important factor for staying employed and being offered promotions?
    6·2 answers
  • What is the advantage of defining a target user?
    6·1 answer
  • How many bits would be in the memory of a computer with 4kb memory?
    8·1 answer
  • If you use the assign software to a user option, how does the new software install to the user's computer? 70-411
    10·1 answer
  • Riya wants to save her project work in an external device so that she can show it to her class teacher. Help her to choose the d
    13·2 answers
  • In what ways do you think the media should function in a democratic society?
    9·1 answer
  • MacBook Pro (2019) at 93% max capacity battery rating after 100 charge cycles. Is this normal? Used coconut battery to check thi
    10·2 answers
  • You do not really know that you have a hand. Here is why. Imagine the possibility in which you are only a handless brain floatin
    14·1 answer
  • What are the steps for creating a bookmark? 1. Place the insertion point where the bookmark should appear. 2. Click the tab. 3.
    12·1 answer
  • What is the best method to avoid getting spyware on a machine
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!