Watch & Subscribe my SQL videos on YouTube | Join me on Facebook

Java Program to display a moving banner on the applet window – Q47

January 24, 2009 Leave a comment

Q47: Java Program to display a moving banner on the applet window.

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

public class Ap_Banner extends Applet implements Runnable{
	
	String msg = "My name is Manoj Pandey";
	Thread th;
	int state;
	boolean stopflag;
	
	public void init(){
		setBackground(Color.blue);
		setForeground(Color.yellow);
	}
	
	public void start(){
		th = new Thread(this);
		stopflag = false;
		th.start();
	}
	
	public void run(){
		char ch;
		for(;;){
			try{
				repaint();
				Thread.sleep(500);
				ch = msg.charAt(0);
				msg = msg.substring(1, msg.length());
				msg += ch;
				if(stopflag) break;
			}
			catch(InterruptedException e){
			}
		}
	}
	
	public void sleep(){
		stopflag = true;
		th = null;
	}
	
	public void paint(Graphics g){
		g.drawString(msg, 50, 50);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to display a simple message “A simple Applet” on the applet window – Q46

January 23, 2009 Leave a comment

Q46: Java Program to display a simple message “A simple Applet” on the applet window.

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

public class Ap_Simple extends Applet{
	public void paint(Graphics g){
		g.drawString("Manoj Pandey", 50, 50);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to use all the methods defined by StringBuffer class which are different from String class – Q45

January 23, 2009 Leave a comment

Q45: Java Program to use all the methods defined by StringBuffer class which are different from string class.

class Str_Buffer{
	
	public static void main(String args[]){
		StringBuffer sb = new StringBuffer("Hello");

		System.out.println("buffer before= " + sb);
		System.out.println("charAt(1) before= " + sb.charAt(1));
		
		sb.setCharAt(1, 'i');
		sb.setLength(2);
		
		System.out.println("buffer after= " + sb);
		System.out.println("charAt(1) after= " + sb.charAt(1));
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to use all the methods defined by string class (substring, indexOf, length, charAt, getchars etc) – Q44

January 22, 2009 Leave a comment

Q44: Java Program to use all the methods defined by string class (substring, indexOf, length, charAt, getchars etc).

class StringMethods{
	public static void main(String args[]){
	    String s = "This is a demo of the getchars method.";
		int start = 10;
		int end = 14;
		char buf[] = new char[end - start];
		s.getChars(start, end, buf, 0);
	
		System.out.println(buf);
		String org = "This is a test. This is, too.";
		String search = "is";
		String sub = "was";
		String result = "";
		int i;
		
		do{
			System.out.println(" ");
			System.out.println(org);
			i = org.indexOf(search);
			if(i != -1){
				result = org.substring(0, i);
				result = result + sub;
				result = result + org.substring(i + search.length());
				org = result;
			}
		}while(i != -1);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to handle object serialization using ObjectInputStream and ObjectOutputStream – Q43

January 22, 2009 Leave a comment

Q43: Java Program to handle object serialization using ObjectInputStream and ObjectOutputStream.

import java.io.*;

public class SerializationDemo{
	public static void main(String args[]){
		try{
			Myclass object1 = new Myclass("Hello", -7, 2.7e10);
			System.out.println("object1: " + object1);
			FileOutputStream fos = new FileOutputStream("serial");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(object1);
			oos.flush();
			oos.close();
		}
		catch(IOException e){
			System.out.println("Exception during serialization: " + e);
			System.exit(0);
			
		}
		try{
			Myclass object2;
			FileInputStream fis = new FileInputStream("serial");
			ObjectInputStream ois = new ObjectInputStream(fis);
			object2 = (Myclass)ois.readObject();
			ois.close();
			System.out.println("object2: " + object2);
		}
		catch(Exception e){
			System.out.println("Exception during Deserialization: " + e);
			System.exit(0);
		}
	}
}
class Myclass implements Serializable{
	String s;
	int i;
	double d;
	public Myclass(String s, int i, double d){
		this.s = s;
		this.i = i;
		this.d = d;
	}
	public String toString(){
		return "s= " + s + "; i= " + i + "; d= " + d;
	}
}

… from College notes (BCA/MCA assignments):