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
Suppose a package pkg1 contains a class named MyClass and another package pkg2 contains a different class named MyClass. What ha
pogonyaev

Answer:

Following are the solution to this question:

Explanation:

In this question, two packages "pkg1 and pkg2" is defined, that hold a class that is "MyClass", in which the class name is the same. If we import the package so, it can't import both classes in the very same file. If we really need to apply to both, then must completely specify one class name, and in the class name is different so, we simply use both classes by creating the instance.  

7 0
3 years ago
What is work immersion and its nature​
Kobotan [32]
Work Immersion refers to the subject of the Senior High School Curriculum, which involves hands-on experience or work simulation in which learners can apply their competencies and acquired knowledge relevant to their track.
3 0
3 years ago
If you implement a Wireless LAN (WLAN) to support connectivity for laptops in the Workstation Domain, which domain does WLAN fal
Mariana [72]

Answer:

Remote access domain

Explanation:

5 0
4 years ago
Select the correct answer.
jenyasd209 [6]
I think it is D
Hope my answer help you?
5 0
3 years ago
Using the Vigenere cipher, does the length of the key matter?
Dafna1 [17]
Yes, the longer the Key the harder the encryption i believe
6 0
4 years ago
Other questions:
  • What is the disk bandwidth? the total number of bytes transferred total time between the first request for service and the compl
    13·1 answer
  • A camera in manual mode means of the camera will make all the adjustments for the photographer
    8·1 answer
  • What is the unit of electric power?
    15·1 answer
  • The CPU package is installed using the ____ process
    7·1 answer
  • No workplace is immune to the threat of workplace violence. true or false
    9·2 answers
  • What is the definition of Simplex transmission and Duplex Transmission?
    9·1 answer
  • Which of the following statements is NOT true regarding the Security Configuration and Analysis (SCA) tool?
    12·1 answer
  • Which statement best describes 'abstraction'? *
    5·1 answer
  • Given a MxN matrix.Print the elements in odd rows from left to right and the elements in even rows from right to left.(Java or C
    11·1 answer
  • GIVING 50 POINTS!
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!