At the end of the first exercise Mr. Sang suggested we try the
following
4. For your own exercise, please do the following
Create your own NetBeans project named as MyHashSet
Create your own HashSet object with initial capacity of 5
Add the following objects to the newly created HashSet object
2 String objects
2 MyOwnClass object (You will have to create MyOwnClass.java first)
3 Integer objects
Display the HashSet object
****** I'm a little confused by the HashSet data structure
This is what I came up with but I have doubts. Can any one tell me if
I am on the right track or explain it to me a little more.
I have posted the the code and results below:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
import java.util.HashSet;
/**
*
* @author Trey-Asus
*/
public class Main {
public static void main (String [] args)
{
HashSet hs = new HashSet(5);
MyOwnClass moc = new MyOwnClass("FirstName", "LastName",
1,2,3);
MyOwnClass moc1 = new MyOwnClass("FirstName2", "LastName2",
4,5,6);
System.out.println(" " + hs.add(moc.getFName()));
System.out.println(" " + hs.add(moc.getLName()));
System.out.println(" " + hs.add(moc.getFirstNumber()));
System.out.println(" " + hs.add(moc.getSecondNumber()));
System.out.println(" " + hs.add(moc.getThirdNumber()));
System.out.println(" " + hs.add(moc1.getFName()));
System.out.println(" " + hs.add(moc1.getLName()));
System.out.println(" " + hs.add(moc1.getFirstNumber()));
System.out.println(" " + hs.add(moc1.getSecondNumber()));
System.out.println(" " + hs.add(moc1.getThirdNumber()));
// Print out the HashSet object
System.out.println(hs);
}
}
/*
=============================================================================
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package MyHashSet;
class MyOwnClass{
String fName;
String lName;
int first;
int second;
int third;
public MyOwnClass (String fName, String lName, int first, int
second, int third)
{
this.fName = fName;
this.lName = lName;
this.first = first;
this.second = second;
this.third = third;
}
public String getFName()
{
return fName;
}
public String getLName()
{
return lName;
}
public int getFirstNumber()
{
return first;
}
public int getSecondNumber()
{
return second;
}
public int getThirdNumber()
{
return third;
}
}
/
*****************************************************************************/
Results
run:
true
true
true
true
true
true
true
true
true
true
[1, 2, 3, 4, 5, 6, FirstName2, LastName2, FirstName, LastName]
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en