Question

In: Computer Science

The following is a set of tree-set test programs that show the following outputs: Switch to...

The following is a set of tree-set test programs that show the following outputs: Switch to ArrayList, LinkedList, Vector, TreeMap, and HashMap to display similar output results.

Results:

Tree set example!

Treeset data: 12 34 45 63
Treeset Size: 4
First data: 12
Last Data: 63
Removing data from a tree set
Current tree set elements: 12 34 63
Current tree set size :3
Tree set empty.

Example code

import java.util.Iterator;
import java.util.TreeSet;

public class TreeDemo2 {
public static void main(String[] args) {
System.out.println("Tree set example!\n");
TreeSet<Integer> Tree = new TreeSet<Integer>();
Tree.add(12); Tree.add(63); Tree.add(34); Tree.add(45);

// Review Alignment
Iterator<Integer> iterator = Tree.iterator();
System.out.print("Treeset data: ");
// Display Tree Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();

// Review if empty
if (Tree.isEmpty()) {
System.out.print("Tree set is empty.");
} else {
System.out.println("tree set size: " + Tree.size());
}

// Retrieve the first data in a tree set
System.out.println("first data: " + Tree.first());

// Retrieve the last data in a tree set
System.out.println("last data: " + Tree.last());

if (Tree.remove(45)) { // Elimination of elements by value
System.out.println("Removing data from a tree set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current tree set elements: ");
iterator = Tree.iterator();

// Display Tree Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current tree set size: " + Tree.size());


// Remove All
Tree.clear();
if (Tree.isEmpty()) {
System.out.print("Tree set is empty.");
} else {
System.out.println("tree set size: " + Tree.size());
}
}
}

Please write it in Java code. And please write an annotation.

Thank you:)

Solutions

Expert Solution

Required Code in JAVA:

I have written 5 different codes for different collections and used the same values as used in the example given.

ArrayList:

import java.util.Iterator;
import java.util.ArrayList;

