Java Unit testing help!

NeXuS1

Fully Optimized
Messages
2,960
Here are the instructions, I have some code but having trouble converting and doing the multiplication.

Project 4: ISBN Evaluator and test harness.
An International Standard Book Number (ISBN) is a code of 10 characters separated by dashes such as
0-8065-0959-7. An ISBN consists of four parts:
• a group code,
• a publisher code,
• a code that uniquely identifies the book among those published by the particular publisher, and
• a check character
For the ISBN 0-8065-0959-7, the group code is 0, which identifies the book as one from an Englishspeaking
country. The publisher code 8065 identifies the book as one published by Citadel Press. The
code 0959 uniquely identifies the book among those published by Citadel Press (Brode: Woody Allen:
His Films and Career, in this case).
The check character is computed as follows:
First, compute the sum of the first digit plus two times the second digit plus three times the third
digit, ... , plus nine times the ninth digit. The last character is the remainder when the sum is divided by
11. If the remainder is 10, the last character is X. For example, the sum for the ISBN 0-8065-0959-7 is
0 + 2*8 + 3*0 + 4*6 + 5*5 + 6*0 + 7*9 + 8*5 + 9*9 = 249
The remainder when 249 is divided by 11 is 7, the last character in the ISBN. The check character is
used to validate an ISBN.
Write a class that has a method that accepts an array of characters, consisting of both numbers and
hyphens. The function returns true if the array represents a valid ISBN and false otherwise.
For full credit, you must also provide an adequate test harness to test your class with multiple ISBNs.
Here are a few you might consider using:
"0-8065-0959-7",
"0-534-37964-8",
"0-618-50298-X",
"0-8065-0959-8", //bad
"0-534-37964-9", //bad
"0-618-50298-5", //bad
"0-534-37964-8",
"0-618-50298-X",
"032121353X",
"0321199553",
"0201794896",
"0870495275",
"0452264464",
For additional information on ISBN's, see http://en.wikipedia.org/wiki/ISBN.


import java.util.StringTokenizer;
public class ISBN1
{
public static void main(String[] args)
{
String ISBN = "0-8065-0959-7";
boolean valid = false;
StringTokenizer strTokenizer = new StringTokenizer(ISBN, "-");
String part1 = strTokenizer.nextToken();
String part2 = strTokenizer.nextToken();
String part3 = strTokenizer.nextToken();
String part4 = strTokenizer.nextToken();


}
}

I got them separated but I am having trouble allocating them then converting then into integers.
 
Integer.parseInt(String x) turns a given String into a primitive int type.

Try that.
 
Yeah but then it would turn it into a number like 8065, I want to separate them so I can work with each like an array but I am confused.
 
Ok I need to do this anyone know how to use unit testing??
Write a class that has a method that accepts an array of characters, consisting of both numbers and
hyphens. The function returns true if the array represents a valid ISBN and false otherwise.
For full credit, you must also provide an adequate test harness to test your class with multiple ISBNs.
Here are a few you might consider using:
"0-8065-0959-7",
"0-534-37964-8",
"0-618-50298-X",
"0-8065-0959-8", //bad
"0-534-37964-9", //bad
"0-618-50298-5", //bad
"0-534-37964-8",
"0-618-50298-X",
"032121353X",
"0321199553",
"0201794896",
"0870495275",
"0452264464",
For additional information on ISBN's, see http://en.wikipedia.org/wiki/ISBN.

Here is the ISBN checker finished.

import java.util.StringTokenizer;
public class ISBN1
{
public static void main(String[] args)
{
String ISBN = "0 - 8 0 6 5 - 0 9 5 9 - 7";
boolean valid = false;
int multiplier = 1;
int total = 0;
StringTokenizer strTokenizer = new StringTokenizer(ISBN, "- ");//Separates the string into single numbers ignoring "-"
int[] nums = new int[10];//Array of Int that takes each number of the ISBN
for(int i = 0; i < 10; i++)//This for is used for assigning each number into the array and converting the String into an integer
{
nums = (Integer.parseInt(strTokenizer.nextToken()));//Assingns while converting into an integer
}
for(int i = 0; i < 9; i++)//multiply each of the ISBN num (see the instructions)
{
nums = nums * multiplier;
multiplier++;
}
for(int i = 0; i < 9; i++)//sum each of the ISBN num
{
total = total + nums;
}
if(total % 11 == nums[9])//Gets the remainder of the division and checks if ISBN is valid acording to the algorithm of how to checkl for a valid ISBN
{
valid = true;
}
else
{
valid = false;
}
if(valid == true)
{
System.out.println("Valid ISBN");
}
else
{
System.out.println("Invalid ISBN");
}
}
}
 
I'm not sure about unit testing but a method I always use when I need to split a string is using a pattern and splitting the string based on the pattern, and then placing them in an array
Code:
import java.util.regex.*;
 
//later in code
public void splitString(String myString) {
Pattern p = new Pattern();
p.compile("[-]+"); // split the string based on hyphens.
String brokenNumberString[] = new String[1000];
brokenNumberString = p.split(myString);
int myIntegerArray[] = new int[brokenNumberString.length];
for (int i = 0; i<myIntegerArray.length; i++) {
 myIntegerArray[i] = Integer.parseInt(brokenNumberString[q].charAt(i));
}
// note that this is not compiled code, and doesn't work as I didn't complete it.
}
thats not complete code, but after looking at your code, I think i'm going to abandon my old method, because yours looks a lot less brute-forcy, and simpler.
I did a little bit of reading on the subject, and it seems like unit testing is a fancy way to simply write another program to test your program by giving it values, and if you get the error message, you have a bug to fix. I found this article:
http://javaboutique.internet.com/tutorials/UnitTesting/index.html
If you aren't using an ide, and just using textpad(or some equivalent), you might want to try NetBeans, as it offers code examples and snippets on JUnit type applications.
http://www.netbeans.org
hope this helps, sorry I don't know more.
 
Back
Top Bottom