0% found this document useful (0 votes)
151 views26 pages

Silo - Tips - Summer 13 Examination Model Answer Subject Name Advanced Java Programming

1) The document provides instructions for examiners evaluating answers to questions on an Advanced Java Programming exam. It emphasizes understanding over exact word-for-word responses and allows for variation in figures. 2) Question 1 asks examinees to explain the use of frames in AWT with an example, list differences between TCP and UDP protocols, and provide the uses and steps for a JTabbedPane control. 3) Question 1b asks examinees to describe the use of the URL class and provide a servlet program to accept and display a password from a client.

Uploaded by

darte1307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views26 pages

Silo - Tips - Summer 13 Examination Model Answer Subject Name Advanced Java Programming

1) The document provides instructions for examiners evaluating answers to questions on an Advanced Java Programming exam. It emphasizes understanding over exact word-for-word responses and allows for variation in figures. 2) Question 1 asks examinees to explain the use of frames in AWT with an example, list differences between TCP and UDP protocols, and provide the uses and steps for a JTabbedPane control. 3) Question 1b asks examinees to describe the use of the URL class and provide a servlet program to accept and display a password from a client.

Uploaded by

darte1307
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

Important Instructions to examiners:


1) The answers should be examined by key words and not as word-to-word as given in the
model answer scheme.
2) The model answer and the answer written by candidate may vary but the examiner may try
to assess the understanding level of the candidate.
3) The language errors such as grammatical, spelling errors should not be given more
Importance (Not applicable for subject English and Communication Skills).
4) While assessing figures, examiner may give credit for principl components indicated in the
figure. The figures drawn by candidate and model answer may vary. The examiner may give
credit for any equivalent figure drawn.
5) Credits may be given step wise for numerical problems. In some cases, the assumed constant
values may vary and there may be some difference in the candidate’s answers and model answer.
6) In case of some questions credit may be given by judgement on part of examiner of relevant
answer based on candidate’s understanding.
7) For programming language papers, credit may be given to any other program based on equivalent
concept.

Q.1.a) Attempt any THREE of the following: MARKS 12


i) Explain the use of frame in AWT with example.
USE: (1 Mark)
Frame is a subclass of Window and has a title bar, menu bar, borders, and resizing corners. We will
use it to create child windows within applets, and top-level or child windows for applications.
Example. (3 Marks - any one )
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="AppletFrame" width=300 height=50>
</applet> */
// Create a subclass of Frame.
class SampleFrame extends Frame
{
SampleFrame(String title)
{
super(title);
}
public void paint(Graphics g)
{

Page 1 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

g.drawString("This is in frame window", 10, 40);


}
}
// Create frame window.
public class AppletFrame extends Applet
{
Frame f;
public void init()
{
f = new SampleFrame("A Frame Window");
f.setSize(250, 250);
f.setVisible(true);
}
public void start()
{
f.setVisible(true);
}
public void stop()
{
f.setVisible(false);
}
public void paint(Graphics g)
{
g.drawString("This is in applet window", 10, 20);
}
}

Page 2 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

ii) List difference between TCP and UDP


(1- Mark each point, any four points)

TCP UDP

It is connection Oriented It is connectionless

It gives acknowledgement It does not give acknowledgement

It is reliable It is unreliable

It is use to transfer for large amont of data It is use to transfer for small amont of data

Transmission seed is low Transmission seed is High

TCP header size is 20 bytes UDP Header size is 8 bytes

UDP does error checking, but no recovery


TCP does error checking options

iii) Write the use of


1) Drive Manager:
(Explanation-2 Marks)
Driver Manager is a static class in the Java Software Development Kit (J2SDK). Driver Manager
manages the set of Java Database Connectivity (JDBC) drivers that are available for an application
to use.
Applications can use multiple JDBC drivers concurrently if necessary. Each application specifies a
JDBC driver by using a Uniform Resource Locator (URL). By passing a URL for a specific JDBC
driver to the Driver Manager, the application informs the Driver Manager about which type of
JDBC connection should be returned to the application.
Before this can be done, the Driver Manager must be made aware of the available JDBC drivers so
it can hand out connections. By making a call to the Class.forName method, it loads a class into
the running Java virtual machine (JVM) based on its string name that is passed into the method.

