SDTL Lab Manual
SDTL Lab Manual
SDTL Lab Manual
Experiment No : 1
1. Title: Implementation of simple Java programs exploring concepts of Data-types, Variables, Arrays and control statements.
2. Prior Concepts:
Class, Object, Member functions, Data types, Operators, Control Statements in Java. 3. Information Input:
3.1 Syntax for class definition:
Where class is a keyword. class_name is a name of the class. data_type is built-in, derived or user defined type. member_name is a name of a variable. return_type is data_type of a returned value. method1 is name of a method. parameter list is list of parameters.
SBPCOE
TE(COMP)
Example: class Rectangle { int length; int width; void getData(int m, int n) { length = m; width = n; } } Task: Study the sample program given below, and note the class definition and declaration of object, and how to access data members of an object. Sample program: Program to define a class Student having data members roll, name. Initialize and display this data for an object of the class Student. import java.lang.*; // import language package which is default package. class Student { int roll ; // data members declaration. String name; //String is a built in class used to store string. void getData(int r, String s) // method to initialize data members. { roll = r; name = s; } void putData( ) // method to display information. { System.out.println(Roll number = + roll); System.out.println(Name = + name); } public static void main(String args[]) // main method { Student s = new Student( ); // creating object. s.getData(1,Raj); // invoking methods s.putData( ); } } Output of the Program : Roll number = 1 Name =Raj
SBPCOE
TE(COMP)
Exercise : Implement following programs using appropriate data-type 1. Initialize the temperature value in Celsius and convert it to Fahrenheit and vice versa 2. Initialize suitable data and calculate area of circle, triangle, rectangle and square . 3. Initialize one dimensional array of any size and separate out even ,odd, prime numbers from this array and calculate average of each separated array 4. Initialize any two dimensional matrix and pick up largest number from this matrix 5. Write a program to produce following output. ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
SBPCOE
TE(COMP)
Experiment No : 2
1. Title :
Exploring concepts Inheritance ,Packages , Interfaces ,String handling ,Exception handling
Super Class
Sub Class
3.3 Syntax for inheritance a class: Class subclassname extends superclassname { Variable declaration; Method decloation; }
SBPCOE
TE(COMP)
The keyword extends signifies that the properties of the super class superclassname are extends to the sub class subclassname. 3.4 String Class :
String class is a built in class from java.lang package. It is used to create strings. The String class object can be created implicitly or explicitly depending on how the string is being used in the program. To create a String implicitly, a string literal is placed in the program and Java creates a String object for the string automatically. e.g. putData(Lata,10); this statement will create a String object automatically to store the string Lata. To create a String object explicitly, the String class is instantiated as below
The above statements will create an object of String class explicitly and initialize it to string abcd. 3.5 Methods of String class.
String class has number of methods which are used to get information about the string or manipulate the string. The details of these methods are available in any Java languagedocumentation or reference books. 3.6 Interface: An Interface is basically a kind of class. Like classes, interface contains variables and methods. The difference is that the interface defines only abstract method.? and final v3riables. By default variables declared inside antrrferface are final and sfatic.'They must be initialized with a constant value. It is the responsibility of the class that implements an interface to deTine the code for these methods. Syntax : interface interface name { variable declaration; method declaration; } Exercise: 1. Calculate the balance of different accounts like Savings account , Fixed Deposit account, Recurring account of a customer . Here Balance is superclass and other classes are derived classes from it . Make use of inheritance In this assignment make appropriate use of package and interface Define an interface which contain methods like add ,delete , display. Implement the classes Queue and stack from this interface
SBPCOE
TE(COMP)
Experiment No : 3
1. Title: Program for file processing 2. Prior Concepts: File, input and output streams, primitive operations on file. 3 Information Input: 3,1 Java io classes for file handling The Java io package provides stream classes to provide capabilities for processing types of data. The InputStream nnd OutputStream classes are the maior components of this hierarchy. The InputStream and OutputStream classes are abslract classes that lay the foundation for the Java Input and Output class hierarchy. As such, they provide methods that are inherited by all lnputStream and OutputStream classes. The inputStream and QutputStream classes have complementary subclasses . 3.2 Random access File : Random Access File class provides the capability to perform I/O directly to specific locations within a file .Random Access File implements both the DataInput and DataOutput interfaces this provides the capability to perform I/O using all objects and primitive data types. Random Access File also supports basic file read/write permissions allowing files to be accessed in read-only or read-write mode. 4. Algorithm 1) Accept from user or specify the name of file to be opened 2) Open the stream in input/output mode as specified 3) Use file to store /retrieve/process from the file 4) Close the files
SBPCOE
TE(COMP)
Exercise : a. Implement student database using file . Accept student name, rollno, class, Percentage marks from user and store it in file and search particular student record and display it. Make appropriate use of features you have studied in previous experiments like inheritance, packages ,interfaces
SBPCOE
TE(COMP)
Experiment No : 4
1.Title: Design GUI using Applet. 2. Prior Concepts: Inheritance, Method overloading and overriding, Interface. 3 Information Input:
3.1
Java Applet An Applet is a dynamic and interactive program that can run inside a web page displayed by a Java-capable web browser Web browser is a program to view web pages. Every applet is implemented by creating a subclass of the Applet class which is the single class in the applet package. The following figure shows the inheritance hierarchy of the Applet class.
The Applet class extends the AWT Panel class, which extends the AWT Container class, which extends the AWT Component class. From Component, an applet inherits the ability to draw and handle events. From Containerf-an applet inherits the ability to include other components and to have a layout manager control the size and position of those components. From Applet, an applet inherits several capabilities, including the ability to respond to major
SBPCOE
TE(COMP)
Every Java applet inherits a set of default behaviors from the Applet class. In most cases, These default behaviors do nothing, unless you override some of Applet's methods in order to Extend the applet's basic functionality. There are five stages in an applets life cycle, each has a matching method that can be overridden to gain access to that stage of the applet's life cycle. 1. Initialization stage: In this stage the applet object is created and loaded. The initialization stage occurs only once in the applet's life cycle. To tap Into the initialization stage, the init() method of Applet has to be overridden. 2. Start stage: This stage occurs when the system starts running the applet. The start stage can occur right after the initialization stage or when an applet is restarted. Unlike the initialization stage, the start stage can occur several times over the life of the applet. To provide start code, override the Applet class's start() method. 3. Paint stage: The paint stage occurs whenever the applet's display mud be drawn on the screen. This happens right after the applet's start stage, as well as wherever the applet's display must be restored or changed. Almost every applet has a paint() method, which is used for display Stop stage: The stop stage is the counter part to the start stage. Java executes this stage of the applet's life cycle when the applet is no longer visible on the screen. To handle the stop stage differently, the stop( ) method of Applet class has to be overridden. Destroy stage: This is the counterpart to the initialization stage and occurs the system is about to remove the applet from memory. Like the initialization the destroy stage occurs only once. If the applet has resources that need cleaned up before the applet exits, then it is done in by overriding the Applet destroy() method. when stage, to be class's
4.
5.
SBPCOE
TE(COMP)
Through web browser: To run an Applet, the html tag containing applet tag has to be stored in a file with extension .html, which can be opened through any Java-compatible web browser. The html tag is given as below.
1.
In the above syntax code is assigned the name of the .class file of the Applet to be viewed and height and width is the size of the Applet in pixels. Through Appletvicwer : Applets can run without a Java-compatible web browser using the Appletviewer tool that comes as part of the JDK. Appletviewer is a Windows application that is run from a DOS command line. When Appletviewer appears, the applet appears in the viewer's main window. The command to view Applet through the Appletviewer is as below. c:\jdkl2 \bin>appletviewer prog1.html. Where c:\jdk1.2\bin is the path where appleviewer.exe file is stored and the file prog1.html contains (eg for the applet. The height and width is the size of the applet in pixels
Algorithm: 1.
2.
Applet subclass, override at least one of the following methods: init( ) , start( ), and paint() and declare them as public. The init and start methods, along with stop and destroy, are called when major events (milestones) occur in the applet's life cycle. The paint method is called when the applet needs to draw itself to the screen
SBPCOE
10
TE(COMP)
Compute
SBPCOE
11
TE(COMP)
Experiment No : 5
1. Title : Study of AWT
2. Prior Concepts:
Applet and event handling 2. Information Input : The Java programming language class library provides a user interface toolkit called the Abstract Windowing Toolkit, or the AWT. The AWT is both powerful and flexible. 3.1 What is a user interface The user interface is that part of a program that interacts with the user of the program. User interfaces take many forms. These forms range in complexity from simple command-line interfaces to the point-and-click graphical user interfaces provided by many modern applications. The AWT provides a well-designed object-oriented interface to these low-level services and resources. . The AWT was designed to provide a common set of tools for graphical user interface design that work on a variety of platforms.
SBPCOE
12
TE(COMP)
The AWT provides nine basic non-container component classes from which a user interface may be constructed. (Of course, new component classes may be derived from any of these or from class Component itself.) These nine classes are class Button, Canvas, Checkbox, Choice, Label, List, Scrollbar, TextArea, and TextField. Types of containers The AWT provides four container classes. They are class Window and its two subtypes -class Frame and class Dialog -- as well as the Panel class. In addition to the containers provided by the AWT, the Applet class is a container -- it is a subtype of the Panel class and can therefore hold components. Brief descriptions of each container class provided by the AWT are provided below.
A top-level display surface (a window). An instance of the Window class is not Window attached to nor embedded within another container. An instance of the Window class has no border and no title. Frame A top-level display surface (a window) with a border and title. An instance of the Frame class may have a menu bar. It is otherwise very much like an instance of the
SBPCOE
13
TE(COMP)
Window class. A top-level display surface (a window) with a border and title. An instance of the Dialog Dialog class cannot exist without an associated instance of the Frame class. A generic container for holding components. An instance of the Panel class Panel provides a container to which to add components.
Creating a container Before adding the components that make up a user interface, the programmer must create a container. When building an application, the programmer must first create an instance of class Window or class Frame. When building an applet, a frame (the browser window) already exists. Since the Applet class is a subtype of the Panel class, the programmer can add the components to the instance of the Applet class itself. The code in Listing 1 creates an empty frame. The title of the frame ("Example 1") is set in the call to the constructor. A frame is initially invisible and must be made visible by invoking its show() method. import java.awt.*; public class Example1 { public static void main(String [] args) { Frame f = new Frame("Example 1"); f.show(); } } Listing 1. An empty frame
The code in Listing 2 extends the code from Listing 1 so that the new class inherits from class Panel. In the main() method, an instance of this new class is created and added to the Frame object via a call to the add() method. The result is then displayed. The results of both examples should look identical (that is, they should look quite uninteresting).
SBPCOE
14
TE(COMP)
import java.awt.*; public class Example1a extends Panel { public static void main(String [] args) { Frame f = new Frame("Example 1a"); Example1a ex = new Example1a(); f.add("Center", ex); f.pack(); f.show(); } }
Listing 2. A frame with an empty panel
By deriving the new class from class Applet instead of class Panel, this example can now run as either a standalone application or as an applet embedded in a Web page. The code for this example is provided in Listing 3. import java.awt.*; public class Example1b extends java.applet.Applet { public static void main(String [] args) { Frame f = new Frame("Example 1b"); Example1b ex = new Example1b(); f.add("Center", ex); f.pack(); f.show(); } }
Listing 3. A frame with an empty applet Figure 5. An empty frame
SBPCOE
15
TE(COMP)
Adding components to a container To be useful, a user interface must consist of more than just a container -- it must contain components. Components are added to containers via a container's add() method. There are three basic forms of the add() method. The method to use depends on the container's layout manager (see the section titled Component layout). The code in Listing 4 adds the creation of two buttons to the code presented in Listing 3. The creation is performed in the init() method because it is automatically called during applet initialization. Therefore, no matter how the program is started, the buttons are created, because init() is called by either the browser or by the main() method. Figure 6 contains the resulting applet. import java.awt.*; public class Example3 extends java.applet.Applet { public void init() { add(new Button("One")); add(new Button("Two")); } public Dimension preferredSize() { return new Dimension(200, 100); } public static void main(String [] args) { Frame f = new Frame("Example 3"); Example3 ex = new Example3(); ex.init(); f.add("Center", ex); f.pack(); f.show(); } }
SBPCOE
16
TE(COMP)
3. Exercise :
Implement standalone application for IceCream parlor using frame
Vanilla Strawberry
Total Amount
SBPCOE
17
TE(COMP)
Experiment No. 6
1. Title : Miniproject 2. Prior Concepts : Database management system, relational database concepts, SQL commands , HTML, All core java concepts 3.Information Input: 3.1 Open database connectivity (ODBC): Open Database Connectivity ODBC is a standard developed by Microsoft . ODBC is an application program regardless of their type. If the data source is ODBC complaint any program can access it using ODBC .
3.2
JDBC : Java Database Connectivity: It is standard API for java developed by Sun Microsystems. Java Database Connectivity or JDBC offers a generic SQL database access mechanism that provides a consistent interface to a variety of relational database management systems. JDBC ODBC bridge: To gain a wider acceptance of JDBC ,Sun based JDBCs framework on ODBC. ODBC has widespread support in a variety of platforms. As JDBC is close to ODBC in design the ODBC is a thin layer over JDBC, Internally this driver maps JDBC methods to ODBC calls and thus interacts with any available ODBC driver, The advantage of this bridge is that now JDBC has capability to access almost all databases as ODBC drivers are widely available.
3.3
3.4 Database Drivers : The consistent interface to variety of relational database management systems is achieved through the use of plug-in database connectivity modules called drivers. To have JDBC support it is necessary to provide the driver for each platform the driver for each platform that the database and Java run on. 3.5 JDBC classed for database access The Java.sql package provides JDBC classes . There are number of classes provides by JDBC for database access. Out of these classes following four classes are required to access any database 1. DriverManager This class is responsible for managing all the available database drivers. The DriverManager class retrieves the list of available classes for drivers from the system properly calles jdbc.drivers. Each of the found drivers is loaded . 2. Connection This interface defines session between an application and
SBPCOE
18
TE(COMP)
a database 3. Statement- This interface is used to issue a single SQL statement through a Connection. It owns only one ResultSet. Therefore multiple concurrent SQL statements must be done through Statements.Issuing a SQL statement while processing the ResultSet of aprevious Statement will result in the overwriting of the results. 4. ResultSet This interface provides access to the data returned from the execution a Statement. 3.6 URL for data access When any database is loaded it is registered with DriverManager, The DriverManager when required to open a connection selects the driver depending on the JDBC URL . JDBC identifies a database with an URL . The URL is in the form jdbc:odbc:<subname related to the DBMS> Where subname Dta Source Name created to access database. E.g. if dataname is created to access Oracle database then URL for the accessing that Oracle database using JDBC will be jdbc :odbc:dataname
HTML stands for Hyper Text Markup Language An HTML file is a text file containing small markup tags The markup tags tell the Web browser how to display the page An HTML file must have an htm or html file extension An HTML file can be created using a simple text editor
Basic HTML Tags Tag <html> <body> <h1> to <h6> <p> <br>
<hr> <!-->
Description Defines an HTML document Defines the document's body Defines header 1 to header 6 Defines a paragraph Inserts a single line break
Defines a horizontal rule Defines a comment
SBPCOE
19
TE(COMP)
HTML Tag Attributes HTML tags can have attributes. Attributes provide additional information to an HTML element. Attributes always come in name/value pairs like this: name="value". Attributes are always specified in the start tag of an HTML element. Attributes Example <h1> defines the start of a heading. <h1 align="center"> has additional information about the alignment. Text Formatting Tags Tag <b> <big> <em> <i> <small> <strong> <sub> <sup> <ins> <del> <s> <strike> <u> Description Defines bold text Defines big text Defines emphasized text Defines italic text Defines small text Defines strong text Defines subscripted text Defines superscripted text Defines inserted text Defines deleted text Deprecated. Use <del> instead Deprecated. Use <del> instead Deprecated. Use styles instead
Tables Tables are defined with the <table> tag. A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). The letters td stands for "table data," which is the content of a data cell. A data cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc.
3.8 JSP
SBPCOE
20
TE(COMP)
3.8.1 What is JSP Page? A text-based document capable of returning both static and dynamic content to a client browser Static content and dynamic content can be intermixed Static content HTML, XML, Text Dynamic content Java code Displaying properties of JavaBeans Invoking business logic defined in Custom tags A Simple JSP Page <html> <body> Hello World! <br> Current time is <%= new java.util.Date() %> </body> </html 3.8.2 JSP Benefits Content and display logic are separated Simplify web application development with JSP, JavaBeans and custom tags Supports software reuse through the use of components (JavaBeans,Custom tags ) Automatic deployment Recompile automatically when changes are made to JSP pages Easier to author web pages Platform-independent 3.8.3 JSP Scripting Elements Lets you insert Java code into the servlet that will be generated from JSP page Minimize the usage of JSP scripting elements in your JSP pages if possible There are three forms Expressions: <%= Expressions %> Scriptlets: <% Code %> Declarations: <%! Declarations %> 3.8.4 Three Types of Directives page: Communicate page dependent attributes and communicate these to the JSP container <%@ page import="java.util.* %> include: Used to include text and/or code at JSP page translation-time <%@ include file="header.html" %> Taglib: Indicates a tag library that the JSP container should interpret <%@ taglib uri="mytags" prefix="codecamp" %>
SBPCOE
21
TE(COMP)
3.8.5 Page Directives Give high-level information about the servlet that results from the JSP page. Control Which classes are imported <%@ page import="java.util.* %> Exercise : Develop a miniproject based upon the concepts like a) Database Management System b) Web based application c) Any standalone Java application e.g, Game etc. Sample web application :
Design Online Marketing site which gives the facility to sell and buy various items in various categories like 1. Art 2.Books 3. Business & Industrial 4.Cameras & Photo 5. Cell Phones & PDAs 6. Clothing, Shoes & Accessories 7.Computers & Networking 8.Consumer Electronics 9. DVDs & Movies 10. Home & Garden 11. Musical Instruments 12. Real Estate 13. Sporting Goods 14 Stamps 15.Tickets etc
SBPCOE
22