Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 05-08-2007, 07:38 PM   #1
Fully Optimized
 
NeXuS's Avatar
 
Join Date: Dec 2005
Posts: 2,960
Default Java Unit testing help!

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.
__________________

__________________
//Antec 902 V3 Case, Intel i7-2600K (@4.5GHz 1.280V watercooled w/ Antec H2O Kuhler 920), 8GB DDR3 Corsair Vengeance 1866MHz, ASUS P8P67 PRO B3, MSI 3GB N580 GTX Lightning Xtreme (@850MHz/2100MHz), 1TB Samsung Spinpoint F3, Corsair HX850W Pro Series//
//XPS 410 C2D E6400 @ 2.13GHz, 2GB DDR2 667MHz, 250GB HD, eVGA 320mb 8800GTS(605/1900MHz)//
//AMD Athlon XP +2000, Socket A MSI-6390, 768 mb of PC 2100 RAM, AGP ATI RADEON 9600SE//

I <3 TrancE
NeXuS is offline   Reply With Quote
Old 05-08-2007, 08:03 PM   #2
Fully Optimized
 
UK31337's Avatar
 
Join Date: Feb 2005
Posts: 2,776
Default Re: Java Unit testing help!

Integer.parseInt(String x) turns a given String into a primitive int type.

Try that.
__________________

__________________
Master of common sense. If you don't like it, stop reading.
UK31337 is offline   Reply With Quote
Old 05-09-2007, 12:07 AM   #3
Fully Optimized
 
NeXuS's Avatar
 
Join Date: Dec 2005
Posts: 2,960
Default Re: Java Unit testing help!

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.
__________________
//Antec 902 V3 Case, Intel i7-2600K (@4.5GHz 1.280V watercooled w/ Antec H2O Kuhler 920), 8GB DDR3 Corsair Vengeance 1866MHz, ASUS P8P67 PRO B3, MSI 3GB N580 GTX Lightning Xtreme (@850MHz/2100MHz), 1TB Samsung Spinpoint F3, Corsair HX850W Pro Series//
//XPS 410 C2D E6400 @ 2.13GHz, 2GB DDR2 667MHz, 250GB HD, eVGA 320mb 8800GTS(605/1900MHz)//
//AMD Athlon XP +2000, Socket A MSI-6390, 768 mb of PC 2100 RAM, AGP ATI RADEON 9600SE//

I <3 TrancE
NeXuS is offline   Reply With Quote
Old 05-09-2007, 09:34 PM   #4
Fully Optimized
 
NeXuS's Avatar
 
Join Date: Dec 2005
Posts: 2,960
Default Re: Java Unit testing help!

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[i] = (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[i] = nums[i] * multiplier;
multiplier++;
}
for(int i = 0; i < 9; i++)//sum each of the ISBN num
{
total = total + nums[i];
}
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");
}
}
}
__________________
//Antec 902 V3 Case, Intel i7-2600K (@4.5GHz 1.280V watercooled w/ Antec H2O Kuhler 920), 8GB DDR3 Corsair Vengeance 1866MHz, ASUS P8P67 PRO B3, MSI 3GB N580 GTX Lightning Xtreme (@850MHz/2100MHz), 1TB Samsung Spinpoint F3, Corsair HX850W Pro Series//
//XPS 410 C2D E6400 @ 2.13GHz, 2GB DDR2 667MHz, 250GB HD, eVGA 320mb 8800GTS(605/1900MHz)//
//AMD Athlon XP +2000, Socket A MSI-6390, 768 mb of PC 2100 RAM, AGP ATI RADEON 9600SE//

I <3 TrancE
NeXuS is offline   Reply With Quote
Old 05-10-2007, 09:23 AM   #5
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: Java Unit testing help!

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/tut...ing/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.
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:34 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0