Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finish my code please  RSS feed

 
bill pahceco
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
z
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bill pahceco wrote:You will write a program called Sets.java that will:

* Read two sets of numbers from the keyboard.
* Print the intersection of the two sets, in any order, without duplicates.
* Print the union of the two sets, in any order, without duplicates.

Sample Run #1

Just for illustration, what you type looks like this:

Enter values for first set, ending with -1
1 3 9 7 5 -1
Enter values for second set, ending with -1
2 3 5 7 -1
Intersection: 3 7 5
Union: 1 3 9 7 5 2




What i have so far:


sorry for bad indentation....









You may try this. It is better to use Sun's collection than using Strings and parse it in this kind of situations. But it has a limitation as provided by your procedure to add -1 as the end of the set so -1 cannot be a member of a set. Sorry for my indentation also.






package Sets;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class Sets {
int set = 0;
HashSet<Integer> hs1 = new HashSet<Integer>();
HashSet<Integer> hs2 = new HashSet<Integer>();

public HashSet<Integer> getHs1() {
return hs1;
}
public HashSet<Integer> getHs2() {
return hs2;
}

public static void main(String[] args) {
Sets sets = new Sets();
System.out.println("Enter values for first set, ending with -1");
sets.insert(sets.getHs1());
System.out.println("Enter values for second set, ending with -1");
sets.insert(sets.getHs2());
sets.getUnion(sets.getHs1(),sets.getHs2());
System.out.print("Union is "+sets.getUnion(sets.getHs1(), sets.getHs2()));
System.out.print("Intersection is "+sets.getIntersection(sets.getHs1(), sets.getHs2()));
}

public HashSet<Integer> getUnion(HashSet<Integer> hs1,HashSet<Integer> hs2){
HashSet<Integer> hs3 = new HashSet<Integer>();
HashSet<Integer> hs4 = new HashSet<Integer>();
hs3.addAll(hs1);
hs4.addAll(hs2);
hs3.addAll(hs4);
return hs3;
}

public HashSet<Integer> getIntersection(HashSet<Integer> hs1,HashSet<Integer> hs2){
HashSet<Integer> hs3 = new HashSet<Integer>();
Iterator<Integer> itr1 = hs1.iterator();
while(itr1.hasNext()){
Integer obj1=itr1.next();
Iterator<Integer> itr2 = hs2.iterator();
while(itr2.hasNext()){
Integer obj2=itr2.next();
if(obj1.equals(obj2)){
hs3.add(obj1);
}
}
}
return hs3;
}


public void insert(HashSet<Integer> hs){
Scanner in = new Scanner(System.in);
while (set != -1){
set = in.nextInt();
if (set != -1) {
hs.add(set);
}
}
set=0;
}
}

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, you should "finish" your code with a closing brace...

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to finish mine with a newline after the closing brace

__________________________________________________________
no luck, db
 
bill pahceco
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the code.
I havent even heard of HashSets, Iterator

so im not sure if that would fly for my instructor but
just wondering. what are they lol
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!