Archive

Archive for the ‘Others’ Category

Java Program to write an Applet with Check Boxes handling and display output – Q55

January 28, 2009 Leave a comment

Q55: Java Program to write an applet, which shows a two set of check boxes. The first set is independent and second set belongs to a checkbox group (like radio buttons). Whichever check box is selected by the user, it should be displayed on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

abstract class Ap_CheckboxGroupDemo extends Applet implements ItemListener{
 	String msg = "";
 	Checkbox Win98, WinNT;
 	CheckboxGroup cbg;
 	Button Win;
 	MyCheckbox myCheckbox1,myCheckbox2,myCheckbox3;

 	public void init(){
 		Win98 = new Checkbox("Windows 98/Xp");
 		WinNT = new Checkbox("Windows NT/2000");
 		
 		add(Win98);
 		add(WinNT);
 		
 		Win98.addItemListener(this);
 		WinNT.addItemListener(this);
 		
 		cbg = new CheckboxGroup();
 		myCheckbox1 = new MyCheckbox("Item 1",cbg,true);
 		add(myCheckbox1);
 		
 		myCheckbox2 = new MyCheckbox("Item 2",cbg,false);
 		add(myCheckbox2);
 		
 		myCheckbox3 = new MyCheckbox("Item 3",cbg,false);
 		add(myCheckbox3);
	}
	
	public void paint(Graphics g){
		msg = "current state: ";
 		g.drawString(msg,6,80);
 		msg = "Windows 98/Xp:" + Win98.getState();
 		g.drawString(msg,6,100);
 		msg = "Windows NT/2000:" + WinNT.getState();
 	}
 }
 		
 


class MyCheckbox extends Checkbox{
 	
 	public MyCheckbox(String label,CheckboxGroup cbg,boolean flag){
 		super(label,cbg,flag);
 		enableEvents(AWTEvent.ITEM_EVENT_MASK);
	}
	
	protected void processItemEvent(ItemEvent ie){
		showStatus("checkbox name/state: " + getLabel() + 
 					"/" + getState());
 		super.processItemEvent(ie);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an Applet which handles user input and show output – Q54

January 27, 2009 Leave a comment

Q54: Java Program to write an applet, which shows one choice item and one list item. Add some items in both. Whenever user selects an item that message should be displayed on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Ap_ListDemo extends Applet implements ActionListener {
	
	List os, browser;
	String msg = "";
	
	public void init(){
		os = new List(4,true);
		
		browser = new List(4,false);
		
		os.add("Windows 98/Xp");
		os.add("Solaris");
		os.add("Macos");
		
		browser.add("netscape 3.x");
		browser.add("netscape 4.x");
		browser.add("netscape 5.x");
		browser.add("netscape 6.x");
		browser.add("internet explorer 4.0");
		browser.add("internet explorer 5.0");
		browser.add("internet explorer 6.0");
		browser.add("Lynx 2.4");
		
		browser.select(1);
		
		add(os);
		add(browser);
		
		os.addActionListener(this);
		browser.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent ae){
		repaint();
	}
	
	public void paint(Graphics g){
		int idx[];
		
		msg = "current OS";
		idx = os.getSelectedIndexes();
		for(int i = 0; i < idx.length; i++)
			msg += os.getItem(idx[i]) + " ";
		
		g.drawString(msg,6,120);
		msg = "current Browser: ";
		msg += browser.getSelectedItem();
		g.drawString(msg,6,140);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an Applet having data entry operation (basic calculator) – Q53

January 27, 2009 Leave a comment

Q53: Java Program to write an applet which have three text fields, first two accepts input from user and third one shows the result. It should have four buttons indicating operation add, subtract, multiply and divide, pressing the button should put the result in the third text field.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.TextField.*;

public class Ap_Calc extends Applet implements ActionListener{
	TextField one, two, res;
	Button add, sub, mul, div;

	public void init(){
		Label l1 = new Label("First Number:   ",Label.RIGHT);
		Label l2 = new Label("Second Number:  ",Label.RIGHT);
		Label l3 = new Label("Result:         ",Label.RIGHT);
 
		one = new TextField(5);
		two = new TextField(5);
		res = new TextField(7);

		add = new Button("Add");
		sub = new Button("Substract");
		mul = new Button("Multiply");
		div = new Button("Divide");

		add(l1);
		add(one);
		add(l2);
		add(two);
		add(l3);
		add(res);
	
		add(add);
		add(sub);
		add(div);
		add(mul);

		one.addActionListener(this);
		two.addActionListener(this);
		res.addActionListener(this);
		add.addActionListener(this);
		sub.addActionListener(this);
		div.addActionListener(this);
		mul.addActionListener(this);		
	}

	public void actionPerformed(ActionEvent ae){
		String str = ae.getActionCommand();
		float b1, b2, b3 = 0;
		String msg;
		
		b1 = Float.parseFloat(one.getText());
		b2 = Float.parseFloat(two.getText());
		
		if(str.equals("Add"))
			b3 = b1 + b2;
		if(str.equals("Substract"))
			b3 = b1 - b2;
		if(str.equals("Divide"))
			b3 = b1 / b2;
		if(str.equals("Multiply"))
			b3 = b1 * b2;
		msg = String.valueOf(b3);
		res.setText(msg);	
		repaint();
	}
	
	public void paint(Graphics g){
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an applet to make tic-tac-toe – Q52

January 26, 2009 Leave a comment

Q52: Java Program to write an applet to make tic-tac-toe.

import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.awt.event.*;
import java.lang.String.*;
import java.awt.Label.*;

public class Ap_Tic_Tac_Toe extends Applet implements ActionListener{
	static int k = 1;
	Button a1, a2, a0, a3, a4, a5, a6, a7, a8;
	static String b1, b2, b3, b4, b5, b6, b7, b8, b9;
	String msg = " ";
	TextField ne,ne1,ne2;

	public void init(){
		setLayout(new GridLayout(4,3));
		ne = new TextField(13);
		ne1 = new TextField(13);
		ne2 = new TextField(13);
		a0 = new Button("1");
		a1 = new Button("2");
		a2 = new Button("3");
		a3 = new Button("4");	
		a4 = new Button("5");
		a5 = new Button("6");
		a6 = new Button("7");
		a7 = new Button("8");
		a8 = new Button("9");
		
		add(a0);
		add(a1);
		add(a2);
		
		add(a3);
		add(a4);
		add(a5);
		
		add(a6);
		add(a7);
		add(a8);
		add(ne);
		add(ne1);
		add(ne2);
		a0.addActionListener(this);
		a1.addActionListener(this);
		a2.addActionListener(this);
		a3.addActionListener(this);
		a4.addActionListener(this);
		a5.addActionListener(this);
		a6.addActionListener(this);
		a7.addActionListener(this);
		a8.addActionListener(this);
		ne.addActionListener(this);
		ne1.addActionListener(this);
		ne2.addActionListener(this);
		ne1.setText("Player 1:  X");
		ne2.setText("Player 2:  0");
	}

	public void actionPerformed(ActionEvent ae){
		String str = ae.getActionCommand();
		if(str.equals("1")){
			if(k%2 == 0)
				a0.setLabel("0");
			else
				a0.setLabel("X");
		}
		if(str.equals("2")){
			if(k%2 == 0)
				a1.setLabel("0");
			else
				a1.setLabel("X");
		}
		if(str.equals("3")){
			if(k%2 == 0)
				a2.setLabel("0");
			else
				a2.setLabel("X");
		}
		if(str.equals("4")){
			if(k%2 == 0)
				a3.setLabel("0");
			else
				a3.setLabel("X");
		}
		if(str.equals("5")){
			if(k%2 == 0)
				a4.setLabel("0");
			else
				a4.setLabel("X");
		}
		if(str.equals("6")){
			if(k%2 == 0)
				a5.setLabel("0");
			else
				a5.setLabel("X");
		}
		if(str.equals("7")){
			if(k%2 == 0)
				a6.setLabel("0");
			else
				a6.setLabel("X");
		}
		if(str.equals("8")){
			if(k%2 == 0)
				a7.setLabel("0");
			else
				a7.setLabel("X");
		}
		if(str.equals("9")){
			if(k%2 == 0)
				a8.setLabel("0");
			else
				a8.setLabel("X");
			}
		k++;
		check();
	}
	
	public void check(){
		
		String msg1 = "Player 1 Wins";
		String msg2 = "Player 2 Wins";
		b1 = a0.getLabel();
		b2 = a1.getLabel();
		b3 = a2.getLabel();
		b4 = a3.getLabel();
		b5 = a4.getLabel();
		b6 = a5.getLabel();
		b7 = a6.getLabel();
		b8 = a7.getLabel();
		b9 = a8.getLabel();
		
		if(b1.equals("X")&&b2.equals("X")&&b3.equals("X"))
		ne.setText(msg1);
		
		if(b1.equals("0")&&b2.equals("0")&&b3.equals("0"))
		ne.setText(msg2);
		
		if(b4.equals("X")&&b5.equals("X")&&b6.equals("X"))
		ne.setText(msg1);
		
		if(b4.equals("0")&&b5.equals("0")&&b6.equals("0"))
		ne.setText(msg2);
		
		if(b7.equals("X")&&b8.equals("X")&&b9.equals("X"))
		ne.setText(msg1);
		
		if(b7.equals("0")&&b8.equals("0")&&b9.equals("0"))
		ne.setText(msg2);
		
		if(b1.equals("X")&&b4.equals("X")&&b7.equals("X"))
		ne.setText(msg1);
		
		if(b1.equals("0")&&b4.equals("0")&&b7.equals("0"))
		ne.setText(msg2);

		if(b2.equals("X")&&b5.equals("X")&&b8.equals("X"))
		ne.setText(msg1);
		if(b2.equals("0")&&b5.equals("0")&&b8.equals("0"))
		ne.setText(msg2);

		if(b6.equals("X")&&b3.equals("X")&&b9.equals("X"))
		ne.setText(msg1);
		if(b9.equals("0")&&b6.equals("0")&&b3.equals("0"))
		ne.setText(msg2);
	
		if(b1.equals("X")&&b5.equals("X")&&b9.equals("X"))
		ne.setText(msg1);
		if(b1.equals("0")&&b5.equals("0")&&b9.equals("0"))
		ne.setText(msg2);

		if(b3.equals("X")&&b5.equals("X")&&b7.equals("X"))
		ne.setText(msg1);
		if(b3.equals("0")&&b5.equals("0")&&b7.equals("0"))
		ne.setText(msg2);
	}
	
	public void paint(Graphics g){
		g.drawString(msg,299,100);
		}
	}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an applet with Mouse Event Handling using inner class which extends MouseAdapter – Q51

January 26, 2009 Leave a comment

Q51: Java Program to write an applet which does mouse event handling using concept of inner class which extends MouseAdapter.

import java.applet.*;
import java.awt.event.*;

public class Ap_MousePressedDemo extends Applet{
	public void init(){
		addMouseListener(new MyMouseAdapter(this));
	}
}

class MyMouseAdapter extends MouseAdapter{
	Ap_MousePressedDemo mousePressedDemo;
	public MyMouseAdapter(Ap_MousePressedDemo mousePressedDemo){
		this.mousePressedDemo = mousePressedDemo;
	}
	public void mousePressed(MouseEvent me){
		mousePressedDemo.showStatus("Mouse Pressed.");
	}
} 

… from College notes (BCA/MCA assignments):