Cheking/Printing a List

i created a deck of cards and then made a method which creates a CardList(a list) out of a couple of those cards. I wanna check if thisa works. i try to print it out in my driver(ill post it) and im not sure if im printing it incorrectly or if my list has a problem. can anyone help?

Deck Class

publicclass Deck{

//variables

private Card deck[];

privateint deckMarker = 0;

private CardList someCards;

//accesors

public Deck(){

makeDeck();

}

//methods

public Card[] makeDeck(){

int count = 0;

deck =new Card[40];

for(int suit = 1; suit <= 4; suit++){

for(int value = 1; value <= 10; value++){

deck[count] =new Card(value, suit);

count++;

}

}

return deck;

}

publicvoid shuffle(){

for (int i = 39; i > 0; i-- ){

int rand = (int)(Math.random()*(i+1));

Card temp = deck[i];

deck[i] = deck[rand];

deck[rand] = temp;

}

}

public CardList draw (int n){

deckMarker += n;

System.out.println(deckMarker);

CardList someCards = CardList.NIL;

for(int i=0;i < deckMarker;i++){

System.out.println(i);

someCards = someCards.push(deck[i]);

}

return someCards;

}

public String toString(){

String s ="Deck ";

for (int i = 40 - 1; i >= 0; i--)

s += deck[i] +" ";

return s;

}

}

heres where i try to print it

publicclass CardTest{

publicstaticvoid main(String ars[]){

Deck aDeck =new Deck();

//aDeck.shuffle();

System.out.println(aDeck);

System.out.println(aDeck.draw(3));

}

}

i getthis error

Exception in thread"main" java.lang.NullPointerException

at List.toString(List.java:57)

at List.toString(List.java:57)

at List.toString(List.java:57)

at List.toString(List.java:57)

at java.lang.String.valueOf(String.java:2827)

at java.io.PrintStream.println(PrintStream.java:771)

at CardTest.main(CardTest.java:11)

[b]heres line 57[/b]

[code]public String toString (){

return this.isEmpty() ?"()" :"(" + this.head().toString() +

(this.tail().isEmpty() ?"" :" ") + this.tail().toString().substring(1);

}

thanks guys

[4681 byte] By [comedyboy19a] at [2007-11-26 22:43:40]
# 1
If your List class is not too big, could you post it as well?
prometheuzza at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 2

here is the List class(basically CardList just fOr all objects), btw CardList implements List, so its basicaly just same methods just for Cards:

public class List implements Comparable {

protected Object data;

protected List next;

public static final List NIL = new List();

public List (Object d, List n) { data = d; next = n; }

public List (Object d) { this(d, NIL); }

protected List () { }

public List nil () {

return NIL;

}

public List push (Object d) {

return new List(d, this);

}

public Object head () { return data; }

public List tail () { return next; }

public boolean isEmpty () {

return this == nil();

}

public int length () {

return this.isEmpty() ? 0 : 1 + this.tail().length();

}

public List find (Object d) {

return this.isEmpty() || d.equals(this.head()) ? this : this.tail().find(d);

}

public List append (List a) {

return this.isEmpty() ? a : this.tail().append(a).push(this.head());

}

public List reverse () {

return this.isEmpty() ? this : this.tail().reverse().append(nil().push(this.head()));

}

public Object nth (int n) {

if (this.isEmpty() || n < 0) return null;

return n == 0 ? this.head() : this.tail().nth(n - 1);

}

public List delete (Object d) {

if (this.isEmpty()) return this;

if (d.equals(this.head())) return this.tail().delete(d);

return this.tail().delete(d).push(this.head());

}

public List purge () {

return this.isEmpty() ? this : this.tail().delete(this.head()).purge().push(this.head());

}

public String toString () {

return this.isEmpty() ? "()" : "(" + this.head().toString() +

(this.tail().isEmpty() ? "" : " ") + this.tail().toString().substring(1);

}

public static List parseIntList (String s) {

IllegalArgumentException badArg = new IllegalArgumentException(s);

int openBracket = s.indexOf('(');

int closeBracket = s.indexOf(')');

if (openBracket != 0 || closeBracket != s.length() - 1) throw badArg;

String[] intStrings = s.substring(openBracket + 1, closeBracket).split(" ");

List result = NIL;

for (int i = intStrings.length - 1; i >= 0; i--)

result = result.push(Integer.parseInt(intStrings[i]));

return result;

}

private List firstN (int n) {

return n == 0 ? nil() : this.tail().firstN(n - 1).push(this.head());

}

public List firstHalf () {

return firstN(this.length() / 2);

}

private List remFirstN (int n) {

return n == 0 ? this : this.tail().remFirstN(n - 1);

}

public List lastHalf () {

return remFirstN(this.length() / 2);

}

public List merge (List m) {

if (this.isEmpty()) return m;

if (m.isEmpty()) return this;

if (((Comparable) this.head()).compareTo(m.head()) < 0)

return this.tail().merge(m).push(this.head());

return this.merge(m.tail()).push(m.head());

}

public List sort () {

if (this.isEmpty() || this.tail().isEmpty()) return this;

return (this.firstHalf().sort()).merge(this.lastHalf().sort());

}

public List insert (Comparable d) {

return this.isEmpty() || d.compareTo(this.head()) < 0 ?

this.push(d) :

this.tail().insert(d).push(this.head());

}

public int compareTo (Object o) {

if (!this.getClass().equals(o.getClass())) throw new ClassCastException("List compared to non-List");

List ol = (List) o;

if (this.isEmpty() && ol.isEmpty()) return 0;

if (this.isEmpty()) return -1;

if (ol.isEmpty()) return 1;

switch (((Comparable) this.head()).compareTo(ol.head())) {

case -1: return -1;

case 1: return 1;

default: return this.tail().compareTo(ol.tail());

}

}

public List powerSet () { return powerSet(nil()); }

public List powerSet (List selected) {

if (this.isEmpty()) return NIL.push(selected); // notice use of NIL as opposed to nil()

// this is because a powerSet is always a List, not

// the potential subclass

return this.tail().powerSet(selected).append(this.tail().powerSet(selected.push(this.head())));

}

}

comedyboy19a at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 3

> here is the List class(basically CardList just fOr

> all objects), btw CardList implements List, so its

> basicaly just same methods just for Cards:

>

You probably mean extends List, that monster can't be implemented since it's no interface.

My advice is to use some sort of collection from the java.util package: this thing is horrible! It seems to be some sort of likned list, but I don't see a Node class.

If you can't use a java.util.List implementation (if it's homework), then I suggest throwing this thing away and implement a proper (linked) list.

Sorry I couldn't be of more help. Good luck though.

prometheuzza at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 4
your right it does extend it not implement it, sorry for that.Bt what do you mean by node class. do you mean my Card class? becuase i didntpost all the classes to this program
comedyboy19a at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 5

> your right it does extend it not implement it, sorry

> for that.Bt what do you mean by node class. do you

> mean my Card class?

> ...

No, I mean your List class seems to be some sort of a linked list. Linked list normally consist of Node objects (head and tail and everything in between). In your case a List has some sort of head and tail made out of type List: that's not the way to do it.

Here's a small demo of how you'd implement a linked list:class TestMyLinkedList {

public static void main(String[] args) {

MyLinkedList myList = new MyLinkedList();

myList.add("A");

myList.add("B");

myList.add("C");

System.out.println(myList);

}

}

class MyLinkedList {

private Node head, tail;

private int size;

public MyLinkedList() {

head = tail = null;

size = 0;

}

public void add(Object o) {

if(head == null) {

head = tail = new Node(o);

} else {

tail.next = new Node(o);

tail = tail.next;

}

size++;

}

// the rest of your methods like 'get(int index)', 'size()', etc.

public String toString() {

StringBuffer strb = new StringBuffer();

Node pointer = head;

while(pointer != null) {

strb.append(pointer);

strb.append(" --> ");

pointer = pointer.next;

}

strb.append("null");

return strb.toString();

}

}

class Node {

protected Object data;

protected Node next;

protected Node(Object d){ this.data = d; }

public boolean equals(Object o) { return data.equals(o); }

public int hashCode(){ return data.hashCode(); }

public String toString(){ return "["+data+"]";}

}

prometheuzza at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 6

Everything in thisprogram now works, and according to the assignment it need to use that List class so as to the way you showed me how to make a linked list i dont think i can use that. the only prob i see now is my toString method i think. i run the CardTest class and i get this error:

Exception in thread "main" java.lang.NullPointerException

at List.toString(List.java:57)

at List.toString(List.java:57)

at List.toString(List.java:57)

at List.toString(List.java:57)

at java.lang.String.valueOf(String.java:2827)

at java.io.PrintStream.println(PrintStream.java:771)

at Deck.draw(Deck.java:43)

at CardTest.main(CardTest.java:11)

i showed my TA and he couldnt figfure it out he thnks it has to do with my toString method but i just dont know. anyone?

comedyboy19a at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 7
Could you please post the toString() method of your List class?
quittea at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 8
sorry, its in one of theprevious posts in he List class
comedyboy19a at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...
# 9

> Everything in thisprogram now works, and according to

> the assignment it need to use that List class so as

> to the way you showed me how to make a linked list i

> dont think i can use that.

You don't need to use it: you need to see how a linked list works! Your own implementation of your List class has an attribute 'next' which is of type List, which is plain wrong!

public class List implements Comparable {

protected Object data;

protected List next; // <- !!!!!

// ...

}

Best of luck with your assignment.

prometheuzza at 2007-7-10 12:00:05 > top of Java-index,Java Essentials,Java Programming...