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

Read programs and find out their outputs. Please provide process tree with our answer to get...
Read programs and find out their outputs. Please provide process tree with our answer to get full points. (a) #include <unistd.h> #include <stdlib.h> #include <stdio.h> int main() { for(i=0;i<3;i++) if(fork()>0) printf(“A\n”); } (b) #include <unistd.h> #include <stdlib.h> #include <stdio.h> int gVar=0; int main() { int lVar=0; int *p; p=(int *)malloc(sizeof(int)); int pid=fork(); if(pid>0){ gVar=1; lVar=1; *p=1; printf("%d, %d, %d\n", gVar, lVar, *p); }else{ gVar=2; lVar=2; *p=2; printf("%d, %d, %d\n", gVar, lVar, *p); } } (c) Read the following code and...
Draw a schematic for a DPDT switch and show how it can be wired to switch...
Draw a schematic for a DPDT switch and show how it can be wired to switch two separate circuits on and off. : Why is it not possible to connect sensors such as thermocouples, strain gages, and accelerometers directly to a digital computer or microprocessor? : What is successive Approximation, discuss them in detail.
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...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is...
Problem set Rewrite the following if statement as an equivalent switch statement. The variable digit is of type int. if (digit == 0) value = 3; else if (digit == 1) value = 3; else if (digit == 2) value = 6; else if (digit == 3)     value = 9; The decision table below shows fines imposed for speeding violations. Write a code segment that assigns the correct fine to type double variable fine based on the value 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.
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels...
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2^(height+1) - 1.) /** Returns true if the tree is a perfect binary tree */ public boolean isPerfectBST() Class Name: Exercise25_03
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT