cecsFTL 0 Newbie Poster

Hello,

I recently turned in this assignment and I didn't received full credit. He wrote "It could be better." I'm not sure what to change.

Here's the assignment:
Write your own driver for a stack class. Instantiate one stack container object. Your driver generate random strings (uppercase letters) to push onto the stack. The length of each string is 100. Prompt the user to enter the number of strings for the stack, and generate and add them to the stack.

Ask the user to enter the number of strings again, then produce and push these strings on top of those generated previously. (Do the last 2 steps separately.)

Here are my codes:

import java.util.Random;

public class LinkedStack<T> {
	private Node top;
	
	//Instantiate random container object for getString method
	Random random = new Random();

	private class Node {
		private Node prev;
		private T data;

		public Node (T elem, Node ref) {
			data = elem;
			prev = ref;
		}

		public T getData ( ) {
			return data;
		}

		public void setData (T elem) {
			data = elem;
		}

		public void setPrev (Node ref) {
			this.prev = ref;
		}

		public Node getPrev ( ) {
			return prev;
		}
	}
	
	public LinkedStack ( ) {
		top = null;
	}

	public LinkedStack (T elem) {
		top = new Node (elem,null);
	}
	
	public void clear ( ) {
		top = null;
	}
	
	public T pop ( ) {
		T retval = peek ( );
		if (!isEmpty( )) {
			top = top.getPrev ( );
		}
		return retval;
	}

	public void push (T elem) {
		top = new Node (elem,top);
	}

	public boolean isEmpty ( ) {
		return (top == null);
	}
	
	//If stack is empty, return data at top of stack
	public T peek ( ) {
		T retval = null;
		if (!isEmpty( )) 
			retval = top.getData ( );
		return retval;
	}

	//getString method converts random integer to random char 
	//and creates random string of 100 letters
	public String getString(int amount)
	{
		int randNum = 0;//random integer initialized
		char randChar = 'a';//random Character initialized
		String randString = "";//random string is initialized with null

		//for loop to create first 100 letter Random String
		for (int j = 0; j < 100; j++)
		{
			//random integer between min 65 and max 90 (Ascii A-Z)
			//Convert random integer into char and sum to get 100 letter String
			randNum = random.nextInt(90-64) + 65;
			randChar = (char) randNum;
			randString = randString + randChar;
		}

		//return random string of uppercase letters A-Z
		return randString;
	}
}

Tester:

import java.util.*;

public class stackDriver {
	public static void main (String [ ] a) {
		LinkedStack stack = new LinkedStack ();
		Scanner keyboard = new Scanner(System.in);

		//Clear stack
		stack.clear ( );
		
		//Declare and initialize random string as null
		String randString = null;
		
		//Declare counter used in for loop
		int counter;

		//Ask user how many times to push random string onto stack
		System.out.println("How many times would you like to add a random string to stack: ");
		int amount = keyboard.nextInt();
		
		//Continuously push random string onto stack until amount user entered
		for (counter = 0; counter < amount; counter++)
		{
			//Call method to generate random strings to push onto stack
			randString = stack.getString(amount);

			//Push random String onto stack
			stack.push (new String (randString));
		}

		//After push of first random string, display current top
		System.out.println("Current top of stack: " + stack.peek());

		//Do next two steps separately
		//Ask user if they want to add anymore random strings
		System.out.println("\nNow, how many random strings would you like to add to the stack again?: ");
		int amount2 = keyboard.nextInt();

		//Continuously push random string onto stack until amount user entered
		for (counter = 0; counter < amount; counter++)
		{
			//Call method to generate random string to push onto stack
			randString = stack.getString(amount);

			//Push random String onto stack
			stack.push (new String (randString));
		}
		
		//After new random strings are added to stack, display new top
		System.out.println("New top of stack: " + stack.peek());
	}
}

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.