Advertisements

Archive

Archive for January 27, 2009

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):


Advertisements
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: