Need help with my assignment
Hello!
I'm not new to Java, but my Java knowledge is very poor. My teacher gave me this assignment.
I need help in solving this because I have no idea how to do it.
-----
Create a boolean set class. A boolean set is a set containing only boolean values. The class should contain the following data members:
boolean set[] - This array is the data structure designated to be the set container wherein each index of the array represents the "real" value in the set while the boolean value just indicates if the associated index is an element of the set. So, if set[5] is equal to TRUE, then 5 is an element of the set.
int count - The data member responsible for keeping track of the number of elements/items in the boolean set. So the value of count should be updated upon insertion and deletion.
The class should also define 2 constructors:
public BooleanSet(int size) - where size is to be the size of the set.
public BooleanSet() - where the size of the set is to be 100.
Also, it should define the following methods:
public int cardinality() - returns the number of elements/items currently in the set.
public boolean isMember(int x) - returns TRUE if x is an element in the set, otherwise returns FALSE.
public boolean isEmpty() - returns TRUE if the set is empty, otherwise returns FALSE.
public boolean isFull() - returns TRUE is the set is full, otherwise returns FALSE.
public BooleanSet Union (BooleanSet another) - returns a boolean set that contains the union of this boolean set and BooleanSet another.
public BooleanSet Intersection (BooleanSet another) - returns a boolean set that contains the intersection of this boolean set and BooleanSet another.
-----
For far, this is what I have done.
Code :
import java.util.*;
class BooleanSet
{
boolean[] set = new boolean[100];
int count;
static ArrayList boolList;
public BooleanSet(int size) //where size is to be the size of the set
{
boolList = new ArrayList(size);
}
public BooleanSet() //where the size of the set is to be 100
{
boolList = new ArrayList(100);
}
public int cardinality() //returns the number of elements currently in the set
{
count = boolList.size();
return count;
}
public boolean isMember(int x) //returns true if x is an element in the set, otherwise returns false
{
return boolList.contains(x);
}
public static boolean isEmpty() //returns true if the set is empty, otherwise returns false
{
return boolList.isEmpty();
}
public boolean isFull() //returns true is the set is full, otherwise returns false
{
if (!(boolList.isEmpty()))
return true;
else
return false;
}
/*public BooleanSet Union (BooleanSet another) //returns a boolean set that contains the union of this boolean set and BooleanSet another
{
}
public BooleanSet Intersection (BooleanSet another) //returns a boolean set that contains the intersection of this boolean set and BooleanSet another
{
}*/
public static void main(String[] args)
{
System.out.println("This is a boolean set.");
BooleanSet newSet = new BooleanSet(2);
boolList.add(0,54);
boolList.add(1,10);
boolList.add(2,3);
boolList.add(3,286);
boolList.add(4,97);
System.out.println(boolList);
isEmpty();
}
}
Re: Need help with my assignment
1. Welcome to Java Programming Forums.
2. Where do you help need in the above given code? Is it throwing exception or generating errors? If yes, paste the whole error/exception message here.
Re: Need help with my assignment
Mr.777, easy:-) I think the OP needs help with the concepts.
kohii,
set and boolList are two fields of your BooleanSet class. What is each for? Explain the difference.
boolList is static and set is not? What is the difference, and why do they need to be different?
Answering these questions may help you clarify the problem in your mind.
Re: Need help with my assignment
Quote:
Originally Posted by
2by4
Mr.777, easy:-) I think the OP needs help with the concepts.
kohii,
set and boolList are two fields of your BooleanSet class. What is each for? Explain the difference.
boolList is static and set is not? What is the difference, and why do they need to be different?
Answering these questions may help you clarify the problem in your mind.
Read the section title. It's "What's Wrong with my Code?", not "Java Theory & Concepts"
Re: Need help with my assignment
Not general concepts -- concepts specific to the programming assignment he is working on.
Come on man, would you find it helpful to compile and run half a program you haven't figured out?
Re: Need help with my assignment
Quote:
Originally Posted by
2by4
Not general concepts -- concepts specific to the programming assignment he is working on.
Come on man, would you find it helpful to compile and run half a program you haven't figured out?
Read OP's problem description, (s)he has never mentioned what (s)he wants to do, just wrote that i need to make following assignment, this is what i have done so far, what else?
Do we read minds? For sure NO. How would we know where OP is stuck and where (s)he needs help. We can't just interrogate without knowing what (s)he wants.
Re: Need help with my assignment
Quote:
Originally Posted by
Mr.777
Read OP's problem description, (s)he has never mentioned what (s)he wants to do, just wrote that i need to make following assignment, this is what i have done so far, what else?
Do we read minds? For sure NO. How would we know where OP is stuck and where (s)he needs help. We can't just interrogate without knowing what (s)he wants.
Yes we can. We may be able to see errors that the OP cannot see.
For example, is the fact that something is not empty mean that it is full? We can ask the question.
We can ask positive questions that can help the OP ask the right questions.
Re: Need help with my assignment
There have been 7 posts so far in this thread and OP didn't even respond what (s)he needs. Why wasting time on a problem where OP is not even interested. Good Luck.
Note: 8 posts now :P
Re: Need help with my assignment
It's not about the personal OP. It's about the problem in the OP.
Anyone can learn from a thread, not just the OP.
Why let everyone down by falling out on the personal stuff?
Re: Need help with my assignment
Quote:
Originally Posted by
2by4
It's not about the personal OP. It's about the problem in the OP.
Anyone can learn from a thread, not just the OP.
Why let everyone down by falling out on the personal stuff?
There are thousands, infact millions of questions asked over the web, why don't you start replying each? Infact that's for everyone, not for the personal stuff. Stop wasting your time arguing with me but start posting the answers and wait to get the respond who actually posted the problem.
This is not, how it works, i appreciate your energy level for now but trust me, it's better to help who are really interested to get help than waiting for a response from a problem where the person is really not interested anymore or may be (s)he got the solution and didn't even bother to tell here.
Better stay on the topic and help if you can else no need to comment.
Re: Need help with my assignment
lol, because I am not a saint who is "just helping".
I am learning. I pick topics of interest to me, and learn from them.
I am learning how to read different coding styles, good code, bad code, simple code, complex code, spaghetti code, spotting mistakes, discussing concepts, understanding misconceptions (my own and those of others).
Cutting good code is half my job. Fixing bad code, which often requires understanding how other people conceptualize problems, is another part of my job.
So my message is, "comment, don't run away, even if the OP does".
OP may even be asleep right now, in another time zone.
Re: Need help with my assignment
Thanks everyone for checking out my topic. I was busy with my work that's why I haven't replied yet.
Anyway, I'm sure my code is a mess. They're actually copied concepts of what I have read from the internet.
I only know how to make a very simple class like "hello world", because Java needs classes to compile right?
At least that's what I can recall about Java.
My problem is that my code won't work as expected. There's no error when I compiled it.
In my code, I tried to set a size of 2.
Code :
BooleanSet newSet = new BooleanSet(2);
But the following lines won't return an error even if they're already above the size I set.
Code :
boolList.add(2,3);
boolList.add(3,286);
boolList.add(4,97);
Re: Need help with my assignment
answer post #3 if you don't want to waste too much time and effort on the wrong thing..
Re: Need help with my assignment
Quote:
Originally Posted by
kohii
Thanks everyone for checking out my topic. I was busy with my work that's why I haven't replied yet.
Anyway, I'm sure my code is a mess. They're actually copied concepts of what I have read from the internet.
I only know how to make a very simple class like "hello world", because Java needs classes to compile right?
At least that's what I can recall about Java.
My problem is that my code won't work as expected. There's no error when I compiled it.
In my code, I tried to set a size of 2.
Code :
BooleanSet newSet = new BooleanSet(2);
But the following lines won't return an error even if they're already above the size I set.
Code :
boolList.add(2,3);
boolList.add(3,286);
boolList.add(4,97);
Because the constructor you are using for ArrayList initially occupies that space in the memory and as soon as it tends to get extra elements, it appends it's size. For more information you can see ArrayList (Java Platform SE 6)
Re: Need help with my assignment
I asked my friend to make the code for me. This is already solved but I don't know how to mark the thread as solved. :(