一、
编程(一个带main方法的类)采用冒泡法实现对数组元素由小到大的排序。数组的内容为10个0到100间的随机整数,排序后输出数组的这十个元素二、
1. 声明一个表示栈的接口Stack。假定接口内只有initStack、push、pop和StackEmpty方法。
2. 分别在顺序存储和链接存储结构下实现该接口,即声明两个类SStack和LStack实现这个接口。
编程(一个带main方法的类)采用冒泡法实现对数组元素由小到大的排序。数组的内容为10个0到100间的随机整数,排序后输出数组的这十个元素二、
1. 声明一个表示栈的接口Stack。假定接口内只有initStack、push、pop和StackEmpty方法。
2. 分别在顺序存储和链接存储结构下实现该接口,即声明两个类SStack和LStack实现这个接口。
public class Taxis {
public static void main(String[] args) {
try {
int[] a = new int[10];
for (int b = 0; b < 10; b++) {
a[b] = (int)(Math.random() * 100);
}
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
System.out.print(a[i] + " ");
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
后面的以后有时间再弄。
代码如下:
interface MyStack{
public void initStack(int i);
public void push(Object obj);
public Object pop();
public void stackEmpty();
}public class SStack implements MyStack{
int maxIndex,curIndex;
Object items[]; public void initStack(int i){
maxIndex=i;
items=new Object[maxIndex];
curIndex=-1;
} public void push(Object obj){
if(curIndex == maxIndex-1)return;
items[++curIndex]=obj;
} public Object pop(){
if(curIndex == -1)return null;
Object tmp=items[curIndex];
items[curIndex--]=null;
return tmp;
} public void stackEmpty(){
for( ; curIndex>=0 ; ){
items[curIndex--]=null;
}
} public static void main(String args[]){
MyStack stack=new SStack();
stack.initStack(5);
for(int i=0 ; i<5 ; i++){
stack.push(""+(i+1));
} for(int i=0 ; i<5 ; i++){
System.out.println(stack.pop());
} for(int i=0 ; i<5 ; i++){
stack.push(""+(i+1));
}
stack.stackEmpty();
System.out.println("Is empty?:"+stack.pop());
}
};
程序是不成功就直接返回. public void push(Object obj)
{
if(curIndex == maxIndex-1)return;
items[++curIndex]=obj;
}这个地方如果到了栈底也最好抛异常! 这句"items[curIndex--]=null;"很好,相当于C++的释放内存,很重要. public Object pop()
{
if(curIndex == -1)return null;
Object tmp=items[curIndex];
items[curIndex--]=null;
return tmp;
}我感觉用一个集合类实现也是不错的方法,栈大小不受限制.以上只是个人观点,请大牛指正!楼上的方法是顺序存储.
我正在写一个Stack的链接实现,发现很不简单.写完后贴上来.package java.util;/**
* The <code>Stack</code> class represents a last-in-first-out (LIFO) stack of
* objects. It extends class <tt>Vector</tt> with five operations that allow a
* vector to be treated as a stack. The usual <tt>push</tt> and <tt>pop</tt>
* operations are provided, as well as a method to <tt>peek</tt> at the top
* item on the stack, a method to test for whether the stack is <tt>empty</tt>,
* and a method to <tt>search</tt> the stack for an item and discover how far
* it is from the top.
* <p>
* When a stack is first created, it contains no items.
*
* @author Jonathan Payne
* @version 1.28, 12/19/03
* @since JDK1.0
*/
public class Stack<E> extends Vector<E>
{
/**
* Creates an empty Stack.
*/
public Stack()
{
} /**
* Pushes an item onto the top of this stack. This has exactly the same
* effect as: <blockquote>
*
* <pre>
* addElement(item)
* </pre>
*
* </blockquote>
*
* @param item
* the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item)
{
addElement(item);
return item;
} /**
* Removes the object at the top of this stack and returns that object as
* the value of this function.
*
* @return The object at the top of this stack (the last item of the
* <tt>Vector</tt> object).
* @exception EmptyStackException
* if this stack is empty.
*/
public synchronized E pop()
{
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
} /**
* Looks at the object at the top of this stack without removing it from the
* stack.
*
* @return the object at the top of this stack (the last item of the
* <tt>Vector</tt> object).
* @exception EmptyStackException
* if this stack is empty.
*/
public synchronized E peek()
{
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
} /**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains no items;
* <code>false</code> otherwise.
*/
public boolean empty()
{
return size() == 0;
} /**
* Returns the 1-based position where an object is on this stack. If the
* object <tt>o</tt> occurs as an item in this stack, this method returns
* the distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to be at
* distance <tt>1</tt>. The <tt>equals</tt> method is used to compare
* <tt>o</tt> to the items in this stack.
*
* @param o
* the desired object.
* @return the 1-based position from the top of the stack where the object
* is located; the return value <code>-1</code> indicates that the
* object is not on the stack.
*/
public synchronized int search(Object o)
{
int i = lastIndexOf(o); if (i >= 0) { return size() - i; }
return -1;
} /** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}
声明一个Box类。
1. 该类有3个float类型的数据成员长、宽和高。
2. 有一个不带参数的构造器,它负责扰长、宽和高都初始化为5。
3. 有一个带三个参数的构造器,它负责把长、宽和高设置为程序员指定的值。
4. 有一个vol方法返回Box的体积。
5. 有一个area方法返回Box的全面积。
另外在main方法中创建二个长、宽、高分别为(3,4,5)和(6,7,8)的Box,并打印出这两个Box的体积和全面积。要求main方法不能声明在Box类中。class Box{
float length;
float width;
float height;
Box(){
length=width=height=5;
}
Box(float l,float w,float h){
length=l;
width=w;
height=h;
}
float vol(){
return length*width*height;
}float area(){
//2(长*宽+宽*高+长*高).
return 2*(length*width+ width*height + length *height);
} public String toString(){
return vol()+"";
}
}class Test{
public static void main(String[] args){
System.out.println(“体积:”+Box(3,4,5)+ “”+“面积:”+ Box(6,7,8));
}
}
应改为:
System.out.println("体积:"+new Box(3,4,5).col()+"面积"+new Box(6,7,8).area());
其它地方感觉没什么问题(我没在机器上运行).不过最好你的类中方法加个修饰符,public,private,protected什么的,你的程序没写,默认是包中可见,就是只有在这个包中的类能调用,其它的类不能!
你写toString()方法是一个好习惯,但是写的不好(我感觉),写得与类内部信息没什么关系.虽然是类的属性算出来的,但是属性不同也可以算出同样的值,如长、宽、高颠倒一下得出一样的结果。最好改成
return "lengh = " + lengh + ", width = " + width + ", leight = " + leight;按题意你的输出方法也不对,题目是让"分别输出".
{
public void initStack(); public void push(Object obj); public Object pop(); // stackEmpty 这个方法应该叫这个名字 isEmpty
public boolean isEmpty(); // 感觉栈应该有这个方法
public int size();
}public class LinkedStack implements MyStack
{
private Node head;
private int size; class Node extends Object
{
private Object value = null;
private Node next = null;
private Node prior = null; // private Node getPrior() throws NullPointerException
// {
// if (prior == null)
// throw new NullPointerException("没有");
// return prior;
// } public Node()
{
} private void setNext(Node n)
{
next = n;
} private Node getNext()
{
return this.next;
} private void setValue(Object o)
{
value = o;
} private Object getValue()
{
return value;
} private void setPrior(Node prior)
{
this.prior = prior;
} private Node getPrior()
{
return prior;
} private Node destroy()
{
this.value = null;
Node p = this.prior;
this.prior = null;
return p;
}
} public void initStack()
{
head = new Node();
size = 0;
} public synchronized void push(Object obj) throws NullPointerException
{
head.setValue(obj);
head.setNext(new Node());
head.getNext().setPrior(head);
head = head.getNext();
size = size + 1;
} public synchronized Object pop()
{
if (size == 0) { throw new NullPointerException("栈已空"); }
Object o = head.getPrior().getValue();
head = head.destroy();
size = size - 1;
return o;
} public boolean isEmpty()
{
if (size == 0)
return true;
else
return false;
} public int size()
{
return this.size;
}
}写的像是 C++ 的双链表。如没加头指针,只有一个栈项指针。