We help IT Professionals succeed at work.

Testing individual methods from a class

coolgem
coolgem asked
on
365 Views
Last Modified: 2010-03-31
I do not understand and know how to test and what individual methods in the below Java class.  I don't know for each method how to pass either characters,strings for the respective methods.

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
 * Java class to add chracters &/or a string of characters to a
 * default alpha table for use by developers for testing an input
 * string to be valid for developer's objective
 *
 *
 * Creation date:     2004/02/10
 * Modification date: 0000/00/00
 * @author: TJW
 */
public class ValidCharactersChecker {

    private char[] initialChars = {'a','b','c','d','e','f','g',
                                                 'h','i','j','k','l','m','n',
                                                 'o','p','q','r','s','t','u',
                                                 'v','w','x','y','z','A','B',
                                                   'C','D','E','F','G','H','I',
                                                 'J','K','L','M','N','O','P',
                                                 'Q','R','S','T','U','V','W',
                                                 'X','Y','Z'};
    private TreeSet validCharacterSet = null;

    public ValidCharactersChecker() {
        validCharacterSet = new TreeSet();
            this.addChars(initialChars);
    } // 21 c'tor

    public void addChars(char[] c) {
            int numChars = initialChars.length;
          for (int i = 0;i <numChars;++i ){
                  Character myChar = new Character(initialChars[i]);
                  addChar(myChar);

          }
      }
    //31 input of string example: ("-,.")
    public void addChars(String s){

          int numChars = s.length();
        for (int i = 0;i < numChars;++i){
                  char k = s.charAt(i);
                  Character myChar = new Character (k);
                  addChar(myChar);
        }

    }

    public void addChar(char c) {
        validCharacterSet.add(new Character(c));
    }
    public void addChar(Character validChar) {
        validCharacterSet.add(validChar);
    }

    public void removeChar(char invalidChar) {
        validCharacterSet.remove(new Character(invalidChar));
    }

    public void removeChar(Character invalidChar) {
        validCharacterSet.remove(invalidChar);
    }

    public char[] getValidCharacters() {
        int numChars = validCharacterSet.size();
        char[] validCharArray = new char[numChars];

        Iterator iter = validCharacterSet.iterator();
        int i = 0;
        while(iter.hasNext()) {
            Character c = (Character) iter.next();
            validCharArray[i] = c.charValue();
            ++i;
        } // while
        return validCharArray;
    } // getChars

      //overloaded method
      public boolean isValid(String s) {
            int slen = s.length();
            for (int i =0;i < slen;i++){
                  char k = s.charAt(i);
                  boolean bull = this.isValid(k);
                  if (false == bull) {
                     return false;
               }
            }
            return true;
      }

      //overloaded method
      public boolean isValid(char c) {
        Character d = new Character(c);
            if (validCharacterSet.contains (d))
             return true;
          else
             return false;
      }


      public Set getValidCharacterSet() {
        return Collections.unmodifiableSet(validCharacterSet);
    } // getChars
}  
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
I'm getting a feeling of deja vu - what's going on ;-) ?
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Mr. CEHJ,
Yes im the same one.  The solution will help me and I can then use it as an example. By the way, I wan't rying to tell you what to do yesterday. I'm just in need of help.  Maybe I'm not expl;aining myself correctly.
Best Regards
CG
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>By the way, I wan't rying to tell you what to do yesterday.

Don't worry - i wasn't addressing that remark to you. Please close this question and return to the old one. Post a message for 0 points to Community Support and they'll give your points back to you for this one.

You may not know this, but you're not allowed to have two accounts.

I'll help you in the old one

Author

Commented:
CEHJ
I'M TRYING TO GET RID OF THE OLD ONE BECAUSE IT HAS MY NAME IN THE CLASS.
Under coolgem account the author name is not in the class.

import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
/**
 * Java class to add chracters &/or a string of characters to a
 * default alpha table for use by developers for testing an input
 * string to be valid for developer's objective
 *
 *
 * Creation date:     2004/02/10
 * Modification date: 0000/00/00
 * @author: TJW
 */
public class ValidCharactersChecker {