public class Main
{
   public static void main(String[] args) {
System.out.println("ArrayList example!\n");
ArrayList<Integer> ArrList = new ArrayList<Integer>();
ArrList.add(12); ArrList.add(63); ArrList.add(34); ArrList.add(45);
  
// Review Alignment
Iterator<Integer> iterator = ArrList.iterator();
System.out.print("ArrayList data: ");
// Display ArrList Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
  
// Review if empty
if (ArrList.isEmpty()) {
System.out.print("ArrList set is empty.");
} else {
System.out.println("ArrList set size: " + ArrList.size());
}
  
// Retrieve the first data in a ArrList set
System.out.println("first data: " + ArrList.get(0));
  
// Retrieve the last data in a ArrList set
System.out.println("last data: " + ArrList.get(ArrList.size()-1));
  
int index = ArrList.indexOf(45);
if (index!=-1) { // Elimination of elements by value
ArrList.remove(index);
System.out.println("Removing data from a ArrList set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current ArrList set elements: ");
iterator = ArrList.iterator();
  
// Display ArrList Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current ArrList set size: " + ArrList.size());
  
  
// Remove All
ArrList.clear();
if (ArrList.isEmpty()) {
System.out.print("ArrList set is empty.");
} else {
System.out.println("ArrList set size: " + ArrList.size());
}
}
}

LinkedList:

import java.util.Iterator;
import java.util.LinkedList;

public class Main
{
   public static void main(String[] args) {
System.out.println("LinkedList example!\n");
LinkedList<Integer> Link = new LinkedList<Integer>();
Link.add(12); Link.add(63); Link.add(34); Link.add(45);
  
// Review Alignment
Iterator<Integer> iterator = Link.iterator();
System.out.print("LinkedList data: ");
// Display Link Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
  
// Review if empty
if (Link.isEmpty()) {
System.out.print("Link set is empty.");
} else {
System.out.println("Link set size: " + Link.size());
}
  
// Retrieve the first data in a Link set
System.out.println("first data: " + Link.get(0));
  
// Retrieve the last data in a Link set
System.out.println("last data: " + Link.get(Link.size()-1));
  
int index = Link.indexOf(45);
if (index!=-1) { // Elimination of elements by value
Link.remove(index);
System.out.println("Removing data from a Link set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current Link set elements: ");
iterator = Link.iterator();
  
// Display Link Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current Link set size: " + Link.size());
  
  
// Remove All
Link.clear();
if (Link.isEmpty()) {
System.out.print("Link set is empty.");
} else {
System.out.println("Link set size: " + Link.size());
}
}
}

Vector:

import java.util.Iterator;
import java.util.Vector;

public class Main
{
   public static void main(String[] args) {
System.out.println("Vector example!\n");
Vector<Integer> Vec = new Vector<Integer>();
Vec.add(12); Vec.add(63); Vec.add(34); Vec.add(45);
  
// Review Alignment
Iterator<Integer> iterator = Vec.iterator();
System.out.print("Vector data: ");
// Display Vec Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
  
// Review if empty
if (Vec.isEmpty()) {
System.out.print("Vec set is empty.");
} else {
System.out.println("Vec set size: " + Vec.size());
}
  
// Retrieve the first data in a Vec set
System.out.println("first data: " + Vec.get(0));
  
// Retrieve the last data in a Vec set
System.out.println("last data: " + Vec.get(Vec.size()-1));
  
int index = Vec.indexOf(45);
if (index!=-1) { // Elimination of elements by value
Vec.remove(index);
System.out.println("Removing data from a Vec set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current Vec set elements: ");
iterator = Vec.iterator();
  
// Display Vec Set Data
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
System.out.println("Current Vec set size: " + Vec.size());
  
  
// Remove All
Vec.clear();
if (Vec.isEmpty()) {
System.out.print("Vec set is empty.");
} else {
System.out.println("Vec set size: " + Vec.size());
}
}
}

TreeMap:

import java.util.Iterator;
import java.util.TreeMap;
import java.util.Map;

public class Main
{
   public static void main(String[] args) {
System.out.println("TreeMap example!\n");
TreeMap<Integer, Integer> TMap = new TreeMap<Integer, Integer>();
TMap.put(12, 0); TMap.put(63, 0); TMap.put(34, 0); TMap.put(45, 0);
  
// Review Alignment
System.out.print("TreeMap data: ");
// Display TMap Set Data
int i=0;
Object first=0, last=0;
for(Map.Entry m:TMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
i++;
}
System.out.println();
  
// Review if empty
if (TMap.isEmpty()) {
System.out.print("TMap set is empty.");
} else {
System.out.println("TMap set size: " + TMap.size());
}
  
// Retrieve the first data in a TMap set
System.out.println("first data: " + first);
  
// Retrieve the last data in a TMap set
System.out.println("last data: " + last);
  
// int index = TMap.indexOf(45);
if(TMap.containsKey(45)){
TMap.remove(45);
System.out.println("Removing data from a TMap set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current TMap set elements: ");
  
for(Map.Entry m:TMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
}
System.out.println();
System.out.println("Current TMap set size: " + TMap.size());
  
  
// Remove All
TMap.clear();
if (TMap.isEmpty()) {
System.out.print("TMap set is empty.");
} else {
System.out.println("TMap set size: " + TMap.size());
}
}
}

HashMap:

import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

public class Main
{
   public static void main(String[] args) {
System.out.println("HashMap example!\n");
HashMap<Integer, Integer> HMap = new HashMap<Integer, Integer>();
HMap.put(12, 0); HMap.put(63, 0); HMap.put(34, 0); HMap.put(45, 0);
  
// Review Alignment
System.out.print("HashMap data: ");
// Display HMap Set Data
int i=0;
Object first=0, last=0;
for(Map.Entry m:HMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
i++;
}
System.out.println();
  
// Review if empty
if (HMap.isEmpty()) {
System.out.print("HMap set is empty.");
} else {
System.out.println("HMap set size: " + HMap.size());
}
  
// Retrieve the first data in a HMap set
System.out.println("first data: " + first);
  
// Retrieve the last data in a HMap set
System.out.println("last data: " + last);
  
// int index = HMap.indexOf(45);
if(HMap.containsKey(45)){
HMap.remove(45);
System.out.println("Removing data from a HMap set");
} else {
System.out.println("Data does not exist!");
}
System.out.print("Current HMap set elements: ");
  
for(Map.Entry m:HMap.entrySet()){
System.out.print(m.getKey()+" ");
if(i==0)
first = m.getKey();
else
last = m.getKey();
}
System.out.println();
System.out.println("Current HMap set size: " + HMap.size());
  
  
// Remove All
HMap.clear();
if (HMap.isEmpty()) {
System.out.print("HMap set is empty.");
} else {
System.out.println("HMap set size: " + HMap.size());
}
}
}


Related Solutions

Provide a comprehensive set of test cases for the two programs below. Do not provide actual...
Provide a comprehensive set of test cases for the two programs below. Do not provide actual test data, but rather describe each test. For example, if one of the programs was to search an array, you might include test cases such as search an array that is in ascending order, search an empty array, search for an element not in the array, etc. a. The program described in Question 4. Question 4 is A text file called coit20245.txt consists of...
SWITCH CASE, WHILE, DO WHILE STRUCTURES Implement the following programs, attach a flow chart, and photos...
SWITCH CASE, WHILE, DO WHILE STRUCTURES Implement the following programs, attach a flow chart, and photos of its operation. 1. Read a number that represents the month and say what months it is. 2. Using the conditional "while" calculate the factorial of a number 3. Using the “do-while” conditional, make a program that prints all numbers from 1 up to a maximum number entered by the user.
SWITCH CASE, WHILE, DO WHILE STRUCTURES, using ARDUINO. Urgent Please! Implement the following programs, attach a...
SWITCH CASE, WHILE, DO WHILE STRUCTURES, using ARDUINO. Urgent Please! Implement the following programs, attach a flow chart, and photos of its operation. 1. Read a number that represents the month and say what months it is. 2. Using the conditional "while" calculate the factorial of a number 3. Using the “do-while” conditional, make a program that prints all numbers from 1 up to a maximum number entered by the user.
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that...
give an example of a simple, undirected, weighted graph such that breadth-firstsearch outputs a search-tree that is not a single source shortest path tree. Youranswer must (a) Specify the graphG= (V, E)by specifyingVandE. (b) Specify the weight functionw. (c) Specify an ordering of the vertices and the search-tree output by breadth-first search assuming the specified graph and ordering. (d) Specify a valid single-source shortest path treeT= (V, ET)by specifyingETand its root, the first vertex in your specified ordering. (e) Include...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array...
C++(screenshot output please) Part 2 - Tree programs Program 1 Implement a tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
You have access two 7-segment decoder outputs from an existing circuit. These outputs show the numbers...
You have access two 7-segment decoder outputs from an existing circuit. These outputs show the numbers 00 through 15. Build a circuit to convert the two 7-segment displays, which display decimal, to one 7- segment display, which displays the correct hexadecimal number.   
Please show screenshot outputs and fully functional code for the Java program. Write the following methods...
Please show screenshot outputs and fully functional code for the Java program. Write the following methods to   1) read the content of an array of 5 doubles public static double[] readingArray() 2) find and print:the smallest element in an array of 5 double public static void smallest(double [] array) 3) find and print:the largest element in an array of 5 doubles pubic static void largest (double [] array) In the main method - invoke readingArray and enter the following numbers...
Apply the classification algorithm to the following set of data records. Draw a decision tree. The...
Apply the classification algorithm to the following set of data records. Draw a decision tree. The class attribute is Repeat Customer. RID Age City Gender Education Repeat Customer 101 20..30 NY F College YES 102 20..30 SF M Graduate YES 103 31..40 NY F College YES 104 51..60 NY F College NO 105 31..40 LA M High school NO 106 41..50 NY F College YES 107 41..50 NY F Graduate YES 108 20..30 LA M College YES 109 20..30 NY...
Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
  For the following Minitab outputs,      (i) name the test used     (ii) state Ho and Ha    (iii)...
  For the following Minitab outputs,      (i) name the test used     (ii) state Ho and Ha    (iii) conduct the test at a = .05 (identify and use the P-value if  it is available.) (a)  MTB > ttes A c3;         SUBC> alte -1.     Test of mu = A vs mu < 0     Variable     N      Mean    StDev   SE Mean         P‑Value     C3         200    3.8300   0.5403    0.0310         0.90       (i) Name: __________________________________      (ii) Ho: _________________ ,                                          Ha : _________________          (iii)                                                                                               (b)       MTB > let c3=c2‑c1      MTB > wtes 0 c3;      SUBC> alte ‑1.      TEST OF MEDIAN = 0.000000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT