Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How do I return this class type?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9574
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nvm.
 
Paul Clapham
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hopefully that means your problem is solved!
 
Mike Lonsky
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic