-->
当前位置:首页 > 题库

PROGRAMMING:Jmu-java-05 set (generic) - 10 generalstack

Luz5年前 (2021-05-10)题库438
The previously defined integerstack interface can only be used to store data of type integer. However, for stack, no matter what type of data is stored inside, the basic operation has nothing to do with the specific type of element.
###1. Write a general generalstack interface. The operations in the interface are applicable to any data of reference type.
Once defined, only one type of data can be stored, such as string or integer` The interface method of generalstack is as follows:
```java
push(item); // If the item is null, null will be returned without stack.
pop(); // If the stack is empty, null is returned.
peek(); // Get the top of stack element, if it is empty, return null
public boolean empty();// If it is empty, return true
public int size(); // Returns the number of elements in the stack
```
###2. Define the implementation class arraylistgeneralstack of generalstack
The ArrayList object is used internally for storage, and the attribute name is' list '.
**Methods:**
`Public string tostring() '/ / the code of this method is' return list. Tostring()`
**Tips:**
1. Do not use the top pointer.
2. Directly reuse the existing methods in ArrayList.
3. The corresponding elements should be removed from ArrayList when pop.
4. Code * * do not appear * * type unsafe * * cast * *.
###3. Define the car object
Properties:
```
private int id;
private String name;
```
Methods: Eclipse automatically generates setter / getter, toString methods.
###4. Main method description
1. Input options, including ` quit, integer, double and car '. If you enter quit, you exit directly. Otherwise, enter the integers m and n. M is the number of stacks, n is the number of stacks. Then declare the stack variable 'stack'.
2. Enter 'Integer' and print 'integer test'. Create an 'arraylistgeneralstack' that can store integer types. Enter the stack m times and exit the stack n times. Print the toString method of the stack. Finally, the remaining elements in the stack are output.
3. Enter 'double' and print 'double test'. The rest is the same as entering integer.
4. Enter 'car' and print 'car test'. Other operations are basically the same as integer and double. It's just that the elements in the stack are finally put out of the stack and their names are output in turn.
2. Use the code 'system. Out. Println (stack. Getclass(). Getinterfaces() [0]) after 3 and 4 steps` Print identification information
**Special note: if the stack continues to exit when it is empty, null will be returned
###Input sample
```in
Integer
five
two
1 2 3 4 5
Double
five
three
1.1 2.0 4.9 5.7 7.2
Car
three
two
1 Ford
2 Cherry
3 BYD
quit
```
###Output sample
```out
Integer Test
push:1
push:2
push:3
push:4
push:5
pop:5
pop:4
[1, 2, 3]
sum=6
interface GeneralStack
Double Test
push:1.1
push:2.0
push:4.9
push:5.7
push:7.2
pop:7.2
pop:5.7
pop:4.9
[1.1, 2.0]
sum=3.1
interface GeneralStack
Car Test
push:Car [id=1, name=Ford]
push:Car [id=2, name=Cherry]
push:Car [id=3, name=BYD]
pop:Car [id=3, name=BYD]
pop:Car [id=2, name=Cherry]
[Car [id=1, name=Ford]]
Ford
interface GeneralStack
```






answer:If there is no answer, please comment