Page 3 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

2) Connection:
(Explanation-2 Marks)
Connection is a interface. It allows to establish connection between data source and application i.e
program written by the user. Connection interface helps by providing the different methods for
transactions and error handling. Different approaches can be used to establish a connection with
the database.

1. DriverManager.getConnection (string url)


2. DriverManager.getConnection (string url, String user,String url)
3. Connection con= DriverManager.getConnection (string url)

iv) Give sequential steps to use JTabbed Pane Control


STEPS: (Correct steps 4 Marks)
1. Create a JTabbedPane object using following constructor.
JTabbedPane( )
2. Call addTab( ) to add a tab to the pane.
(The arguments to this method define the title of the tab and the component it contains.)
3. Repeat step 2 for each tab.
4. Add the tabbed pane to the content pane of the applet.

b) Attempt any ONE of the following: MARKS 6


i) Give the use of URL class along with syntax of constructor of URL class.
(Use - 2 Marks, Constructor - 1 Mark each)
The URL provides a reasonably intelligible form to uniquely identify or address information on the
Internet. URLs are ubiquitous; every browser uses them to identify information on the Web. In fact,
the Web is really just that same old Internet with all of its resources addressed as URLs plus HTML
Java's URL class has several constructors. One commonly used form specifies the URL with a string
that is identical to what you see displayed in a browser:
1. URL(String urlSpecifier)
The next two forms of the constructor allow you to break up the URL into its component parts:
2. URL(String protocolName, String hostName, int port, String path)

Page 4 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

3. URL(String protocolName, String hostName, String path)


Another frequently used constructor allows you to use an existing URL as a reference context and then
create a new URL from that context. Although this sounds a little contorted, it's really quite easy and
useful.
4. URL(URL urlObj, String urlSpecifier)

ii) Write a servlet to store password accepted from client and display it.
(HTML code - 2 Marks, Servlet Program - 4 Marks)
HTML program:
<html>
<body>
<center>
<form name="Form1" method="post"
action="http://localhost:8080/examples/servlet/PostParametersServlet">
<B>Password</B>
<input type=textbox name="pass" size="25" value="">
<input type=submit value="Submit">
</body>
</html>

Servlet Program
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class PostParametersServlet extends GenericServlet
{
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException
{
PrintWriter pw = response.getWriter();
String pvalue = request.getParameter(pass);

Page 5 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

pw.println(“Password is :” + pvalue);
}
pw.close();
}

Q.2. Attempt any TWO of the following: MARKS 16


a) Write a program to change the size of a font by 2 points when mouse button is clicked.
( Correct logical steps - 6 Marks, syntax - 2 Marks)
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 = 10;
Font f;
public void init()
{
addMouseListener(this);
}
public void mouseClicked(MouseEvent me)
{
size = size + 2;
repaint();
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)

Page 6 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

{
}
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);
}
}

b) Write a program to demonstrate the use of Border layout which shows four buttons at four
sides of an applet with caption as East, West, North and South.
(Ccorrect logical steps - 6 Marks, syntax - 2 Marks)

// Demonstrate BorderLayout.
import java.awt.*;
import java.applet.*;
import java.util.*;
/* <applet code="BorderLayoutDemo" width=400 height=200>
</applet> */
public class BorderLayoutDemo extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);

Page 7 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"), BorderLayout.EAST);
add(new Button("WEST"), BorderLayout.WEST);
}
}

c) Define and state function of following terms


i) Web Server
(Explanation-2 Marks)
Web Server: The primary function of a web server is to deliver web pages on the request of
clients using the Hypertext Transfer Protocol (HTTP). This means delivery of HTML documents
and any additional content that may be included by a document, such as images, style sheets and
scripts.
Web server has IP address & domain name.
e.g. http://www.msbte.com/index.html

ii) Protocol
(Explanation-2 Marks)
Protocol: it is set of rules that governance the communication between computers on a network.
Thease rule include guidelines that regulate the feture of a network like access method, allow
physical topologies, types of cabling and speed of data transfer etc.
Protocols like TCP, UDP, HTTP, IP

iii) HTTP
(Explanation-2 Marks)
HTTP: The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed,
collaborative, hypermedia information systems. HTTP functions as a request-response protocol in
the client-server computing model. A web browser, for example, may be the client and an
application running on a computer hosting a web site may be the server. The client submits an
HTTP request message to the server. The server, which provides resources such as HTML files

Page 8 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

and other content, or performs other functions on behalf of the client, returns a response message
to the client. The response contains completion status information about the request and may also
contain requested content in its message body.

iv) Proxy server


(Explanation-2 Marks)
Proxy Servers: A proxy server speaks the client side of a protocol to another server. This is often
required when clients have certain restrictions on which servers they can connect to. Thus, a client
would connect to a proxy server, which did not have such restrictions, and the proxy server would
in turn communicate for the client. A proxy server has the additional ability to filter certain
requests or cache the results of those requests for future use. A caching proxy HTTP server can
help reduce the bandwidth demands on a local network's connection to the Internet. When a
popular web site is being hit by hundreds of users, a proxy server can get the contents of the web
server's popular pages once, saving expensive internetwork transfers while providing faster access
to those pages to the clients.

Q.3. Attempt any FOUR of the following: MARKS 16


a) List any four methods of checkbox class with syntax.
(1 Mark each method, any four method)
Methods of Checkbox class.
1. public boolean getState()
This determines if the checkbox is checked (true) or unchecked (false).
2. public void setState(boolean checkedState)
This method sets the checkbox to be checked (true) or unchecked (false).
3. public String getLabel()
This retrieves the current label.
4. public void setLabel(String newLabel)
The setLabel method changes the label. As with buttons, if the checkbox is already displayed, doing
this does not automatically resize it in its Container. So the containing window should be invalidated
and validated to force a new layout, as illustrated here:

Page 9 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

b) What are dialog boxes? Give the types of dialog boxes.


(Dialog Box - 2 Marks, Types - 2 Marks)
A dialog box is a secondary window in which users perform a task that is supplemental to the task in
the primary window. For example, a dialog box might enable users to set preferences or choose a file
from the hard disk. A dialog box can contain panes and panels, text, graphics, controls (such as
checkboxes, radio buttons, or sliders), and one or more command buttons. Dialog boxes use the native
window frame of the platform on which they are running (in both non-MDI and MDI applications).
There are two types of Dialog boxes modal or modeless.
i. A modal dialog box prevents users from interacting with the application until the dialog box is
dismissed. However, users can move a modal dialog box and interact with other applications while
the modal dialog box is open. This behavior is sometimes called "application-modal."
ii. A modeless dialog box does not prevent users from interacting with the application they are in
or with any other application. Users can go back and forth between a modeless dialog box and
other application windows.

c) Explain necessity of JDBC-ODBC bridge driver.


(Explanation-4 Marks)
i. The JDBC type1 driver, also known as the JDBC-ODBC bridge, is a database driver
implementation that employs the ODBC driver to connect to the database.
ii. The driver converts JDBC method calls into ODBC function calls.
iii. The driver is platform-dependent as it makes use of ODBC which in turn depends on native
libraries of the underlying operating system the JVM is running upon.
iv. Also, use of this driver leads to other installation dependencies; for example, ODBC must be
installed on the computer having the driver and the database must support an ODBC driver.
v. The use of this driver is discouraged if the alternative of a pure-Java driver is available.
vi. The other implication is that any application using a type 1 driver is non-portable given the
binding between the driver and platform.
vii. This technology isn't suitable for a high-transaction environment. Type 1 drivers also don't
support the complete Java command set and are limited by the functionality of the ODBC
driver.

Page 10 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

d) Explain the use of prepared statement interface with example.


