StringStack

miu90

Neues Mitglied
Hi, ich bin neu hier im Forum und hab vorher eigentlich noch nie Foren benutzt. Ich muss für mein Studium eine Aufgabe bewältigen und weiß nicht richtig, wie ich beginnen muss, ich komm einfach nicht rein. Ich hab auch noch keine große Programmiererfahrung. Die einzigen Kenntnisse die ich habe sind ein wenig C-Programmierung. Jetzt fangen wir im Studium mit Java an und ich komme einfach nicht mit der Aufgabe zurecht. Ich hoff es könnt jemand mal drüberschauen und mir ein paar Tips geben. Ich weis es ist nich Sinn der Sache, aber ich poste mal die Aufgabe hier hinein. Danke schonmal im voraus!

Java:
package labor2;

/*
 * Aufgabe 2
Erstellen Sie eine Stack Klasse, die String Objekte als LIFO (Last in first out) Liste speichert.
Spezifikation
1. Erstellen Sie eine Klasse StringStack mit den folgenden Methoden (hier in javadoc)

 */

public class StringStack
{
	private final String[] stackArray;
	private int arraysize = 0;
	private final int elementnumber = 0;

	public StringStack(int size)
	{
		stackArray = new String[size];
		arraysize = size;
	}

	public StringStack()
	{
		stackArray = new String[0];
		arraysize = 0;
	}

	/**
	 * Adds a new element to the top of the stack. One element should only be
	 * added once to the stack. If an element is added twice, the element should
	 * not been added and the return value should be set to false. True should
	 * be returned otherwise.
	 * 
	 * @param newElement
	 *            the element to add
	 * @return boolean true if the element could be added, false otherwise
	 */
	boolean push(String aString);

	/**
	 * Removes and returns the top element of the stack
	 * 
	 * @return the element that was on top of the stack
	 * 
	 */
	String pop();

	/**
	 * Returns but does not remove the top element of the stack
	 * 
	 * @return the top element of the stack
	 * 
	 */
	String peek();

	/**
	 * Tests if this stack is empty
	 * 
	 * @return true if this stack contains no elements
	 */
	boolean isEmpty();

	/**
	 * Returns the number of elements in the stack. OOP I Praktikum
	 * Dipl.-Ing.(FH) Patrick Luchner, M.Sc.
	 * 
	 * @return int number of real elements, not the length of the underlying
	 *         array
	 */
	int size();

	/**
	 * Tests if the given String is already included in the stack.
	 * 
	 * @param aString
	 *            the given String to search in the stack
	 * @return true if this stack already contains the given element
	 */
	boolean contains(String aString);

	/**
	 * Inverts the stack. That means ordering of the elements will be inverted.
	 * The former bottom element will afterwards be the top element of the
	 * stack.
	 */
	void invert();
	
	/*
	 * 2. Implementieren sie die Klasse StringStack folgendermaßen:
a. Verwenden sie ein Array zum Speichern der Elemente
b. Erstellen sie einen Konstruktor, der es erlaubt, die Anfangskapazität
des Arrays zu spezifizieren. Das Array kann folgendermaßen erzeugt
werden: new String[initialCapacity];
c. Beim Aufruf von push soll für den Fall, dass kein Platz mehr im Array
ist, ein größeres Array angelegt werden. Die Einträge aus dem alten
Array können ins neue Array mittels System.arraycopy(source,
startIndexSource, destination, startIndexDestination, size) kopiert
werden. Danach kann das neue Element hinzugefügt werden.
d. Der Stack darf keine doppelten String-Objekte enthalten. Wird beim
Aufrufen der push Mehtode festgestellt, dass das übergebene String
Objekt bereits im Stack enthalten ist, soll false zurückgeliefert
werden.
3. Erstellen sie eine junit Testklasse StringStackTest, die in einer setup-Methode
(@Before) die zu testende Instanz von StringStack anlegt und jede ihrer Methoden
prüft (4 Punkte)
	 */

}
 
Zuletzt bearbeitet von einem Moderator:

Camill

Bekanntes Mitglied
Ja genau, fang einfach von oben an alle Methoden zu füllen. Kannst ja vorerst die Fehlersicherungen(zb. das bei "push" Elemente nicht doppelt vorkommen sollen) weg lassen und nur das Grundlegende schreiben.
 

Neue Themen


Oben