Vous êtes sur la page 1sur 18

SOLVED Advanced Java Practical Skill Test 1) Develop a program to add two labels and a single button to an applet.

import java.awt.*; import java.applet.*; /* <applet code = "LabelButtonDemo" width = 300 height = 100> </applet> */ public class LabelButtonDemo extends Applet { Label l1, l2; Button b1; public void init() { l1 = new Label("Label 1"); l2 = new Label("Label 2"); b1 = new Button("Button 1"); add(l1); add(l2); add(b1); } } 2) Develop a program to add a label, a text field and a checkbox to an applet. import java.awt.*; import java.applet.*; /* <applet code = "LabelTextCheck" width = 300 height = 100> </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); } }

3) Develop a program to add text area and button to a STANDALONE application. import java.awt.*; public class TextAreaButton extends Frame { TextArea a1;

Button b1; public TextAreaButton(String msg) { super(msg); a1 = new TextArea("Text Area"); b1 = new Button("Button"); setLayout(new GridLayout(2, 1)); add(a1); add(b1); } public static void main(String args[]) { TextAreaButton tab = new TextAreaButton("TextArea Button"); tab.setSize(200, 200); tab.setVisible(true); } } --Run the program using java and to close the application, DIRECTLY CLOSE THE COMM AND PROMPT. There is no other option. 4) Develop a program to illustrate the use of choice list and scrolling list in applet. import java.awt.*; import java.applet.*; /* <applet code = "ScrollChoice" width = 500 height = 100> </applet> */ public class ScrollChoice extends Applet { Choice c; List l; public void init() { c = new Choice(); l = new List(); c.add("Select Color"); c.add("Red"); c.add("Blue"); c.add("Green"); l.add("Select City"); l.add("Mumbai"); l.add("Pune"); l.add("Kolhapur"); add(c); add(l); } }

5) Develop a program to illustrate the use of scrollbars, sliders and a canvas. import java.awt.*; import javax.swing.*; public class CanvasFrame extends JFrame { public CanvasFrame(String msg) { super(msg); setLayout(new GridLayout(3, 1)); CanvasDemo cd = new CanvasDemo(); JScrollBar sb = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 1 00); JSlider js = new JSlider(SwingConstants.VERTICAL, 0, 100, 0); add(cd); add(sb); add(js); } public static void main(String args[]) { CanvasFrame cf = new CanvasFrame("Canvas Example"); cf.setSize(300, 150); cf.setVisible(true); } } class CanvasDemo extends Canvas { public void paint(Graphics g) { g.drawString("This Is A Canvas", 10, 20); } }

6) 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 add(new add(new add(new Button("Header Portion"), BorderLayout.NORTH); Button("Footer Portion"), BorderLayout.SOUTH); Button("Right"), BorderLayout.EAST); Button("Left"), BorderLayout.WEST);

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

} } 7) Develop a program which makes the use of Flow Layout and Grid Bag Layout mana ger. 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 c2 c3 c4 = = = = new new new new Checkbox("Check Checkbox("Check Checkbox("Check Checkbox("Check Box Box Box Box 1"); 2"); 3"); 4");

add(c1); add(c2); add(c3); add(c4); } } 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)); } } } 8) Develop a program to display the given string in red color, Times New Roman f ont 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); } } 9) Develop a program which will increase the font size of a given string by 2 po int with every mouse click (Max . 10 clicks). import java.awt.*; import java.applet.*; import java.awt.event.*; /* <applet code = "IncreaseSize" width = 250 height = 150> </applet> */ public class IncreaseSize 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); } } The output will have some flickering effect. 10) Develop a program to set the background color of the applet as Yellow. import java.applet.*; import java.awt.*; /* <applet code = "YellowBackground" width = 300 height = 300> </applet> */ public class YellowBackground extends Applet { public void init() { setBackground(Color.YELLOW); } } 11) Develop a program which will create a checkable Menu items- open file and cut menu item under the Menu Edit . import java.awt.*; public class CheckableMenuDemo extends Frame { public CheckableMenuDemo(String msg) { super(msg); setSize(300, 100); setVisible(true); MenuBar mbar = new MenuBar(); setMenuBar(mbar); Menu fileMenu = new Menu("File"); CheckboxMenuItem open = new CheckboxMenuItem("Open"); fileMenu.add(open); mbar.add(fileMenu); Menu editMenu = new Menu("Edit"); CheckboxMenuItem cut = new CheckboxMenuItem("Cut"); editMenu.add(cut); mbar.add(editMenu); } public static void main(String args[]) { CheckableMenuDemo e = new CheckableMenuDemo("Checkable Menu Demo "); } } 12) Develop a program to create a menu File with three Menu items Open , Close and under the menu

Whenever a menu item is clicked, display the same in text field. import java.awt.*; import java.awt.event.*; public class MenuDemo extends Frame implements ActionListener { String msg = ""; TextField tf; public MenuDemo(String msg) { super(msg); setSize(300, 200); setVisible(true); MenuBar mbar = new MenuBar(); setMenuBar(mbar); Menu fileMenu = new Menu("File"); MenuItem open = new MenuItem("Open"); MenuItem close = new MenuItem("Close"); MenuItem save = new MenuItem("Save"); fileMenu.add(open); fileMenu.add(close); fileMenu.add(save); mbar.add(fileMenu); tf = new TextField(); add(tf); fileMenu.addActionListener(this); } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand() == "Open") msg = "Selected : Open"; else if(ae.getActionCommand() == "Close") msg = "Selected : Close"; else if(ae.getActionCommand() == "Save") msg = "Selected : Save"; repaint(); } public void paint(Graphics g) { tf.setText(msg); } public static void main(String args[]) { MenuDemo e = new MenuDemo("Menu Demo"); } } To close the application, DIRECTLY CLOSE THE COMMAND PROMPT. There is no other option.

13) 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 = "SquareApplet" width = 200 height = 100> </applet> */ public class SquareApplet 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(""); } } } 14) Develop a program to create an applet to find the factorial of the number en tered in text field 1 when a button captioned find factorial is pressed. import java.awt.*; import java.applet.*; import java.awt.event.*; /* <applet code = "FactorialApplet" width = 200 height = 100> </applet>

*/ public class FactorialApplet 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(""); } } } 15) To develop a program to extend a list. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code = "ListDemo2" width = 300 height = 100> </applet> */ public class ListDemo2 extends Applet { MyList list; public void init()

{ list = new MyList(); list.add("Red"); list.add("Green"); list.add("Blue"); add(list); } class MyList extends List { public MyList() { enableEvents(AWTEvent.ITEM_EVENT_MASK EVENT_MASK); }

AWTEvent.ACTION_

protected void processActionEvent(ActionEvent ae) { showStatus("Action event: " + ae.getActionCommand()); super.processActionEvent(ae); } protected void processItemEvent(ItemEvent ie) { showStatus("Item event: " + getSelectedItem()); super.processItemEvent(ie); } } } 16) Develop a program to print protocol, port, host, file of http://www.msbte.co m import java.net.*; class URLDemo { public static void main(String args[]) throws MalformedURLException, jav a.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); } } 17) Develop a program to send user name to the server and server will send username> to the client using UDP. //This program is for the Server. hello<

import java.io.*; import java.net.*; public class Server { public static void main(String args[]) throws Exception { int clientport = 999; int serverport = 998; int buffer = 1024; DatagramSocket ds = new DatagramSocket(serverport); byte NameByte[] = new byte[buffer]; DatagramPacket p = new DatagramPacket(NameByte, NameByte.length) ; ds.receive(p); String receive = new String(p.getData(), 0, p.getLength()); System.out.println("From Client : " + receive); String send = "Hello " + receive; NameByte = send.getBytes(); DatagramPacket p2 = new DatagramPacket(NameByte, NameByte.length , InetAddress.getLocalHost(), clientport); ds.send(p2); System.out.println("Sending To The Client : " + send); ds.close(); } } //This program is for the Client. import java.io.*; import java.net.*; public class Client { public static void main(String args[]) throws Exception { int clientport = 999; int serverport = 998; int buffer = 1024; DatagramSocket ds = new DatagramSocket(clientport); BufferedReader br = new BufferedReader(new InputStreamReader(Sys tem.in)); System.out.print("Enter Name : "); String Name = br.readLine(); byte NameByte[] = new byte[buffer]; NameByte = Name.getBytes(); DatagramPacket p = new DatagramPacket(NameByte, NameByte.length, InetAddress.getLocalHost(), serverport); ds.send(p); System.out.println("Name Sent To Server."); byte NameByte2[] = new byte[buffer];

DatagramPacket p2 = new DatagramPacket(NameByte2, NameByte2.leng th); ds.receive(p2); String receive = new String(p2.getData(), 0, p2.getLength()); System.out.println("From Server : " + receive); ds.close(); } } First execute the Server file. It will hang. Then execute the Client file. When you write the name and press ENTER, the name will appear in the Server window. 18) To develop a program in JDBC to perform following task 1. To establish a connection to a given database. 2. To insert a record into the given database table name stud table . 3. To change the name of student as John whose roll no. is 2 in the table s tud table. 4. To delete current record. 5. Display the whole contents of database. //This program is little complicated. If you get easier solution, please refer t hat. A) Creating a Database(For More Information, please refer the presentation ) B) Creating A Connection To Database : 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); } } }

19) To develop a program in JDBC to perform following task 1. To establish a connection to a given database. 2. To insert a employee record into the database employee by using prepared statement 3. To select all the entries related to employee Ramesh from the database. 4. To delete the record of the database. //This program is completely based on PreparedStatement. If you get easy solutio n, please refer that. 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); } } } 20) Develop a program to design a frame having labels as Name , Password , and their c

orresponding text fields and two buttons named as onents. import java.awt.*; import javax.swing.*; public class SwingLabelButton extends JFrame { public SwingLabelButton(String title) { super(title); 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"); add(l1); add(t1); add(l2); add(t2); add(b1); add(b2);

login

and

cancel using swing comp

} public static void main(String args[]) { SwingLabelButton f = new SwingLabelButton("Login Form"); f.setSize(300, 100); f.setVisible(true); } } To close the Frame, directly close the Command Prompt. 21) Develop a frame having title as changing color with a provision to select a pa rticular among Red, green, blue. Make use of radio buttons. import java.awt.*; import javax.swing.*; public class ChangingColor extends JFrame { public ChangingColor(String title) { super(title); setLayout(new GridLayout(3, 1)); JRadioButton rb1 = new JRadioButton("Red"); JRadioButton rb2 = new JRadioButton("Green", true); JRadioButton rb3 = new JRadioButton("Blue"); ButtonGroup bg = new ButtonGroup(); bg.add(rb1); bg.add(rb2);

bg.add(rb3); add(rb1); add(rb2); add(rb3); } public static void main(String args[]) { ChangingColor f = new ChangingColor("Changing Color"); f.setSize(300, 100); f.setVisible(true); } } To close the frame, directly close the Command Prompt. 22) To develop a program having a tabbed panes with button and table component a dded to it. 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 b2 = new JButton b3 = new JButton b4 = new JButton b5 = new add(b1); add(b2); add(b3); add(b4); add(b5); } } class TablePanel extends JPanel {

JButton("Computer"); JButton("Information Tech"); JButton("Mechanical"); JButton("Civil"); JButton("ENTC");

public TablePanel() { String colHead[] = {"Event Source", "Event Type", "Event Listene r"}; Object data[][] = { {"Button", "ActionEvent", "ActionListener"}, {"ScrollBar", "AdjustmentEvent", "AdjustmentList ener"}, {"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); } } 23) 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 Listene r"}; Object data[][] = { {"Button", "ActionEvent", "ActionListener"}, {"ScrollBar", "AdjustmentEvent", "AdjustmentList ener"}, {"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);

} }