• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test class

 
Jim gross
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How do I write a Test class for class LinkedList?

[This message has been edited by Cindy Glass (edited September 24, 2001).]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim -
There's a couple things missing from the code; in the readIntValues() method, there's a reference to io that isn't supported anywhere else in the class. One can assume it's an InputStream or Reader, but since it isn't declared or instantiated anywhere, I can only assume "io" is supposed to be a stand-in for System.in.
On the other hand, I don't know which class (if any) allows you to prompt the user as a argument to some input type, and expect an integer in return. It's been a while since I've done any client-side java and I don't have the JDK docs at hand, so I dunno know. But we need a little more to go on here.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Jim gross
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Ernest,
Thank you for answering my question.
I forgot to send the rest of the code. Here they are.
public class List
{

private int element;
private List next;
public List()
{

element = 0;
next = null;
}
public List(int elem, List nextElem)
{
element = elem;
next = nextElem;
}
public void setElem(int someElement)
{
element = someElement;
}
public void setNext(List newNext)
{
next = newNext;
}
public int getElem()
{
return element;
}
public List getNext()
{
return next;
}
}
public class io
{
public io()
{
}

static String readString()
{ int ch;
String r = "";
boolean done = false;
while (!done)
{ try
{ ch = System.in.read();
if (ch < 0 | | (char)ch == '\n')
done = true;
else if ((char)ch != '\r')
r = r + (char) ch;
}
catch(java.io.IOException e)
{ done = true;
}
}
return r;
}


static void printPrompt(String prompt)
{ System.out.print(prompt + " ");
System.out.flush();
}


static int readInt(String prompt)
{ while(true)
{ printPrompt(prompt);
try
{ return Integer.valueOf
(readString().trim()).intValue();
} catch(NumberFormatException e)
{ System.out.println
("Not an integer. Please try again!");
}
}
}

static void error(String err)
{
throw new Error(err);
}

}
 
Jamie Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered using the LinkedList class java.util.LinkedList? I would test your program against it. After all it is a concrete collection. It is easy to use.
------------------
 
Jim gross
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I still don't know how to write a test class.
How do I start?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just add a main() method to your LinkedList class and work with it.
Or you can create a separate class Test that has the main and use it to test your stuff.

[This message has been edited by Cindy Glass (edited September 25, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic