Question

In: Computer Science

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

Solutions

Expert Solution

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

public class TestTreeSet {
        public static void main(String[] args) {
                // creating random object
                Random rand = new Random(5);
                // creating arraylist
                ArrayList<Integer> numAL = new ArrayList<>();
                // generating 10 random numbers and adding
                for (int i = 0; i < 10; i++)
                        numAL.add(rand.nextInt(7));
                // printing arraylist
                System.out.println(numAL);
                // creating TreeSet
                TreeSet<Integer> numTS = new TreeSet<>();
                // creating iterator
                Iterator<Integer> itr = numAL.iterator();
                // iterating through arraylist and adding elements to TreeSet
                while (itr.hasNext())
                        numTS.add(itr.next());

                // printing TreeSet
                System.out.println(numTS);

        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It...
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....
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 program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
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.
Given the following program readline.cpp and the data file e2_input.txt: // Program readline.cpp demonstrates how to...
Given the following program readline.cpp and the data file e2_input.txt: // Program readline.cpp demonstrates how to read a line of text from a file #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string str1, str2, str3, str4; // declares 4 variables ifstream inData; // declares input stream ofstream outData; // declares output stream inData.open("e2_input.txt"); // binds program variable inData to the input file "input.txt" outData.open("e2_output.txt"); // binds program variable outData to the output file "output.txt" //...
Code in Java, Use both Set and TreeSet in only one main to show the differences...
Code in Java, Use both Set and TreeSet in only one main to show the differences between the Set and TreeSet that Set still remove the duplicate but won't sort the elements. Create a class named DictionaryWord as: DictionaryWord - word: String                                                              - meanings: String + DictionaryWord (String word, String meanings) + getWord(): String + setWord (String word): void + getMeanings(): String + setMeanings(String meanings): void Write a program with the following requirements: Creates 8 DictionaryWord objects with: Word...
Which of the following can be done as part of the bank reconciliation process? Select all...
Which of the following can be done as part of the bank reconciliation process? Select all that apply. Select one or more: A. You can open and edit transactions listed on the reconciliation screen. B. Service charges and interest income not previously recorded can be entered. C. Transactions dated subsequent to the bank statement ending date can be hidden from view. D. New banking transactions (checks and deposits for example) can be entered.
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 program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT