Back to tyco.paawanchaudhary.com
Download the Advanced Java Lab Manual from here

Press F5, atleast 2 to 3 times to see the most updated verison of this website.


Index:-

Q1. Develop a program to add a label, a text field and a checkbox to an applet.
Q2. Develop a program to illustrate the use of Border Layout in an Applet.
Q3. Develop a program which makes the use of FlowLayout and GridLayout Manager.
Q4. Develop a program to display the given string in red color, Times New Roman font with italic style and font size of 14.
Q5. Develop a program which will increase the font size of a given string by 2 point with every mouse click.
Q6. Develop a program to set the background color of Applet as Yellow.
Q7. Develop a program to create 3 menus "Format", "Tool" and "Help" and add submenus under these items.Now uploaded!!!:-)
Q8. Develop a program to create an applet to accept a number in a text field and display the square of the number when a button with caption Square is pressed.
Q9. Develop a program to create an applet to find the factorial of the number entered in text field 1 when a button captioned ‘find factorial’ is pressed.
Q10. Develop a program to print protocol, port, host, file of http://www.msbte.com
Q11, Q12 and Q13. Database related.
Q14. Develop a program to design a frame having labels as “Name”, “Password”, and their corresponding text fields and two buttons named as “login” and “cancel” using swing components.
Q15. Develop a program in swing using the table component.
Q16. Develop a program in swing showing use of Tabbed Pane.
An example program of Database


Disclaimer:- Students are advised not to copy blindly from here, this website should only be used as a reference and not for copying/cheating. I am not responsible for the accuracy of the content provided on this site. This website is in no way related to any Educational Institute or Board, it has been created by a student to provide help and give guidance to other students.

Q1. Develop a program to add a label, a text field and a checkbox to an applet.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
1. Develop a program to add a label, a text field and a checkbox to an applet.
==================================================================================================================================*/
import java.awt.*;
import java.applet.*;
public class LabelTextCheck extends Applet
{
	Label l1;
	TextField t1;
	Checkbox c1;
	public void init()
	{
		l1 = new Label("Label 1");
		t1 = new TextField("TextField 1");
		c1 = new Checkbox("Checkbox 1");

		add(l1);
		add(t1);
		add(c1);
	}
}
/*
<applet code = "LabelTextCheck" width = 300 height = 100>
</applet>
*/
/*
For executing Applet programs, type this in CMD:-
javac LabelTextCheck.java
appletviewer LabelTextCheck.java
*/


Q2. Develop a program to illustrate the use of Border Layout in an Applet.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
2. Develop a program to illustrate the use of Border Layout in an Applet.
==================================================================================================================================*/
import java.awt.*;
import java.applet.*;
/*
<applet code = "BorderLayoutDemo" width = 400 height = 200>
</applet>
*/
public class BorderLayoutDemo extends Applet
{
	public void init()
	{
		setLayout(new BorderLayout());

		add(new Button("Header Portion"), BorderLayout.NORTH);
		add(new Button("Footer Portion"), BorderLayout.SOUTH);
		add(new Button("Right"), BorderLayout.EAST);
		add(new Button("Left"), BorderLayout.WEST);

		String msg = "Advanced Java Programming";
		add(new TextArea(msg), BorderLayout.CENTER);
	}
}

/*
For executing Applet programs, type this in CMD:-
javac BorderLayoutDemo.java
appletviewer BorderLayoutDemo.java
*/


Q3. Develop a program which makes the use of FlowLayout and GridLayout Manager.

Note: I tried pretty hard but couldn't apply both the Layouts in the same Applet, only one works, if you are able to incorporate both the LayoutManagers in the same program then please tell me, I'll correct the program below:-

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
3. Develop a program which makes the use of FlowLayout and GridLayout Manager.
This one is for FlowLayout only.
==================================================================================================================================*/
import java.awt.*;
import java.applet.*;
/*
<applet code = "FlowLayoutDemo" width = 250 height = 100>
</applet>
*/
public class FlowLayoutDemo extends Applet
{
	Checkbox c1, c2, c3, c4;

	public void init()
	{
		setLayout(new FlowLayout(FlowLayout.RIGHT));

		c1 = new Checkbox("Check Box 1");
		c2 = new Checkbox("Check Box 2");
		c3 = new Checkbox("Check Box 3");
		c4 = new Checkbox("Check Box 4");

		add(c1);
		add(c2);
		add(c3);
		add(c4);
	}
}

/*
For executing Applet programs, type this in CMD:-
javac FlowLayoutDemo.java
appletviewer FlowLayoutDemo.java
*/

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
3. Develop a program which makes the use of FlowLayout and GridLayout Manager.
This one is for GridLayout only.
==================================================================================================================================*/

import java.awt.*;
import java.applet.*;
/*
<applet code = "GridLayoutDemo" width = 300 height = 200>
</applet>
*/
public class GridLayoutDemo extends Applet
{
	public void init()
	{
		setLayout(new GridLayout(4, 4));

		for(int i = 1; i <= 16; i ++)
		{
			add(new Button("" + i));
		}
	}
}


/*
For executing Applet programs, type this in CMD:-
javac GridLayoutDemo.java
appletviewer GridLayoutDemo.java
*/


Q4. Develop a program to display the given string in red color, Times New Roman font with italic style and font size of 14.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
4. Develop a program to display the given string in red color, Times New Roman font with italic style and font size of 14.
==================================================================================================================================*/

import java.applet.*;
import  java.awt.*;
/*
<applet code = "FontDemo" width = 200 height = 100>
</applet>
*/
public class FontDemo extends Applet
{
	public void init()
	{
		Font f = new Font("Times New Roman", Font.ITALIC, 14);
		setFont(f);
		setForeground(Color.RED);
	}
	public void paint(Graphics g)
	{
		g.drawString("Java Programming !!!", 20, 20);
	}
}

/*
For executing Applet programs, type this in CMD:-
javac FontDemo.java
appletviewer FontDemo.java
*/


Q5. Develop a program which will increase the font size of a given string by 2 point with every mouse click (Max . 10 clicks).

Note: There would be a constant flickering in the text.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
5. Develop a program which will increase the font size of a given string by 2 point with every mouse click (Max . 10 clicks).
==================================================================================================================================*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code = "IncreaseSizeDemo" width = 250 height = 150>
</applet>
*/
public class IncreaseSizeDemo extends Applet implements MouseListener
{
	String msg = "Java Programming";
	int size = 8;
	Font f;
	public void init()
	{
		addMouseListener(this);
	}
	public void mouseClicked(MouseEvent me)
	{
		if(size <= 28)
		{
			size = size + 2;
			repaint();
		}
	}
	public void mouseEntered(MouseEvent me)
	{
	}
	public void mouseExited(MouseEvent me)
	{
	}
	public void mousePressed(MouseEvent me)
	{
	}
	public void mouseReleased(MouseEvent me)
	{
	}
	public void paint(Graphics g)
	{
		f = new Font("Times New Roman", Font.PLAIN, size);
		setFont(f);
		g.drawString(msg, 10, 60);
	}
}


/*
For executing Applet programs, type this in CMD:-
javac IncreaseSizeDemo.java
appletviewer IncreaseSizeDemo.java
*/


Q6. Develop a program to set the background color of Applet as Yellow.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
6. Develop a program to set the background color of Applet as Yellow.
==================================================================================================================================*/
import java.applet.*;
import  java.awt.*;
/*
<applet code = "BgDemo" width = 300 height = 300>
</applet>
*/
public class BgDemo extends Applet
{
	public void init()
	{
		setBackground(Color.YELLOW);
	}
}


/*
For executing Applet programs, type this in CMD:-
javac BgDemo.java
appletviewer BgDemo.java
*/


Q7. Develop a program to create 3 menus "Format", "Tool" and "Help" and add submenus under these items. Now uploaded!!!:-)

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
7. Develop a program to create 3 menus "Format", "Tool" and "Help" and add submenus under these items.
==================================================================================================================================*/
import java.awt.*;
import java.awt.event.*;

