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
What is the primary responsibility of the physical layer in data communication?
melamori03 [73]
 It is responsible for transmitting these bits as signals over a wire, optical fiber, wireless, or other <span>medium.</span>
3 0
3 years ago
What is the device used for the calculations of payment and balancing the accounts record s​
Greeley [361]
A calculator or bank? Maybe an atm
6 0
1 year ago
Read 2 more answers
How can you tell if a website is biased?
Leona [35]

Answer:

the author has not considered other points of view.

7 0
1 year ago
Which of the following is true about simulation games? A. Simulation games involve competing in a sport against other players. B
WARRIOR [948]
<span>Simulation games recreate a real-world environment. </span>
6 0
3 years ago
Read 2 more answers
To distinguish advertising, which typically is conveyed via print, TV, radio, the Internet, etc., from person-to-person forms of
neonofarm [45]

Answer: Mediated

Explanation:

 Mediated communication is the type of communication in which the information are carried out so that it can be used in various types of communication technology.

It is also known as mediated interaction and it can be used as face to face communication.

The mediated communication are typically used to distinguish the various form of communication. The mediated communication are not only computerized but also we can communication by using pen and paper.  

3 0
3 years ago
Read 2 more answers
Other questions:
  • Which cisco ios command is used to display the current ospf neighbors and their rids?
    11·1 answer
  • Enterprise systems help managers and companies improve their performance by enabling them to __________.
    7·1 answer
  • In order to average together values that match two different conditions in different ranges, an Excel user should use the functi
    6·1 answer
  • HELP FOR JAVASCRIPT: 01. What is prototypical inheritance? 02. How can JavaScript be used to improve accessibility on the web? I
    6·1 answer
  • George wants to pursue a career in web technology as a web developer. Which certifications can help him as a web developer?
    5·1 answer
  • You work in an office that uses Linux servers and Windows servers. The network uses both the TCP/IP protocol. The Linux server i
    11·1 answer
  • Write a class Bug that models a bug moving along a horizontal line. The bug moves either to the right or left. Initially, the bu
    13·1 answer
  • What is the output for the following code?<br><br> print (5*4+5)
    11·1 answer
  • Como se llaman las herramientas y maquinas pque utilizan para hacer cosas artesanales
    10·1 answer
  • If the disaster requires actions offsite from the primary infrastructure, it is under the jurisdiction of__________.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!