Simple Collection Manipulation in Java - Sets, Trees, and Vectors

  • Context: Java 
  • Thread starter Thread starter muna580
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a Java programming assignment involving the manipulation of collections, specifically focusing on Sets, Trees, and Vectors. Participants are exploring how to correctly implement the requirements of the assignment and clarify their understanding of the expected outputs.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the assignment, particularly regarding the creation of vector2 and its expected contents compared to vector1.
  • Another participant suggests declaring vector2 and using a copy function, such as cloning vector1, or creating a custom clone function to iterate through vector1.
  • A participant shares their code implementation and notes that the output for TreeSet and Vector only shows "Mary," questioning if more elements should be included.
  • Some participants confirm that the output appears correct but inquire about the expected output beyond "Mary." They also ask if the output should be displayed in brackets.
  • There is a question about whether the assignment specified any formatting requirements for the output, such as brackets.

Areas of Agreement / Disagreement

Participants generally agree that the outputs they are receiving are correct based on their implementations. However, there is uncertainty regarding what the expected output should be, particularly concerning the presence of additional elements and formatting.

Contextual Notes

Participants have not clarified the specific requirements of the assignment regarding the expected outputs for the collections, leading to some ambiguity in their discussions.

muna580
My professor told me to do everything in this program as asked. But I am a little bit confused.

In the code below, take a look at the part I made in BOLD. Well, first of all, he said to display the elements in vector1. But there is only 1 element in vector1. Well, I undestand its printable, but look at the next step. He said to create vector2 an add all elements in vector1 to vector 2. Basically, I created the same thing all over again with a diffrent name. What ever I printed out in vector1 will be printed out in vector2.

import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;

class CollectionExtraCredit
{

public static void main(String[] arg)
{
Iterator i;

Set s1 = new HashSet();
s1.add("Mary");
// Add the following names - Bernadene, Elizabeth, Gene, Elizabeth, Clara
i = s1.iterator();

System.out.println("HashSet s1 \n" + display(i));

// Create another HashSet object set2
// Add the following members - John, Jerry, Elizabeth
// Add set2 to set1
// Display set2

Set tree1 = new TreeSet();
tree1.add("Mary");
i = tree1.iterator();

// Next, use the same data to create TreeSet tree1 - I did it for you.
// Display the elements of tree1
// Create tree2 and add the elements in tree1 to tree2
// Display tree2
System.out.println("TreeSet s1 \n" + display(i));


List vector1 = new Vector(5);
vector1.add("Mary");
// Thirdly,use the same data again to create Vector vector1 - I did it for you.
// Display the elements of vector1

// Create vector2 and add the elements in vector1 to vector2
// Display vector2
i = vector1.iterator();
System.out.println("Vector vector1 \n" + display(i));

}

static String display(Iterator i)
{
if (!i.hasNext())
return "";
else
return i.next() + "\n" + display(i);
}
}
 
Technology news on Phys.org
You haven't declared vector2. Do so, and then you need to use a copy function. e.g. Vector vector2 = vector1.clone();

Or, and this might be better practice for you, is to make your own clone function that creates a new vector and iterates through vector1, adding all the objects in vector1 to the new vector, and returns the new vector. Then you can say Vector vector2 = clone(vector2);
 
This is what I did. But I am not sure if it is correct becaue for TreeSet, and Vector, only get Mary as the output. Am I suppoe to add something else?

Code:
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;

class CollectionExtraCredit
{

	public static void main(String[] arg)
	{
		Iterator i;
		
		Set s1 = new HashSet();
		s1.add("Mary");

		// Add the following names - Bernadene, Elizabeth, Gene, Elizabeth, Clara
		
		s1.add("Bernadene");
		s1.add("Elizabeth");
		s1.add("Gene");
		s1.add("Elizabeth");
		s1.add("Clara");

		i = s1.iterator();
		
		System.out.println("HashSet s1 \n" + display(i));
		
		// Create another HashSet object set2 
		// Add the following members - John, Jerry, Elizabeth
		// Add set2 to set1 
		// Display set2
		
		Set s2 = new HashSet();
		s2.add("John");
		s2.add("Jerry");
		s2.add("Elizabeth");
		
		s2.addAll(s1); 
		
		i = s2.iterator();
		
		System.out.println("HashSet s2 \n" + display(i));		Set tree1 = new TreeSet();
		tree1.add("Mary");
		i = tree1.iterator();
		System.out.println("TreeSet tree1 \n" + display(i));
		
		// Next, use the same data to create TreeSet tree1 - I did it for you.
		// Display the elements of tree1
		// Create tree2 and add the elements in tree1 to tree2
		// Display tree2
		
		Set tree2 = new TreeSet();
		tree2.addAll(tree1);
		i = tree2.iterator();
		System.out.println("TreeSet tree2 \n" + display(i));

		
		List vector1 = new Vector(5);
		vector1.add("Mary");
		// Thirdly,use the same data again to create Vector vector1 - I did it for you.
		// Display the elements of vector1
		i = vector1.iterator(); 
		System.out.println("Vector vector1 \n" + display(i));

		// Create vector2 and add the elements in vector1 to vector2
		// Display vector2
		
		List vector2 = new Vector(5);
		vector2.addAll(vector1);
		i = vector2.iterator();
		System.out.println("Vector vector2 \n" + display(i));
	}
	
	static String display(Iterator i)
	{
		if (!i.hasNext())
			return "";
		else
			return i.next() + "\n" + display(i);
	}
}

This is my output.

HashSet s1
Gene
Bernadene
Clara
Mary
Elizabeth

HashSet s2
Jerry
Bernadene
Gene
Mary
Clara
Elizabeth
John

TreeSet tree1
Mary

TreeSet tree2
Mary

Vector vector1
Mary

Vector vector2
Mary

Press any key to continue...
 
Last edited by a moderator:
Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?
 
0rthodontist said:
Well, it looks correct to me, but you said you expected to get something other than "Mary"? What output should you get?

One more thing, is there suppose to be any output in bracket? Like for example

[Mary]

is that suppsoe to happen?
 
Did the assignment ask for your output to be in brackets?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K