1 Replies - 328 Views - Last Post: 04 October 2012 - 11:44 PM Rate Topic: -----

#1 ICSFailure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-September 12

Generics and compareTo

Posted 04 October 2012 - 11:18 PM

I have been tasked to take String input from a user and based on the first input they give me I will then only accept Strings that are less then the first string (based on their characters) and place them into into a stack. If this is confusing please say so. Anyways I have been given a Node.java class and a Stack.java class. The new class I am making is calling PyramidStack. I am to override the push method in order to abide by the above guidelines by using comparable. I'm pretty much stuck at the compare part. I wrote a compareTo method but it is not working. I no longer think it is the way to do it but I don't know any other way. Looking for advice.

public class Node<E> {

  private E data;
  private Node<E> next;

  public Node(E data, Node<E> next) {
    this.data = data;
    this.next = next;
  }


  public Node(E data) {
    this(data, null);
  }

  public E getData() {
    return data;
  }


  public void setData(E data) {
    this.data = data;
  }


  public Node<E> getNext() {
    return next;
  }

  public void setNext(Node<E> next) {
    this.next = next;
  }
}



public class Stack<E> {

  protected Node<E> top;
  protected int size;


  public Stack() {
    this.top = null;
    this.size = 0;
  }


  public void push(E item) {
    this.top = new Node<E>(item, top);
    this.size++;
  }  


  public E peek() {
    if (this.top == null) {
      throw new IllegalStateException("Can't peek() into empty stack.");
    }
    return this.top.getData();
  }
  

  public E pop() {
    if (this.top == null) {
      throw new IllegalStateException("Can't pop() from empty stack.");
    }
    E data = this.top.getData();
    this.top = this.top.getNext();
    this.size--;
    return data;
  }  


  public int size() {
    return this.size;
  }  
}




public class PyramidStack<E extends Comparable<E>> extends Stack<E> {



	@Override
	public void push(E item) throws IllegalArgumentException {
		
		if (top == null)
			super.push(item);
			
		if (top.getData().compareTo(item) > 0);
			super.push(item);
		
	
	
	}

	@Override
	public String toString() {
		return toString();
	
	}
	
	public int countLessThan(E item) {
	
		return 1;
	
	}
	public int compareTo(E item) {
		if (((String)item).charAt(0) < ((String)top.getData()).charAt(0)) {
			System.out.println(((String)item).charAt(0));
			System.out.println(((String)top.getData()).charAt(0));
			return 1;
		}
		else if (((String)item).charAt(0) == ((String)top.getData()).charAt(0)) {
			System.out.println(((String)item).charAt(0));
			System.out.println(((String)top.getData()).charAt(0));
			return 1;
		}
		else
			return -1;
	}
}


import java.util.Scanner;

public class Main {
	public static void main (String [] args) {
	
		String word;
		System.out.println("This program will save some of the strings you enter.");
		Stack<String> stack = new PyramidStack<String>();
	
		do {
			Scanner scan = new Scanner(System.in);
			System.out.print("Enter a string: ");
			word = scan.nextLine();
			stack.push(word);
			System.out.println(stack.peek());
			
		} while (word.length() != 0);
		
	
	
	
	
	
	
	}
}


Sorry a lot of code I know.

Is This A Good Question/Topic? 0
  • +

Replies To: Generics and compareTo

#2 ICSFailure  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-September 12

Re: Generics and compareTo

Posted 04 October 2012 - 11:43 PM

Bah nevermind. I figured it out... Sorry guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1