(Explain - 2 Marks, Example - 2 Marks any one)
PrepareStatement ()
The preparedStatement() method creates a PreparedStatement object and returns it as a return value.
The Prepared statement object is used to execute dynamic SQL statement against the database. A
dynamic SQL statement is a statement in which some of the parameters in the statement are
unknown when the statement is created. The parameter is placed into the SQL statement as they are
determined by the application. When all the parameters are specified for the SQL statement, the
dynamic SQL statement will be executed just as a static statement is executed. To create a dynamic
SQL statement that takes a first name and last name as parameters, use the following code:

Example.
try
{
String sql=”Select * from temployee” + “where Firstname=?” + “and Lastname=?”;
PreparedStatement p=connection.preparedStatement(sql);
}
Catch(SQLException e)
{
}

e) Give the use of following Swing controls


i) Scroll Panes
(Explanation-2 Marks)
The use of JScrollPane is a component that presents a rectangular area in which a component may
be viewed. Horizontal and /or vertical scroll bar may be provided if necessary. Scroll panels are
implemented in Swing by the JScrollPane class.
Constructor: JScrollPane(Component comp)
This creates a scroll pan with the component specified with comp.

Page 11 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

JScrollPane(int vsb, int hsb)


This creates a scroll panel with the .vsb and .hsb and int constants that define when vertical and
horizontal scroll bars for this scroll panel are shown.
JScrollPane(Component comp,int vsb, int hsb)
This constructor scroll panel creates a combination of above both constructors.

ii) Tree
(Explanation-2 Marks)
A tree is a component that presents a hierarchical view of data. A user has the ability to
expand or collapse individual subtrees in this display. Trees are implemented in Swing
by the JTree class, which extends JComponent. Some of its constructors are shown here:
JTree(Hashtable ht)
JTree(Object obj[ ])
JTree(TreeNode tn)
JTree(Vector v)
The first form creates a tree in which each element of the hash table ht is a child node.

Each element of the array obj is a child node in the second form. The tree node tn is the
root of the tree in the third form. Finally, the last form uses the elements of vector v as
child nodes.

Q.4. a) Attempt any THREE of the following: MARKS 12


i) Give the meaning of following terms in windows components.
1. Frame:
(Explanation-2 Marks)
Frame is a standard window, which is subclass of window class from AWT Hierarchy, windows can
be created from the Applet viewer or it can be created using Frame component. The following are two
constructors of Frame:
Frame();
Frame(String title);

Page 12 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

2. Panel:
(Explanation -2 Marks)

The panel class is a subclass of Container. A Panel may be through of as component. Panel is the
superclass for Applet. When screen output is directed to an applet, it is drawn on the surface of a panel

object. In essence, a panel is window that does not contain a title bar, menu bar or border . This is why
you do not see this item when an applet is run inside a browser. Other component can be added to a
panel object by its add () method, (method of container). Once, these component have been added, you
can position and resize them manually using the setLocation (), setSize () or setBounds () methods
define by component class

ii) Give the meaning of following


1. PORT:
(Explanation -2 Marks)
Port is a numbered socket on a particular machine. A server process is said to “listen” to a port till
a client connects to it. A server is permitted to accept multiple clients connected to a the same port
number, although each session is unique. To manage multiple client connections, a server process
must be multithreaded or have some other means of multiplexing the simultaneous input and
output.
2. URL:
(Explanation -2 Marks)
The URL provides a reasonably intelligible form to uniquely identify or address information on
the Internet. URLs are ubiquitous; every browser uses them to identify information on the Web.
In fact, the Web is really just that same old Internet with all of its resources addressed as URLs
plus HTML. Within Java's network class library, the URL class provides a simple, concise API
to access information across the Internet using URLs.

URL Format

http://www.starwave.com/ or

http://www.starwave.com:80/index.html.

Page 13 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

iii) Explain the use of prepared statement with example.


(Explain - 2 Marks, Example - 2 Marks- any one)

