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
Create a Java Program to calculate compound interest. This can all be done in the main()...
Create a Java Program to calculate compound interest. This can all be done in the main() method. Create a double variable named currentBalance. Create a double variable named newBalance. Create a final double variable named INTEREST_RATE and assign 1.05 to it. Create a int variable named yearCount. Prompt for the "Number of years to invest" and store the input into the yearCount variable. Prompt for the "Amount to Invest" and store this in the currentBalance variable. Assign currentBalance to newBalance....
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...
Design a complete JAVA program with the following methods:- In the main method : 3 variables...
Design a complete JAVA program with the following methods:- In the main method : 3 variables are declared :- employee id, salary and status. A method that will allow user to input employee id and salary. A method that will assign status to “Taxable’ if employee’s salary is more than 2500RM or “Not Taxable” if salary is less and equal to 2500RM. A method that will display the employee’s id , salary and its status. Write the above Using Java.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT