» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
סקירה
דף ראשי  מתקדמים  וקטורים, Hashtables ורשימות  סקירה גרסה להדפסה

סקירה

ש- כתוב אפליקצית Java אשר מתאימה לפירוט הבא.

/* File SampProg153.java Copyright 1998, R.G.Baldwin
From lesson 76

Without viewing the solution that follows, write a Java
application that uses the Vector class to implement a
LIFO stack.  Make certain that the data in the stack is
available only on a LIFO basis using the methods shown
below.  Make the stack class capable of accommodating 
objects of any type. Throw an exception if the user 
attempts to pop an empty stack.

Methods:
isEmpty() -    returns true if this stack is empty. 
pop() -        Removes and returns the object from the top
               of this stack.
push(Object) - Pushes an item onto the top of this stack. 

This program was tested using JDK 1.1.3 under Win95.

The output from the program is:

Mary Jones
Sue Williams
Joe Johnson
Dick Baldwin
java.util.NoSuchElementException
**********************************************************/
import java.io.*;
import java.util.*;

//=======================================================//
class MyStack{
  //Note that this class contains and does not extend
  // Vector.
  private Vector stack = new Vector();
  //-----------------------------------------------------//

  boolean isEmpty(){
    return stack.isEmpty();
  }//end isEmpty()
  //-----------------------------------------------------//
  
  void push(Object item){
    stack.addElement(item);
  }//end push()
  //-----------------------------------------------------//
  
  Object pop()throws NoSuchElementException{
    Object temp = stack.lastElement();
    stack.removeElementAt((stack.size()-1));

//    stack.removeElementAt(stack.lastIndexOf(temp));
    return temp;
  }//end pop()
  //-----------------------------------------------------//
}//end class MyStack

//=======================================================//
class SampProg153{//controlling class
  public static void main(String[] args){
    MyStack stack = new MyStack();
    stack.push(new TestClass("Dick","Baldwin"));
    stack.push(new TestClass("Joe","Johnson"));
    stack.push(new TestClass("Sue","Williams"));
    stack.push(new TestClass("Mary","Jones"));

    try{
      while(!stack.isEmpty())
        System.out.println(stack.pop());
        
      //Try to pop an empty stack
      System.out.println(stack.pop());

    }catch(NoSuchElementException e){System.out.println(e);}
  }// end main
}//end class SampProg153 definition
//======================================================//

class TestClass{
  String first;
  String last;
  
  TestClass(String first, String last){//constructor
    this.first = first;
    this.last = last;
  }//end constructor
  //----------------------------------------------------//
  public String toString(){
    return first + " " + last;
  }//end toString()
}//end TestClass
//======================================================//

 

ש- כתוב תוכנית Java המתאימה לפירוט הבא.

 

/* File SampProg154.java Copyright 1998, R.G.Baldwin
From lesson 76

Without viewing the solution that follows, write a Java
application that uses the Vector class to implement a
LIFO structure similar to a stack.  

This structure has set and get methods that mirror the
typical push and pop methods in a stack.

However, this structure also has an enumerator that allows
you to access the individual elements in the structure
in sequential order, and to modify them in the process.

Make the structure class capable of accommodating 
objects of any type. Throw an exception if the user 
attempts to get an element from an empty structure.

Methods:
isEmpty() -   Returns true if the structure is empty. 
get() -       Removes and returns the object from the top
              of the structure.
set(Object) - Stores an element onto the top of the 
              structure. 

This program was tested using JDK 1.1.3 under Win95.

Typical output from the program is:
Set the structure
Enumerate and modify the structure
Dick Baldwin
Joe Johnson
Sue Williams
Mary Jones
Get the modified structure
Mary Modified
Sue Modified
Joe Modified
Dick Modified
java.util.NoSuchElementException
**********************************************************/
import java.io.*;
import java.util.*;

//=======================================================//
class MyStructure{
  private Vector structure = new Vector();
  //-----------------------------------------------------//

  boolean isEmpty(){
    return structure.isEmpty();
  }//end isEmpty()
  //-----------------------------------------------------//
  
  void set(Object item){
    structure.addElement(item);
  }//end set()
  //-----------------------------------------------------//
  
  Object get()throws NoSuchElementException{
    Object temp = structure.lastElement();
    structure.removeElementAt(structure.size()-1);
    return temp;
  }//end get()
  //-----------------------------------------------------//

  Enumeration getEnumerator(){
    return new Enumerator(this);
  }//end getEnumerator()
  //-----------------------------------------------------//
  
  int getSize(){
    return structure.size();
  }//end getSize()
  //-----------------------------------------------------//

  Object getElement(int which){
    return structure.elementAt(which);
  }//end getElement()
  //-----------------------------------------------------// 
}//end class MyStructure

//=======================================================//

class Enumerator implements Enumeration{
  MyStructure theStructure;
  int elementCounter;
  //-----------------------------------------------------//

  Enumerator(MyStructure theStructure){//constructor
    this.theStructure = theStructure;
    elementCounter = 0;
  }//end constructor
  //-----------------------------------------------------//
  public boolean hasMoreElements(){
    return (elementCounter < theStructure.getSize());
  }//end hasMoreElements
  //-----------------------------------------------------//

  public Object nextElement(){
    return theStructure.getElement(elementCounter++);
  }//end nextElement
}//end class Enumerator
//=======================================================//

class SampProg154{//controlling class
  public static void main(String[] args){
    MyStructure structure = new MyStructure();
    System.out.println("Set the structure");
    structure.set(new TestClass("Dick","Baldwin"));
    structure.set(new TestClass("Joe","Johnson"));
    structure.set(new TestClass("Sue","Williams"));
    structure.set(new TestClass("Mary","Jones"));
    
    System.out.println(
                   "Enumerate and modify the structure");
    Enumeration enum = structure.getEnumerator();
    
    TestClass temp;
    while(enum.hasMoreElements()){
      temp = (TestClass)enum.nextElement();
      //Display the element
      System.out.println(temp);
      //Modify the element
      temp.last = "Modified";
    }//end while loop
    
    System.out.println("Get the modified structure");
    try{
      while(!structure.isEmpty())
        System.out.println(structure.get());
        
      //Try to get element from an empty structure
      System.out.println(structure.get());

    }catch(NoSuchElementException e){
      System.out.println(e);
    }//end catch
  }// end main
}//end class SampProg154 definition
//=======================================================//

class TestClass{
  String first;
  String last;
  
  TestClass(String first, String last){//constructor
    this.first = first;
    this.last = last;
  }//end constructor
  //----------------------------------------------------//
  public String toString(){
    return first + " " + last;
  }//end toString()
}//end TestClass
//======================================================//

 

 

 02-12-03 / 20:28  עודכן ,  13-10-03 / 19:17  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תוכנית - הקודםהבא - Callbacks - קריאה לאחור I 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 1