another question

can someone help me with what's wrong with my LottoTicket?

i tried to run it against the test, but got an error. i dont think it can find LottoTicket.

also, i didnt write the test :\

import java.util.*;

publicclass LottoTicket

{

private String let1;

private String let2;

private String let3;

private String let4;

private String let5;

private String thisticket;

privatestatic String lastticket;

publicvoid lastTicket(String s){

String thisticket = s;

String lastticket ="ZZZZZ";

}

publicvoid thisTicket(){

Random r =new Random();

int x1 = r.nextInt(27);

int x2 = r.nextInt(27);

int x3 = r.nextInt(27);

int x4 = r.nextInt(27);

int x5 = r.nextInt(27);

let1 = Integer.toString(x1);

let2 = Integer.toString(x2);

let3 = Integer.toString(x3);

let4 = Integer.toString(x4);

let5 = Integer.toString(x5);

lastticket ="ZZZZZ";

String ticket = let1+let2+let3+let4+let5;

thisticket = ticket.toUpperCase();

}

publicvoid generateNext(){

Random r =new Random();

int x1 = r.nextInt(27);

int x2 = r.nextInt(27);

int x3 = r.nextInt(27);

int x4 = r.nextInt(27);

int x5 = r.nextInt(27);

let1 = Integer.toString(x1);

let2 = Integer.toString(x2);

let3 = Integer.toString(x3);

let4 = Integer.toString(x4);

let5 = Integer.toString(x5);

lastticket = let1+let2+let3+let4+let5;

}

public String getThisTicket(){

return thisticket;

}

public String getLastTicket(){

return lastticket;

}

}

publicclass LottoTestextends junit.framework.TestCase

{

private LottoTicket lt;

publicvoid TestConstructor1(){

lt =new LottoTicket();

assertEquals("ZZZZZ", lt.getLastTicket());

assertFalse("ZZZZZ".equals(lt.getThisTicket()));

}

publicvoid testConstructor2(){

lt =new LottoTicket("AAAAA");

assertEquals("ZZZZZ", lt.getLastTicket());

assertEquals("AAAAA", lt.getThisTicket());

}

publicvoid testGenerateNext(){

lt =new LottoTicket();

lt.generateNext();

assertFalse("ZZZZZ".equals(lt.getLastTicket()));

}

publicvoid testStatic(){

lt =new LottoTicket();

LottoTicket lt2 =new LottoTicket();

lt.generateNext();

assertEquals(lt.getLastTicket(), lt2.getLastTicket());

}

}

[4945 byte] By [obeserabbita] at [2007-11-26 20:02:10]
# 1

The first errors I receive are that the LottoTest class is looking for 2 different assert methods.

Both of which have been undefined at all. I'm assuming you were trying to use the static methods of the Assert class.

Now that I've seen the TestCase package, and the methods you were using, I believe you need to import it, rather than extend a class of TestCase

import junit.framework.TestCase;

http://junit.sourceforge.net/javadoc/junit/framework/Assert.html

You are trying to call a constructor of the LottoTicket class which has been undefined.

You are also trying to call a constructor of LottoTicket that takes 1 String parameter. You don't have this defined, so the compiler can't find it.

Message was edited by:

lethalwire

Message was edited by:

lethalwire

lethalwirea at 2007-7-9 23:01:09 > top of Java-index,Java Essentials,New To Java...
# 2
thanks, could you clarify on the part about static methods? i'm not really clear on the part about adding a class name in front of the method
obeserabbita at 2007-7-9 23:01:09 > top of Java-index,Java Essentials,New To Java...
# 3

You maybe know that some methods are class methods (Static)

The word static basically means "belong to the class" and not an object.

So of course you can have your regular methods you can use if you create an object of a class.

BUT some methods can be static.

Example

public class MyClass {

//Static method inside of MyClass

public static void sayHello() {

System.out.println("Say Hello");

}

}

In order to class this method, I would have to use:

MyClass.sayHello();

Notice I had to use the class name "MyClass" because the sayHello method is static.

Note that you can only do this for static methods and static variables etc.

lethalwirea at 2007-7-9 23:01:09 > top of Java-index,Java Essentials,New To Java...