• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I return this class type?  RSS feed

 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm confused as to how I'm supposed to return a new CharacterSet type with an Array? I'm trying to add it to the new array, but I have to return a CharacterSet type? (union method) I'm really confused. Any help/tip would be helpful.
I also tried creating an ArrayList<CharacterSet> newArray = new ArrayList<CharacterSet>() but I'm unable to return it still without changing my type. How else can I go about this?
Thank you.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have to return a CharacterSet, then at least in this case the first thing you should do is to create one.

I don't think your nested loops are the way to go either, but at least then you'd have a CharacterSet object to add characters to. However the obvious question then, at the empty space where you have "// add to new array", is "Which character should I add now?" So what I'd do instead is this: add all the characters from this CharacterSet to the new one you just created. Then add all the characters from the other one. You don't need logic to check whether you're adding duplicates in this method because the addChar method already has that logic.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd also point out that your addChar() method has a flaw in it, but put that aside until you get your current question cleared up. Hopefully testing should uncover the flaw anyway.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your union() method you are not returning a CharacterSet but an ArrayList<Character>, so the compiler should be complaining about that. You probably want to return a CharacterSet but in order to do that you'd need a constructor that will build one given an ArrayList. In you union() method you use newArray, because this is the only place it is used you can make it a local variable instead of a class field, as you currently have. Then, at the end of union(), return new CharacterSet(newArray).

cardinality(): You could just return letter.size().

isEmpty(): Always returns false.

Line 24: isLetter is never used.

By convention you should never compare something to false (or true), use

or

 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nvm.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully that means your problem is solved!
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Hopefully that means your problem is solved!


I think I found the problem in add char! I was checking if the char was in the ArrayList before I turned it into a lower case thus it was still adding the char twice. Thanks for the heads up! Really appreciate the help guys.
 
Paul Clapham
Sheriff
Posts: 22831
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Lonsky wrote:I think I found the problem in add char! I was checking if the char was in the ArrayList before I turned it into a lower case thus it was still adding the char twice.


Yup. My take on it was that you could add 'c' twice by passing 'c' and then 'C'. Good work!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably nitpicking - and you certainly shouldn't obsess about it - but it's sometimes worth considering efficiency when you write a method.

Your implementation of addChar(), for example, currently does something like this:Now which of those two "don't add" conditions do you think takes longer? Checking if a char is a letter, or searching your List to see if it's already there?

It may not be obvious, in which case don't worry about it; but one thing should be fairly clear: isLetter() is likely to take constant time, whereas contains() will take longer as the size of your List increases.
In fact, all isLetter() has to do is check a few ranges, so it's extremely fast, which means that you're probably better off making it your first check, viz:A couple of general rules that are worth remembering:
If you have two (or more) conditions in sequence:
  • Put the one that eliminates the most cases first.
  • Put the one that evaluates quickest first.
  • Like I say, if you don't know either of those things, don't worry about it (and don't guess ), but sometimes it's worth thinking about.

    Also: notice the use of 'else if' - it saves a lot of unnecessary indentation.

    HIH

    Winston
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!