PrepareStatement ()
The preparedStatement() method creates a PreparedStatement object and returns it as a return value.
The Prepared statement object is used to execute dynamic SQL statement against the database.A
dynamic SQL statement is a statement in which some of the parameters in the statement are unknown
when the statement is created. The parameter is placed into the SQL statement as they are determined
by the application. When all the parameters are specified for the SQL statement, the dynamic SQL
statement will be executed just as a static statement is executed. To create a dynamic SQL statement
that takes a first name and last name as parameters, use the following code:
try
{
String sql=”Select * from temployee” + “where Firstname=?” + “and Lastname=?”;
PreparedStatement p=connection.preparedStatement(sql);
}
Catch(SQLException e)
{
}

iv) Give steps to display icon on a label and a button using swing.
(Correct logical steps - 4 Marks)
Step 1. Create ImageIcon object using any of following constructor. ImageIcon(String filename)
ImageIcon(URL url) The first form uses the image in the file named filename. The second form uses
the image in the resource identified by url.
Step 2. Create JLabel object using any of following constructor. JLabel(Icon i) JLabel(String s, Icon i,
int align) Here, s and i are the text and icon used for the label. The align argument is either LEFT,
RIGHT, or CENTER. These constants are defined in the SwingConstants interface, along with several
others used by the Swing classes.
Step 3. Create JButton object using any of following constructor. The JButton class provides the
functionality of a push button. JButton allows an icon, a string, or both to be associated with the push

Page 14 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

button. Some of its constructors are shown here: JButton(Icon i) JButton(String s) JButton(String s,
Icon i) Here, s and i are the string and icon used for the button.
Step 4. Add the control JButton & JLabel into swing using add() method. The add( ) method of
Container can be used to add a component to a content pane. Its form is shown here: void add(comp)
Here, comp is the component to be added to the content pane

b) Attempt any ONE of the following: MARKS 6


i) List any three interfaces in Javax. Servlet package. Also give the use of each interface.
( 2 Marks each interface, any three)

The javax.servlet package contains a number of interfaces and classes that establish the framework in
which servlets operate. The following table summarizes the core interfaces that are provided in this
package. The most significant of these is Servlet. All servlets must implement this interface or extend
a class that implements the interface.

1. Servlet : All servlets must implement the Servlet interface. It declares the init( ),
service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. A
method is also provided that allows a servlet to obtain any initialization parameters. It eclares life
cycle methods for a servlet.
2. ServletConfig : The ServletContext interface is implemented by the server. It enables servlets
to obtain information about their environment. Allows servlets to get initialization parameters.
3. ServletContext : The ServletContext interface is implemented by the server. It enables servlets
to obtain information about their environment. Enables servlets to log events and access
information about their environment.
4. ServletRequest : The ServletRequest interface is implemented by the server. It enables a
servlet to obtain information about a client request. Used to read data from a client request.
5. ServletResponse : The ServletResponse interface is implemented by the server. It enables a
servlet to formulate a response for a client. Used to write data to a client response.
6. SingleThreadModel : This interface is used to indicate that only a single thread will execute
the service( ) method of a servlet at a given time. It defines no constants and declares no methods.
If a servlet implements this interface, the server has two options. First, it can create several
instances of the servlet. When a client request arrives, it is sent to an available instance of the

Page 15 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

servlet. Second, it can synchronize access to the servlet. Indicates that the servlet is thread safe.

ii) Write a program to display menus and menubars.


( Correct logical statement - 4 Marks-any one similar program, syntax - 2 Marks)

import java.awt.*;

import java.applet.*;

public class FrameDemo extends Applet

Frame f;

MenuBar mnuBar;

Menu mnuFile,mnuEdit;

MenuItem mnuOpen,mnuNew,mnuSave;

MenuItem mnuCut,mnuCopy,mnuPaste;

public void init()

mnuBar = new MenuBar();

mnuOpen = new MenuItem("Open");

mnuNew = new MenuItem("New");

mnuSave = new MenuItem("Save");

mnuCut = new MenuItem("Cut");

mnuCopy = new MenuItem("Copy");

mnuPaste = new MenuItem("Paste");

mnuFile = new Menu("File");

mnuFile.add(mnuNew);

Page 16 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

mnuFile.add(mnuOpen);

mnuFile.add(mnuSave);

mnuEdit = new Menu("Edit");

mnuEdit.add(mnuCut);

mnuEdit.add(mnuCopy);

mnuEdit.add(mnuPaste);

mnuBar.add(mnuFile);

mnuBar.add(mnuEdit);

f = new Frame("A Frame Window");

f.setMenuBar(mnuBar);

f.setSize(250,250);

f.setVisible(true);

public void paint(Graphics g)

g.drawString("This is in applet window",10,20);

/*<applet code=FrameDemo width=500 height=500>

</applet>*/

Page 17 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

Q.5. Attempt any TWO of the following: MARKS 16


a) Write a program to display a button with an image on it using swing. The image on the button
will change on when the button is clicked.
( Correct logical statement - 6 Marks, syntax - 2 Marks)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

/*<applet code="JButtonDemo" width=250 height=300>

</applet>*/

public class JButtonDemo extends JApplet implements ActionListener

JButton jb1;

public void init()

// Get content pane

Container contentPane = getContentPane();

contentPane.setLayout(new FlowLayout());

// Add buttons to content pane

ImageIcon ii = new ImageIcon("image1.jpg");

jb1 = new JButton(“One”, ii);

jb1.addActionListener(this);

contentPane.add(jb1);

public void actionPerformed(ActionEvent ae)

Page 18 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

jbl.setIcon(new ImageIcon("image2.jpg"));

b) Write a program to make connectivity with the database using JDBC and display records in
table of database. Assume suitable data.
( Correct logical statement - 6 Marks, syntax - 2 Marks)

import java.sql.*;
import java.io.*;
public class JDBCDemo
{
public static void main(String args[])

{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:IIT");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from Student");
System.out.println("Name \t\tRollNumber \t\t Percentage");
while(rs.next())
{

System.out.println(rs.getString(1) + "\t\t" + rs.getString(2) + "\t\t\t" + rs.getString(3));


}
rs.close();
st.close();
Page 19 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

con.close();
}
catch(Exception e)
{
e.printStackTrace();}}}

c) Give steps to create simple servlet and also write program to demonstrate.
(Correct Steps – 4 Marks, any one program - 4 Marks)
Steps : 1) Write a java servet code ;
Steps 2) Compile Servet by setting classpath with servlet-api library.
Step 3 ) Copy the .class servlet file in web apps classpath directory.
Steps 4) Open the web.xml ,open it and add servlet tag and sevlet mapping tag.
Steps 5) Start web server( apche tomcat);
Steps 6) Browse servlet using java compatible browser like IE .
Example shows to start, create a file named WelcomeServlet.java that contains the following
program.

import java.io.*;
import javax.servlet.*;
public class WelcomeServletDemo extends GenericServlet
{
public void service(ServletRequest request,ServletResponse response) throws
ServletException,IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B>Welcome to Servlet");
pw.close();

}
}

Page 20 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

Q.6. Attempt any FOUR of the following: MARKS 16


a) Explain the use of following methods of Datagram Packet Class with example
i) get Port ( )
(Explanation - 2 Marks)
int getport() - It returns the port number
eg. int n= DatagramPacket.getPort();

ii) get Data ( )


(Explanation - 2 Marks)
Byte[] getData() - Returns the byte array of data contained in the datagram. Mostly used to retrieve
data from the datagram after it has been received
eg. public static byte buffer[] = DatagramPacket.getData();

b) Describe three-tier database architecture.


(Figure - 1 Mark, Explanation - 3 Marks)
Three Tier Architecture:-
In the three tier model, the client typically sends requests to an application server, forming the middle
tier. The application server interprets these requests and formats the necessary SQL statement to fulfill
these requests, and sends them to the database. The database process the SQL statements and sends
result back to the application server, which then sends them to the client.
Following are some advantages of Three-tier architecture:
1. Performance can be improved by separating the application server and database server
2. Business logic is clearly separated from the database.
3. Client application can use a simple protocol such as CGI to access services.
The three-tier model show in following fig is a common in web application. In this scenario, the client
tier is frequently implemented in a browse on a client machine, the middle tier is implemented in a
web server with a Servlet engine and the database management system runs on a dedicated database
server.

Page 21 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

Java Application JDBC

Following are the main components of three tier architecture


1. Client Tier: - Typically, this is thin presentation layer that may be implemented using a web
browser
2. Middle tier:- This tier handles the business or application logic . This may be implemented using a
Servlet engine such as Tomcat or an application server such as JBOSS. The JDBC driver also
resides in this layer.
3. Data source layer: This component includes the RDBMS

c) Explain how to create and use the table with an example.


( Step- 2 Marks, Example - 2 Marks-any one program)

1. Create a JTable object.


2. Create a JScrollPane object (Argument to the constructor specifying table and policies for vertical
and Horizontal scrollbar)
3. Add the JTtable to the scrollpane
4. Add Scrollpane to the contentpane of the applet.
import java.awt.*;

import javax.swing.*;

/* <applet code ="JTableDemo" width=300 height=200>

</applet> */

public class JTableDemo extends JApplet

Page 22 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

public void init()

Container cp=getContentPane();

cp.setLayout(new BorderLayout());

final String[] ColHead={ " Name", " Phone","Fax"};

Object[][] data={{ "ABC","2333","4455"},{"XYX","4455","5677"}};

JTable jt=new JTable(data,ColHead);

int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;

int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;

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

cp.add(jsp);

Page 23 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

d) Differentiate between Swing and AWT.


(1 Mark each point, any four points)
Swing AWT

It uses javax.swing package. It uses java.applet package.

It uses JApplet class It uses Applet class

Swing is light weight component AWT is heavy weight component

It does not have add() method to It has add() method to add


add control. control.
AWT provides less features than Swing has variety of component
swing & features which is not in AWT
Awt has huge collection of Swing has bigger collection of
classes & interfaces classes & interfaces than AWT
AWT has predefined formats of Swing provides a facility of
appearance & behavior of different appearance & behavior
component of the same component

e) Explain the use of cookies with example.


(Explanation - 2 Marks, Example - 2 Marks any one)

Cookie is a small piece of information that is passed back and forth in the HTTP request and response.
Even though a cookie can be created on the client side using some scripting language such as
JavaScript, it is usually created by the server resource, such as a Servlet. The Cookies sent by the
server when the client requests another page from the same application. In Servlet programming a
cookie is represented by a Cookie class in the javax.servlet.http package. You can create a cookie by
calling the Cookie class constructor and passing two string objects: the name and value of the cookie.
For instance, the following code creates a cookie object called c1.The cookie has the name
“myCookie” and value of ”secret”:
Cookie c1=new Cookie(“myCookie”,”secret”);
You then can add the cookie to the http response using addCookie method of the
HTTPServletResponse interface: Response.addCookie(c1);

Page 24 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

Program for Add cookies

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class AddCookieServlet extends HttpServlet

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

// Get parameter from HTTP request.

String data = request.getParameter("data");

// Create cookie.

Cookie cookie = new Cookie("MyCookie", data);

// Add cookie to HTTP response.

response.addCookie(cookie);

// Write output to browser.

response.setContentType("text/html");

PrintWriter pw = response.getWriter();

pw.println("<B>MyCookie has been set to");

pw.println(data);

pw.close();

Page 25 of 26
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2005 Certified)

SUMMER – 13 EXAMINATION
Model Answer
Subject Code: 12259 Subject Name: Advanced Java Programming

OR

Program for Get Cookies

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class GetCookiesServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

// Get cookies from header of HTTP request.

Cookie[] cookies = request.getCookies();

// Display these cookies.

response.setContentType("text/html");

PrintWriter pw = response.getWriter();

pw.println("<B>");

for(int i = 0; i < cookies.length; i++)

String name = cookies[i].getName();

String value = cookies[i].getValue();

pw.println("name = " + name +

"; value = " + value);

pw.close();

}
Page 26 of 26

You might also like