Question

In: Computer Science

TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It...

  1. TreeSetDemo

IN JAVA PLEASE

The following program can be done all in the main method. It demonstrates that a TreeSet eliminates duplicates and is ordered.

Create a Random object with a seed of 5.

Create an ArrayList numAL of type Integer

Populate numAL with 10 numbers randomly selected from 0 to 6

Print out numAL

Create a TreeSet numTS of type Integer

Create an Iterator alIter from numAl and use it to add all the elements of numAL in numTS.

Print out numTS

Sample output:

ArrayList: [2, 4, 4, 0, 4, 0, 1, 5, 5, 5]

TreeSet: [0, 1, 2, 4, 5]

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;

public class TreeSetDemo {

        public static void main(String[] args) {
                
                // instantiating random class
                Random rand = new Random();
                
                // setting seed to 5
                rand.setSeed(5);
                int randMax = 6;
                ArrayList<Integer> numAL = new ArrayList<>();
                
                // generating 10 random numbers and adding to numAL
                for(int i=0 ; i<10 ; i++) {
                        int num = rand.nextInt(randMax + 1);
                        numAL.add(num);
                }
                System.out.println("ArrayList : "+numAL);
                
                // instantiating TreeSet and Iterator
                TreeSet<Integer> numTS = new TreeSet<>();
                Iterator<Integer> allIter = numAL.iterator();
                
                // iterating through every element and adding to TreeSet
                while (allIter.hasNext()) { 
                        numTS.add(allIter.next());
        }
                
                System.out.println("TreeSet   : "+numTS);
                
        }

}

Code

Output


Related Solutions

The following program can be done all in the main method. It demonstrates that a TreeSet...
The following program can be done all in the main method. It demonstrates that a TreeSet eliminates duplicates and is ordered. Create a Random object with a seed of 5. Create an ArrayList numAL of type Integer Populate numAL with 10 numbers randomly selected from 0 to 6 Print out numAL Create a TreeSet numTS of type Integer Create an Iterator alIter from numAl and use it to add all the elements of numAL in numTS. Print out numTS
Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user...
Java Program Please Read all directions carefully Write a method named smallToLarge that asks the user to enter numbers, then prints the smallest and largest of all the numbers typed in by the user and the average (rounded to 2 decimal places). You may assume the user enters a valid integer number for the number of numbers to read. Here is an example dialogue: /* initialize smallest and largest variables with the 1st user input for Number */ How many...
This program must be done in JAVA. Write a program to monitor the flow of an...
This program must be done in JAVA. Write a program to monitor the flow of an item into an out of a warehouse. The warehouse has numerous deliveries and shipments for this item (a widget) during the time period covered. A shipment out (an order) is billed at a profit of 50% over the cost of the widget. Unfortunately each incoming shipment may have a different cost associated with it. The accountants of the firm have instituted a last-in, first...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT