|
| Stack of integers Fra : Abdis | Vist : 587 gange 100 point Dato : 07-02-05 20:20 |
|
1)for an ADT stack of integers , you have to specify at least 4 operations (e.g)CRUD) means crate update and delete
specify pre_condition and post_condition
2) make an implementation decision : in java and array data structure with a top variable .
3) implement the stack with methods , and add inavariants in each method test it
4) put the stack in a java package: myConytainers. test it
5) make a head program and use your new Stack .test it
| |
| Kommentar Fra : Abdis |
Dato : 07-02-05 22:01 |
|
my question is making an empty stack where u can add new stack at the top and where u can delete something at top top
| |
| Kommentar Fra : Abdis |
Dato : 07-02-05 22:10 |
|
mit spørgsmål er at lave et tom stak, hvor du kan tilføjer af ny ny stak ad top af staken forstået eller ikke forstået
| |
| Kommentar Fra : Abdis |
Dato : 07-02-05 22:36 |
|
Hi can't I get someone who answers my question please
| |
|
ADT = Abstract Data Type. Means that you have to make a sort of plan for a class (e.g. an object) that contains the mensioned operations.
Operations could be: crate, pop, put, size whatever (delete and update is not typical Stack - operations....)
A Stack is different from a Queue because in a Stack its "first in - last out" in a Queue its "First in - first out"
Lets call the ADT IntStack
1: ADT:
IntStack.create()
IntStack.pop()
IntStack.put()
IntStack.size()
IntStack.toString()
2:
Make a local array of integers as a datastructure:
int[] structure = new int[30]; //Can hold 30 integers....
3:
Kode packace myConytainers; //4...
import java.util.*;
public class IntStack(){
private int[] structure;
private int count;
public IntStack() {
structure = = new int[30];
count = 0;
}
public put(int value) {
structure[count] = value;
count++;
}
public int pop() {
int returnValue = structure[count];
structure[count] = 0;
count--;
return returnValue;
}
public int count() {
return count;
}
public String toString() {
StringBuffer buf = new StringBuffer();
int i = structure.length - 1;
for (i; i >= 1 ; i--) { //It´s a Stack so the list is supposed to be reversed....
buf.append("Index " + i + " = " structure[i] + ", ")
}
buf.append("Index " + i + " = " structure[i]) // to prevent a ", " as the last print....
return buf.toString();
}
public static void main(String args[]) { // 5......
IntStack stack = new IntStack();
stack.put(2); // now stack contains 2
stack.put(4); // now stack contains 4, 2
stack.put(6); // now stack contains 6, 4, 2
stack.pop(); // now stack contains 4, 2
System.out.println("Stack contains " + stack.cont() + " numbers. They are: " + stack.toString());
}
} |
Simlified example, but You can play with it yourself
| |
|
I forgot:
1) Precondition: The IntStack is not created and is empty.
Postcondition: The IntStack may be populated, full or empty.
3) Invariants is what is expectet for each method. Should be the "JavaDoc" for the method. Make pre- and postconditions, and a simple explanation for the functionality of the method.
| |
| Kommentar Fra : Abdis |
Dato : 07-02-05 23:37 |
|
what i want is
IntStack.create()
IntStack.pop()
IntStack.put()
IntStack.size()
and no return value i want instead to throw an EXCEPTION error
| |
|
Correction:
Kode public int pop() {
int returnValue = structure[count - 1];
structure[count - 1] = 0;
count--;
return returnValue;
} |
| |
| Kommentar Fra : Abdis |
Dato : 07-02-05 23:40 |
|
Hi i understand but where is create and add and size operations that is what i want to understand and how to put them in a conatainer
| |
|
According to the Stack concept the operation IntStack.pop should return an int-value. But You can ofcourse just make it dissapear.... by not returning anything...
The the operation would be like:
Kode public pop() {
structure[count-1] = 0;
count--;
} |
| |
| Kommentar Fra : Abdis |
Dato : 07-02-05 23:44 |
|
can u please build it from the beginning to write the comentary of create pop push size and it must be an empty one and put them in a container
thnks
| |
| Accepteret svar Fra : justuniverse | Modtaget 100 point Dato : 07-02-05 23:51 |
|
The "create" operation was in the "abstract" part of the datatype. In the implementation this is the constructor: IntStack() {
.......
}
A size-operation is the "count"-operation, I made. (My fault) You can just rename the count-operation to "size()".
A Stack IS a Container.... (Just an object, that can "contain" other things, in this case integers. Could have been objects too - or even both)
The package "myConytainers" holds your own containers apart from your KanduInvironment. (Just datatypes to "contain" things of same "family")
In standard Java, several containers is made: Vector, Stack (!), Queue, BitSet, List, Set, TreeSet and so on...
The are in the package java.util (as far as I remember)
Actually the Vector also is a Stack (among other things) (its just hard to tell..)
| |
| Godkendelse af svar Fra : Abdis |
Dato : 07-02-05 23:53 |
|
Tak for svaret justuniverse.
| |
|
Du kan nå at slette dit andet spørgsmål, hvis du synes du har fået svar.... (Så sparer du 44 points)
| |
| Du har følgende muligheder | |
|
Eftersom du ikke er logget ind i systemet, kan du ikke skrive et indlæg til dette spørgsmål.
Hvis du ikke allerede er registreret, kan du gratis blive medlem, ved at trykke på "Bliv medlem" ude i menuen.
| |
|
|