Link to home
Start Free TrialLog in
Avatar of coolgem
coolgem

asked on

Testing individual methods from a class

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
}  
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I'm getting a feeling of deja vu - what's going on ;-) ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coolgem
coolgem

ASKER

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
>>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
Avatar of coolgem

ASKER

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
}
OK - so what's the question - how come the previous ones have not helped?
Avatar of coolgem

ASKER

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
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));
      }
      
}
Avatar of coolgem

ASKER

CEHJ
Thank you again..
coolgem
:-)
Avatar of coolgem

ASKER

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));
     }
>>System.out.println(vcc.isValidChar(c));

should have been

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

sorry
Avatar of coolgem

ASKER

CEHJ,
Thank you for helping me.
coolgem  
Avatar of coolgem

ASKER

CEHJ,
Thank you for helping me.
coolgem