ceva java

By mariachi7

Deci sa incep cu niste secvente de cod:

//Coada generica

// MainFrame.java

package pack;

import javax.swing.*;

import pack.Queue;

import java.awt.*;

import java.awt.event.*;

public class MainFrame extends WindowAdapter implements ActionListener {

private int maxX = 500;

private int maxY = 500;

protected JTextField textField;

//stiva:

Queue<String> coada = new Queue<String>(10);

//initializare MainFrame

public MainFrame() {

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

maxX = screenSize.width – 50;

maxY = screenSize.height – 50;

}

//Butoane

protected JComponent createButtonPane() {

JButton b1 = new JButton(“Poll”);

b1.setActionCommand(“poll”);

b1.addActionListener(this);

JButton b2 = new JButton(“Offer”);

b2.setActionCommand(“offer”);

b2.addActionListener(this);

JButton b3 = new JButton(“Element”);

b3.setActionCommand(“element”);

b3.addActionListener(this);

JButton b4 = new JButton(“Peek”);

b4.setActionCommand(“peek”);

b4.addActionListener(this);

JButton b5 = new JButton(“Remove”);

b5.setActionCommand(“remove”);

b5.addActionListener(this);

JButton b6 = new JButton(“Add”);

b6.setActionCommand(“add”);

b6.addActionListener(this);

textField = new JTextField(20);

textField.addActionListener(this);

//panel cu butoane

JPanel pane = new JPanel();

pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

pane.add(b1);

pane.add(b2);

pane.add(b3);

pane.add(b4);

pane.add(b5);

pane.add(b6);

pane.add(textField);

return pane;

}

//Handle action event

public void actionPerformed(ActionEvent e) {

if (“add”.equals(e.getActionCommand())) {

try {

coada.add(textField.getText());

System.out.print(/*”S-a executat add in coada: ” +*/ textField.getText());

}catch(QueueOverflow ex)

{

System.err.println(“Caught QueueOverflow exception: “ + ex.getMessage());

}

}

else if(“offer”.equals(e.getActionCommand())) {

coada.offer(textField.getText());

System.out.print(/*”S-a executat offer in coada: ” +*/ textField.getText());

}

else if(“element”.equals(e.getActionCommand())){

try {

System.out.print(“Element: “ + coada.element());

}catch(QueueEmpty ex)

{

System.err.println(“Caught QueueEmpty exception: “ + ex.getMessage());

}

}

else if(“peek”.equals(e.getActionCommand())){

System.out.print(“Peek: “ + coada.peek());

}

else if(“remove”.equals(e.getActionCommand())) {

try {

System.out.print(“Remove: “ + coada.remove());

}catch(QueueEmpty ex)

{

System.err.println(“Caught QueueEmpty exception: “ + ex.getMessage());

}

}

else if(“poll”.equals(e.getActionCommand())){

System.out.print(“Poll: “ + coada.poll());

}

}

private static void createAndShowGUI(){

//Create and set up the window.

JFrame frame = new JFrame(“Coada Generica”);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MainFrame demo = new MainFrame();

//Add components to it.

Container contentPane = frame.getContentPane();

contentPane.add(demo.createButtonPane(),BorderLayout.PAGE_END);

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

——————————————————————-

//Queue.java

package pack;

import pack.QueueInterface;

// My Exceptions

class QueueOverflow extends Exception {

public QueueOverflow() {}

public QueueOverflow(String msg) {

super(msg);

}

}

class QueueEmpty extends Exception {

public QueueEmpty() {}

public QueueEmpty(String msg) {

super(msg);

}

}

public class Queue<T> implements QueueInterface<T> {

private T coada[];

private int front;

private int rear;

private int nItems;

int dim;

//Constructor

public Queue(int dim)

{

coada = (T[])new Object[dim];

this.dim = dim;

front = 0;

rear = -1;

nItems = 0;

}

//Add & Offer

public void add(T item) throws QueueOverflow {

if(rear==dim-1)

throw new QueueOverflow(“Coada plina”);

else {

coada[++rear]=item;

nItems++;

}

}

public boolean offer(T item) {

if(rear==dim-1)

return false;

else {

coada[++rear]=item;

nItems++;

return true;

}

}

//Element & Peek

public T element() throws QueueEmpty {

T temp = coada[front];

if(front==dim – 1)

throw new QueueEmpty(“Coada goala”);

else {

nItems–;

return temp ;

}

}

public T peek() {

T temp = coada[front];

if(front==dim – 1)

return null;

else {

nItems–;

return temp ;

}

}

//Pop & Poll

public T remove() throws QueueEmpty {

T temp = coada[front++];

if(front==dim)

throw new QueueEmpty(“Coada goala”);

else {

nItems–;

return temp ;

}

}

public T poll() {

T temp = coada[front++];

if(front==dim)

return null;

else {

nItems–;

return temp ;

}

}

}

———————————————————————————————-

//QueueInterface.java

package pack;

public interface QueueInterface<T> {

void add (T item)throws QueueOverflow;

boolean offer(T item);

T element()throws QueueEmpty;

T peek();

T remove()throws QueueEmpty;

T poll();

}

————————————————————–

 

 

________________________________________________________________________

//stack

 

//StackInterface.java

 

package pack;

 

import pack.StackEmpty;

import pack.StackOverflow;

 

public interface StackInterface<T> {

      void push (T item)throws StackOverflow;

      boolean offer(T item);

      T element()throws StackEmpty;

      T peek();

      T pop()throws StackEmpty;

      T poll();

     

}

 

//Stack.java

package pack;

 

import pack.StackEmpty;

import pack.StackInterface;

import pack.StackOverflow;

 

// My Exceptions

class StackOverflow extends Exception {

      public StackOverflow() {}

      public StackOverflow(String msg) {

            super(msg);

      }

}

class StackEmpty extends Exception {

      public StackEmpty() {}

      public StackEmpty(String msg) {

            super(msg);

      }

}

 

public class Stack<T> implements StackInterface<T> {

     

      private T stiva[];

      int dim;

      int k;

     

      //Constructor

      public Stack(int dim)

      {

            stiva =(T[])new Object[dim];

            this.dim=dim;

            k=-1;

      }

     

      //Push & Offer

      public void push(T item) throws StackOverflow {

            if(k==dim)

                  throw new StackOverflow(“Stiva plina”);

            else

                  stiva[++k]=item;             

      }

     

      public boolean offer(T item) {

            if(k==dim)

                  return false;

            else {

                  stiva[++k]=item;

                  return true;

            }

      }

     

      //Element & Peek

      public T element() throws StackEmpty {

            if(k==-1)

                  throw new StackEmpty(“Stiva goala”);

            else

                  return stiva[k];

      }

     

      public T peek() {

            if (k==-1)

                   return null;

            else return stiva[k];

      }

     

      //Pop & Poll

      public T pop() throws StackEmpty {

            if(k==-1)

                  throw new StackEmpty(“Stiva goala”);

            return stiva[k--];

      }

     

      public T poll() {

            if (k==-1)

                   return null;

            else return stiva[k--];

      }

 

}

 

—————————————————————-

//MainFrame.java

 

package pack;

 

 

import javax.swing.*;

 

import java.awt.*;

import java.awt.event.*;

 

 

public class MainFrame extends WindowAdapter implements ActionListener {

            private int maxX = 500;

          private int maxY = 500;

         

          protected JTextField textField;

         

          //stiva:

          Stack<String> stiva = new Stack<String>(10);

                

            //initializare MainFrame

            public MainFrame() {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

              maxX = screenSize.width – 50;

              maxY = screenSize.height – 50;

          }

           

            //Butoane

            protected JComponent createButtonPane() {

                  JButton b1 = new JButton(“Poll”);

                  b1.setActionCommand(“poll”);

                  b1.addActionListener(this);

                 

                  JButton b2 = new JButton(“Offer”);

                  b2.setActionCommand(“offer”);

                  b2.addActionListener(this);

                 

                  JButton b3 = new JButton(“Element”);

                  b3.setActionCommand(“element”);

                  b3.addActionListener(this);

                 

                  JButton b4 = new JButton(“Peek”);

                  b4.setActionCommand(“peek”);

                  b4.addActionListener(this);

                 

                  JButton b5 = new JButton(“Pop”);

                  b5.setActionCommand(“pop”);

                  b5.addActionListener(this);

                 

                  JButton b6 = new JButton(“Push”);

                  b6.setActionCommand(“push”);

                  b6.addActionListener(this);

                 

                  textField = new JTextField(20);

              textField.addActionListener(this);

                 

                  //panel cu butoane

                  JPanel pane = new JPanel();

              pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

              pane.add(b1);

              pane.add(b2);

              pane.add(b3);

              pane.add(b4);

              pane.add(b5);

              pane.add(b6);

              pane.add(textField);

              return pane;

            }

           

            //Handle action event

          public void actionPerformed(ActionEvent e) {

           

                if (“push”.equals(e.getActionCommand())) {

                  try {

                        stiva.push(textField.getText());

                        System.out.print(/*”S-a executat push in stiva: ” +*/ textField.getText());

                  }catch(StackOverflow ex)

                  {

                        System.err.println(“Caught StackOverflow exception: “ + ex.getMessage());

                  }

              }

                else if(“offer”.equals(e.getActionCommand())) {

                  stiva.offer(textField.getText());

                  System.out.print(/*”S-a executat offer in stiva: ” +*/ textField.getText());

              }

                else if(“element”.equals(e.getActionCommand())){

                  try {

                        System.out.print(“Element: “ + stiva.element());

                  }catch(StackEmpty ex)

                  {

                        System.err.println(“Caught StackEmpty exception: “ + ex.getMessage());

                  }

                }

                else if(“peek”.equals(e.getActionCommand())){

                  System.out.print(“Peek: “ + stiva.peek());               

                }

                else if(“pop”.equals(e.getActionCommand())) {

                  try {

                        System.out.print(“Pop: “ + stiva.pop());

                  }catch(StackEmpty ex)

                  {

                        System.err.println(“Caught StackEmpty exception: “ + ex.getMessage());

                  }

                }

                else if(“poll”.equals(e.getActionCommand())){

                  System.out.print(“Poll: “ + stiva.poll());               

                }

           

          }

           

 

          private static void createAndShowGUI(){

                //Create and set up the window.

                  JFrame frame = new JFrame(“Stiva generica”);

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

             

                            

              MainFrame demo = new MainFrame();

                         

              //Add components to it.

              Container contentPane = frame.getContentPane();

              contentPane.add(demo.createButtonPane(),BorderLayout.PAGE_END);

                                 

              //Display the window.

              frame.pack();

              frame.setVisible(true);

          }

     

          public static void main(String[] args) {

                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                  public void run() {

                        createAndShowGUI();

                  }

                });

          }

 

}

 

 

 

Lasă un Răspuns