StackDemo shows how to use the linear set of objects

•February 25, 2009 • Leave a Comment

//— class StackDemo shows how to use the linear set of objects —//
import java.applet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class StackDemo extends Applet {
//— Instance variables —//
Frame f;

//— Instance methods —//
public void init ()
{
f = new StackFrame (“Stack”); // instanciate a frame object
f.setSize (200, 150);
f.setResizable (false);
f.show ();
}

public void start ()
{
f.setVisible (true);
repaint ();
}

public void stop ()
{
f.setVisible (false);
}

public void paint (Graphics g)
{
g.drawString (“Ancestor object for the frame”, 0, 30);
}
}// end of class StackDemo

//— class StackFrame —//
class StackFrame extends Frame {

StackPanel p;
Stack stk;
MessageBox mb;

//— Constructor —//
public StackFrame (String title)
{
super (title);

//— The object to handle action events —//
ActionListener al = new ActionHandler ();

//— Instanciate a panel object and add it into the frame —//
p = new StackPanel (al);
add(BorderLayout.CENTER, p);

//— Instanciate a message box class —//
mb = new MessageBox (this, “Stack Message”, al);

//— Instanciate stack class —//
stk = new Stack ();

//— anonimous inner class for handling window events —//
addWindowListener (new WindowAdapter (){
public void windowClosing (WindowEvent e)
{
e.getWindow ().dispose ();
}
});
}

//— Inner private class StackPanel —//
private class StackPanel extends Panel {
//— Instance variables —//
Label lblTitle;
TextField tfStack;
Button btnPush, btnPop, btnPeek;

//— Constructor —//
public StackPanel (ActionListener al)
{
super ();

lblTitle = new Label (“Stack element”);
tfStack  = new TextField (25);
btnPush  = new Button (“Push”);
btnPop   = new Button (“Pop “);
btnPeek  = new Button (“Peek”);

add (lblTitle);
add (tfStack);
add (btnPush);
add (btnPop);
add (btnPeek);

btnPush.addActionListener (al);
btnPop.addActionListener (al);
btnPeek.addActionListener (al);

setBackground (Color.green);
}
}// end of class StackPanel

//— Inner class MessageBox —//
private class MessageBox extends Dialog {
Label lbl;  // text to show in the dialog box
Button btnOK; // botton OK

//— Constructor —//
public MessageBox (Frame parent, String title, ActionListener al)
{
super (parent, title, true); //call the ancestor constructor and make the dialog box modal
setSize (100, 150);
setBackground (Color.red);

//— Change the layout and add components into the container —//
setLayout (new FlowLayout ());
add ( lbl = new Label ());
add ( btnOK = new Button (“OK”));

btnOK.addActionListener (al);
}

//— Methods —//
public void showBox (String s)
{
lbl.setText (s);
show ();
}
}// end class MessageBox

//— Inner class ActionHandler —//
private class ActionHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
Object source = e.getSource ();
String s = p.tfStack.getText ();

if (source == p.btnPush)
{
//— If the tfStack is not empty than pust its content into the stack —//
if (!s.equals (“”))
stk.push (s);

p.tfStack.setText (“”);
System.out.println (“Push”);
p.tfStack.requestFocus ();
}

else if (source == p.btnPop)
{
//— If the stack is not empty, pop the element of the stack —//
if (!stk.empty ())
p.tfStack.setText ((String) stk.pop ());
else
{
p.tfStack.setText (“”);
mb.showBox (“Stack is empty”);
}

System.out.println (“Pop”);
p.tfStack.requestFocus ();
}

else if (source == p.btnPeek)
{
//— If the stack is not empty, pop the element of the stack —//
if (!stk.empty ())
p.tfStack.setText ((String) stk.peek ());
else
{
p.tfStack.setText (“”);
mb.showBox (“Stack is empty”);
}

System.out.println (“Peek”);
p.tfStack.requestFocus ();
}

else if (source == mb.btnOK)
{
mb.dispose ();
}
}
}// end of class ActionHandler
}// end of class StackFrame

Hide Password From Command Line

•February 25, 2009 • Leave a Comment

HidePasswordFromCommandLine.java

import java.io.*;

/**
* Created on Feb 21, 2003
*/
public class HidePasswordFromCommandLine extends Thread {
boolean stopThread= false;
boolean hideInput= false;
boolean shortMomentGone= false;
public void run() {
try {
sleep(500);
} catch (InterruptedException e) {}
shortMomentGone= true;
while (!stopThread) {
if (hideInput) {
System.out.print(“\b*”);
}
try {
sleep(1);
} catch (InterruptedException e) {}
}
}
public static void main(String[] arguments) {
String name= “”;
String password= “”;
HidePasswordFromCommandLine hideThread= new HidePasswordFromCommandLine();
hideThread.start();
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println(“Name: “);
// Wait for the username and clear the keyboard buffer (if neccessarry)
do {
name= in.readLine();
}
while (hideThread.shortMomentGone == false);
// Now the hide thread should begin to overwrite any input with “*”
hideThread.hideInput= true;
// Read the password
System.out.println(“\nPassword:”);
System.out.print(” “);
password = in.readLine();
hideThread.stopThread= true;
}
catch (Exception e) {}
System.out.print(“\b \b”);
// JUST FOR TESTING – PLEASE DELETE!
System.out.println(“\n\nLogin= ” + name);
System.out.println(“Password= ” + password);
}
}

Input through Keyboard

•February 25, 2009 • Leave a Comment

/*The following program is written in a very simple form to show the keyboard input from the programmer which
returns the corresponding value conditioned by the if-statement in the method min(int, int) */

import java.io.*;

public class KeyBoardInput {

public static int min( int a, int b) {

if (a <= b)
return a;
else
return b;
}

public static void main(String[] args) throws IOException {

int x;
int y;

String sb;
String ks;

BufferedReader bk = new BufferedReader (
new InputStreamReader(System.in));

System.out.println (“Enter x “);

sb = bk.readLine();
x = Integer.parseInt(sb);

System.out.println(“Enter y”);

ks = bk.readLine();
y = Integer.parseInt(ks);

bk.close();

System.out.print(“The returned value is =  “);
System.out.println(Math.min(x * 5, y + 20));
}
}

 
Follow

Get every new post delivered to your Inbox.