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

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" //...
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.
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...
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 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...
1.All of the following can be done when the resources that provide capacity are fixed EXCEPT:a....
1. All of the following can be done when the resources that provide capacity are fixed EXCEPT:a. workforce schedulingb. production schedulingc. both a and b can be done when the resources that provide capacity are fixedd. none of the above2. Which of the following BEST describes process management?a. it increases capacity by removing constraint points (bottlenecks) in the systemb. it can increase capacity by redesigning workflowsc. techniques such as queuing theory rearrange resources to achieve more customers in the systemd....
Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1...
Perform the following tasks with NumPy arrays. All of them can be done (elegantly) in 1 to 3 lines. (a) Create an 8 × 8 array with ones on all the edges and zeros everywhere else. (b) Create an 8 × 8 array of integers with a checkerboard pattern of ones and zeros. (c) Given the array c = np.arange(2, 50, 5), make all the numbers not divisible by 3 negative. (d) Find the size, shape, mean, and standard deviation...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT