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(vali dChar);
}
public void removeChar(char invalidChar) {
validCharacterSet.remove(n ew Character(invalidChar));
}
public void removeChar(Character invalidChar) {
validCharacterSet.remove(i nvalidChar );
}
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.contain s (d))
return true;
else
return false;
}
public Set getValidCharacterSet() {
return Collections.unmodifiableSe t(validCha racterSet) ;
} // getChars
}
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','
'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(vali
}
public void removeChar(char invalidChar) {
validCharacterSet.remove(n
}
public void removeChar(Character invalidChar) {
validCharacterSet.remove(i
}
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.contain
return true;
else
return false;
}
public Set getValidCharacterSet() {
return Collections.unmodifiableSe
} // getChars
}
I'm getting a feeling of deja vu - what's going on ;-) ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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(vali dChar);
}
public void removeChar(char invalidChar) {
validCharacterSet.remove(n ew Character(invalidChar));
}
public void removeChar(Character invalidChar) {
validCharacterSet.remove(i nvalidChar );
}
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.contain s (d))
return true;
else
return false;
}
public Set getValidCharacterSet() {
return Collections.unmodifiableSe t(validCha racterSet) ;
} // getChars
}
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','
'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(vali
}
public void removeChar(char invalidChar) {
validCharacterSet.remove(n
}
public void removeChar(Character invalidChar) {
validCharacterSet.remove(i
}
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.contain
return true;
else
return false;
}
public Set getValidCharacterSet() {
return Collections.unmodifiableSe
} // getChars
}
OK - so what's the question - how come the previous ones have not helped?
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
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.getValidCharact ers()));
// See if punctuation allowed:
char c = '!';
System.out.print("Is exclamation mark valid? - ");
System.out.println(vcc.isV alidChar(c ));
}
}
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.getValidCharact
// See if punctuation allowed:
char c = '!';
System.out.print("Is exclamation mark valid? - ");
System.out.println(vcc.isV
}
}
ASKER
CEHJ
Thank you again..
coolgem
Thank you again..
coolgem
:-)
ASKER
C:\wojo\TestValidCharacter sChecker.j ava:12: cannot resolve symbol
symbol : method isValidChar (char)
location: class ValidCharactersChecker
System.out.println(vcc.isV alidChar(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.getValidCharact ers()));
// See if punctuation allowed:
char c = '!';
System.out.print("Is exclamation mark valid? - ");
System.out.println(vcc.isV alidChar(c ));
}
symbol : method isValidChar (char)
location: class ValidCharactersChecker
System.out.println(vcc.isV
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.getValidCharact
// See if punctuation allowed:
char c = '!';
System.out.print("Is exclamation mark valid? - ");
System.out.println(vcc.isV
}
>>System.out.println(vcc.i sValidChar (c));
should have been
System.out.println(vcc.isV alid(c));
sorry
should have been
System.out.println(vcc.isV
sorry
ASKER
CEHJ,
Thank you for helping me.
coolgem
Thank you for helping me.
coolgem
ASKER
CEHJ,
Thank you for helping me.
coolgem
Thank you for helping me.
coolgem