    private char[] initialChars = {'a','b','c','d','e','f','g',
                                                 'h','i','j','k','l','m','n',
                                                 'o','p','q','r','s','t','u',
                                                 'v','w','x','y','z','A','B',
                                                   'C','D','E','F','G','H','I',
                                                 'J','K','L','M','N','O','P',
                                                 'Q','R','S','T','U','V','W',
                                                 'X','Y','Z'};
    private TreeSet validCharacterSet = null;

    public ValidCharactersChecker() {
        validCharacterSet = new TreeSet();
            this.addChars(initialChars);
    } // 21 c'tor

    public void addChars(char[] c) {
            int numChars = initialChars.length;
          for (int i = 0;i <numChars;++i ){
                  Character myChar = new Character(initialChars[i]);
                  addChar(myChar);

          }
      }
    //31 input of string example: ("-,.")
    public void addChars(String s){

          int numChars = s.length();
        for (int i = 0;i < numChars;++i){
                  char k = s.charAt(i);
                  Character myChar = new Character (k);
                  addChar(myChar);
        }

    }

    public void addChar(char c) {
        validCharacterSet.add(new Character(c));
    }
    public void addChar(Character validChar) {
        validCharacterSet.add(validChar);
    }

    public void removeChar(char invalidChar) {
        validCharacterSet.remove(new Character(invalidChar));
    }

    public void removeChar(Character invalidChar) {
        validCharacterSet.remove(invalidChar);
    }

    public char[] getValidCharacters() {
        int numChars = validCharacterSet.size();
        char[] validCharArray = new char[numChars];

        Iterator iter = validCharacterSet.iterator();
        int i = 0;
        while(iter.hasNext()) {
            Character c = (Character) iter.next();
            validCharArray[i] = c.charValue();
            ++i;
        } // while
        return validCharArray;
    } // getChars

      //overloaded method
      public boolean isValid(String s) {
            int slen = s.length();
            for (int i =0;i < slen;i++){
                  char k = s.charAt(i);
                  boolean bull = this.isValid(k);
                  if (false == bull) {
                     return false;
               }
            }
            return true;
      }

      //overloaded method
      public boolean isValid(char c) {
        Character d = new Character(c);
            if (validCharacterSet.contains (d))
             return true;
          else
             return false;
      }


      public Set getValidCharacterSet() {
        return Collections.unmodifiableSet(validCharacterSet);
    } // getChars
}
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK - so what's the question - how come the previous ones have not helped?

Author

Commented:
I'm not sure how to test for each method.  Could you please tell me the method and how to test for it?  Maybe only a couple of them and i'll try to do the rest.
Thank you for your time and patience.  For me, i'm sure you need it.
coolgem
CERTIFIED EXPERT
Top Expert 2016

Commented:
Something like:


public class ValidCharactersCheckerTest {
      
      public static void main(String[] args) {
            ValidCharactersChecker vcc = new ValidCharactersChecker();
            // Print the range of valid characters
            System.out.println("Valid characters follow...");
            System.out.println(new String(vcc.getValidCharacters()));
            // See if punctuation allowed:
            char c = '!';
            System.out.print("Is exclamation mark valid? - ");
            System.out.println(vcc.isValidChar(c));
      }
      
}

Author

Commented:
CEHJ
Thank you again..
coolgem
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Author

Commented:
C:\wojo\TestValidCharactersChecker.java:12: cannot resolve symbol
symbol  : method isValidChar  (char)
location: class ValidCharactersChecker
          System.out.println(vcc.isValidChar(c));

public static void main(String[] args) {
          ValidCharactersChecker vcc = new ValidCharactersChecker();
          // Print the range of valid characters
          System.out.println("Valid characters follow...");
          System.out.println(new String(vcc.getValidCharacters()));
          // See if punctuation allowed:
          char c = '!';
          System.out.print("Is exclamation mark valid? - ");
          System.out.println(vcc.isValidChar(c));
     }
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>System.out.println(vcc.isValidChar(c));

should have been

System.out.println(vcc.isValid(c));

sorry

Author

Commented:
CEHJ,
Thank you for helping me.
coolgem  

Author

Commented:
CEHJ,
Thank you for helping me.
coolgem  

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.