public class MenuDemo extends Frame
{
	String msg = "";
	TextField tf; 
	public MenuDemo(String msg)
	{
		super(msg);
		setSize(300, 200);
		setVisible(true);

		MenuBar mbar = new MenuBar();
		setMenuBar(mbar);

		Menu formatMenu = new Menu("Format");
		Menu toolMenu = new Menu("Tool");
		Menu helpMenu = new Menu("Help");
		
		MenuItem font = new MenuItem("Font");
		MenuItem options = new MenuItem("Options");
		MenuItem about = new MenuItem("About");
		MenuItem help = new MenuItem("Help");
		
		formatMenu.add(font);
		toolMenu.add(options);
		helpMenu.add(about);
		helpMenu.add(help);
		

		mbar.add(formatMenu);
		mbar.add(toolMenu);
		mbar.add(helpMenu);
		

	}


	public static void main(String args[])
	{
		MenuDemo e = new MenuDemo("Menu Demo");
	}
}


/*
For executing Frame programs, type this in CMD:-
javac MenuDemo.java
java MenuDemo.java
*/


Q8. Develop a program to create an applet to accept a number in a text field and display the square of the number when a button with caption Square is pressed.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
8. Develop a program to create an applet to accept a number in a text field and display the square of the number when a button with caption Square is pressed.
==================================================================================================================================*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code = "SquareDemo" width = 200 height = 100>
</applet>
*/
public class SquareDemo extends Applet implements ActionListener
{
	Label l1, l2;
	TextField t1, t2;
	Button b1, b2;
	public void init()
	{
		l1 = new Label("Enter A Number : ");
		l2 = new Label("Square : ");
		t1 = new TextField(10);
		t2 = new TextField(10);
		b1 = new Button("Square");
		b2 = new Button("Clear");
		b1.addActionListener(this);
		b2.addActionListener(this);
		setLayout(new GridLayout(3, 2));
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(b1);
		add(b2);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == b1)
		{
			int num = Integer.parseInt(t1.getText());
			int sqr = num * num;
			t2.setText(Integer.toString(sqr));
		}

		if(ae.getSource() == b2)
		{
			t1.setText("");
			t2.setText("");
		}
	}
}


/*
For executing Applet programs, type this in CMD:-
javac SquareDemo.java
appletviewer SquareDemo.java
*/


Q9. Develop a program to create an applet to find the factorial of the number entered in text field 1 when a button captioned ‘find factorial’ is pressed.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
9. Develop a program to create an applet to find the factorial of the number entered in text field 1 when a button captioned ‘find factorial’ is pressed.
==================================================================================================================================*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code = "FactorialDemo" width = 200 height = 100>
</applet>
*/
public class FactorialDemo extends Applet implements ActionListener
{
	Label l1, l2;
	TextField t1, t2;
	Button b1, b2;
	public void init()
	{
		l1 = new Label("Enter A Number : ");
		l2 = new Label("Factorial : ");
		t1 = new TextField(10);
		t2 = new TextField(10);
		b1 = new Button("Factorial");
		b2 = new Button("Clear");
		b1.addActionListener(this);
		b2.addActionListener(this);
		setLayout(new GridLayout(3, 2));
		add(l1);
		add(t1);
		add(l2);
		add(t2);
		add(b1);
		add(b2);
	}
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == b1)
		{
			int num = Integer.parseInt(t1.getText());
			int fact = 1;
			for(int i = 1; i <= num; i ++)
			{
				fact = fact * i;
			}
			t2.setText(Integer.toString(fact));
		}

		if(ae.getSource() == b2)
		{
			t1.setText("");
			t2.setText("");
		}
	}
}



/*
For executing Applet programs, type this in CMD:-
javac FactorialDemo.java
appletviewer FactorialDemo.java
*/


Q10. Develop a program to print protocol, port, host, file of http://www.msbte.com

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
10. Develop a program to print protocol, port, host, file of http://www.msbte.com
==================================================================================================================================*/
import java.net.*;

class URLDemo
{
	public static void main(String args[]) throws MalformedURLException, java.io.IOException
	{
		URL hp = new URL("http://www.msbte.com/mainsite");

		System.out.println("Protocol: " + hp.getProtocol());
		System.out.println("Port: " + hp.getPort());
		System.out.println("Host: " + hp.getHost());
		System.out.println("File: " + hp.getFile());
		System.out.println("Ext: " + hp.toExternalForm());

		URLConnection uc = hp.openConnection();
		System.out.println(uc);
	}
}



Database related questions, Develop a program:-
Q11. to establish connection to a given Database.
Q12. to display whole content of a Database.
Q13. to change value of any field in Database.

These are too complicated to explain here as it involves creation of database. But luckily I found a word file created by one of our seniors, that files explains all these things, you can download it from here:-

The Word File that answers all of the above questions:-
JDBC.doc

The PowerPoint Presentation which explains how to create a Database:-
JDBC.ppt
All credits for these files goes to unknown person from TYCO batch of 2010-11!!!




Q14. Develop a program to design a frame having labels as “Name”, “Password”, and their corresponding text fields and two buttons named as “login” and “cancel” using swing components.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
14. Develop a program to design a frame having labels as “Name”, “Password”, and their corresponding text fields and two buttons named as “login” and “cancel” using swing components.
==================================================================================================================================*/
import java.awt.*;
import javax.swing.*;
public class SwingLabelButton extends JFrame
{
	public SwingLabelButton(String title)
	{
		super(title);
		Container cp = getContentPane();
		setLayout(new GridLayout(3, 2));

		JLabel l1, l2;
		JTextField t1, t2;
		JButton b1, b2;
		l1 = new JLabel("Name");
		l2= new JLabel("Password");
		t1 = new JTextField(20);
		t2= new JTextField(20);
		b1 = new JButton("Login");
		b2 = new JButton("Cancel");

		cp.add(l1);
		cp.add(t1);
		cp.add(l2);
		cp.add(t2);
		cp.add(b1);
		cp.add(b2);
	}
	public static void main(String args[])
	{
		SwingLabelButton f = new SwingLabelButton("Login Form");
		f.setSize(300, 100);
		f.setVisible(true);
	}
}


/*
For executing Frame programs like the one above, type this in CMD:-
javac SwingLabelButton.java
java SwingLabelButton
*/


Q15. Develop a program in swing using the table component.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
15. Develop a program in swing using the table component.
==================================================================================================================================*/
import java.awt.*;
import javax.swing.*;
/*
<applet code = "JTableDemo" width = 350 height = 100>
</applet>
*/
public class JTableDemo extends JApplet
{
	public void init()
	{
		String colHead[] = {"Event Source", "Event Type", "Event Listener"};
		Object data[][] = {
				{"Button", "ActionEvent", "ActionListener"},
				{"ScrollBar", "AdjustmentEvent", "AdjustmentListener"},
				{"Choice", "ItemEvent", "ItemListener"},
				{"TextField", "TextEvent", "TextListener"},
				{"MenuItem", "ItemEvent", "ItemListener"},
				{"List", "ActionEvent", "ActionListener"},
				{"Checkbox", "ItemEvent", "ItemListener"}
			             };

		JTable table = new JTable(data, colHead);
		
		int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
		int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

		JScrollPane jsp = new JScrollPane(table, v, h);

		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		contentPane.add(jsp, BorderLayout.CENTER);
	}
}



/*
For executing Applet programs, type this in CMD:-
javac JTableDemo.java
appletviewer JTableDemo.java
*/


Q16. Develop a program in swing showing use of Tabbed Pane.

/*================================================================================================================================
Roll No:- 12
Class:- TYCO - Ist Shift
16. Develop a program in swing showing use of Tabbed Pane.
==================================================================================================================================*/
import java.awt.*;
import javax.swing.*;
/*
<applet code = "JTabbedPaneDemo" width = 350 height = 200>
</applet>
*/
public class JTabbedPaneDemo extends JApplet
{
	public void init()
	{
		JTabbedPane jtp = new JTabbedPane();
		jtp.addTab("Branch", new ButtonPanel());
		jtp.addTab("JAVA Events", new TablePanel());

		Container contentPane = getContentPane();
		contentPane.add(jtp);
	}
}

class ButtonPanel extends JPanel
{
	public ButtonPanel()
	{
		JButton b1 = new JButton("Computer");
		JButton b2 = new JButton("Information Tech");
		JButton b3 = new JButton("Mechanical");
		JButton b4 = new JButton("Civil");
		JButton b5 = new JButton("E&TC");

		add(b1);
		add(b2);
		add(b3);
		add(b4);
		add(b5);
	}
}

class TablePanel extends JPanel
{
	public TablePanel()
	{
		String colHead[] = {"Event Source", "Event Type", "Event Listener"};
		Object data[][] = {
				{"Button", "ActionEvent", "ActionListener"},
				{"ScrollBar", "AdjustmentEvent", "AdjustmentListener"},
				{"Choice", "ItemEvent", "ItemListener"},
				{"TextField", "TextEvent", "TextListener"}
			             };

		JTable table = new JTable(data, colHead);
		
		int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
		int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

		JScrollPane jsp = new JScrollPane(table, v, h);

		setLayout(new BorderLayout());
		add(jsp, BorderLayout.CENTER);
	}
}


/*
For executing Applet programs, type this in CMD:-
javac JTabbedPaneDemo.java
appletviewer JTabbedPaneDemo.java
*/


Database Program Example

import java.sql.*;
import java.io.*;

public class StudentTable
{
	public static void main(String args[])
	{
	try
	{
		String sql;
		ResultSet results;
		//a) Connection To A Database
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		System.out.println("Driver Loaded");

		String URL = "jdbc:odbc:Student";
		Connection con = DriverManager.getConnection(URL);
		System.out.println("Connection to database created.");

		Statement state = con.createStatement();
		System.out.println("Statement Object Created.");

		//b) Insert a Record
		sql = "insert into StudTable values(6, 'XYZ')";
		state.executeUpdate(sql);
		System.out.println("Entry Inserted.");

		//c) Change name as John where Roll No. is 2
		sql = "update StudTable set StudentName = 'John' where Roll = 2";
		state.executeUpdate(sql);
		System.out.println("Entry Updated.");

		//d) Delete Current Record
		sql = "delete * from StudTable where Roll = 4";
		state.executeUpdate(sql);
		System.out.println("Entry Deleted.");

		//e) Display whole contents of Database.
		sql = "select Roll, StudentName from StudTable";
		results = state.executeQuery(sql);
		String text = "" ;
		while(results.next())
		{
			text += results.getInt(1) + "\t" + results.getString(2) + "\n";
		}
		System.out.println("\nROLL\tNAME");
		System.out.println(text);

		results.close();
		state.close();
		con.close();
	}
	catch(Exception e)
	{
		System.out.println(e);
	}
	}
}

2nd Example:-

import java.sql.*;

public class EmployeeTableUpdate
{
	public static void main(String args[])
	{
	try
	{
		String sql;
		ResultSet results;
		PreparedStatement state;
		String text;

		//a) Establish Connection To Database.
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		System.out.println("Driver Loaded");

		String URL = "jdbc:odbc:EmployeeDatabase";
		Connection con = DriverManager.getConnection(URL);
		System.out.println("Connection to database created.");

		//b) Insert Record Using PreparedStatement
		sql = "insert into Employee values(?, ?)";
		state = con.prepareStatement(sql);
		state.setString(1, "XYZ");
		state.setInt(2, 21);
		state.executeUpdate();
		System.out.println("\nRecord Inserted.");

		//c)  Select All Entries Related to "Ramesh"
		sql = "select * from Employee where Name = ?";
		state = con.prepareStatement(sql);
		state.setString(1, "Ramesh");
		results = state.executeQuery();
		text = "" ;
		while(results.next())
		{
			text += results.getString(1) + "\t" + results.getInt(2) + "\n";
		}
		System.out.println("\nEntries Related To Ramesh.");
		System.out.println("NAME\tAGE");
		System.out.println(text);

		//d) To delete the record of Database
		sql = "delete * from Employee where Name = ?";
		state = con.prepareStatement(sql);
		state.setString(1, "ABC");
		state.executeUpdate();
		System.out.println("\nEntry Deleted.");

		System.out.println("\nContents Of Database :");
		sql = "select * from Employee";
		state = con.prepareStatement(sql);
		results = state.executeQuery();
		text = "" ;
		while(results.next())
		{
			text += results.getString(1) + "\t" + results.getInt(2) + "\n";
		}
		System.out.println("NAME\tAGE");
		System.out.println(text);

		results.close();
		state.close();
		con.close();
	}
	catch(Exception e)
	{
		System.out.println(e);
	}
	}
}




View MyStats