Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Need help with my assignment

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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.
    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();
    	}
    }
    Last edited by kohii; December 12th, 2011 at 09:54 AM. Reason: wrong code pasted


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  3. #3
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with my assignment

    Quote Originally Posted by 2by4 View Post
    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"

  5. #5
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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?

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with my assignment

    Quote Originally Posted by 2by4 View Post
    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.

  7. #7
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Need help with my assignment

    Quote Originally Posted by Mr.777 View Post
    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.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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

  9. #9
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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?

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with my assignment

    Quote Originally Posted by 2by4 View Post
    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.

  11. #11
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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.

  12. #12
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    BooleanSet newSet = new BooleanSet(2);

    But the following lines won't return an error even if they're already above the size I set.
    boolList.add(2,3);
    boolList.add(3,286);
    boolList.add(4,97);

  13. #13
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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..

  14. #14
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with my assignment

    Quote Originally Posted by kohii View Post
    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.
    BooleanSet newSet = new BooleanSet(2);

    But the following lines won't return an error even if they're already above the size I set.
    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)

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Help with Assignment
    By Nessera in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2011, 05:28 PM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. please help me in my assignment :(
    By asdfg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2010, 07:59 AM
  4. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 04:20 PM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM