Professional Documents
Culture Documents
Web Technologies AIM: 1. Develop static pages (using Only HTML) of an online Book store. The pages should resemble: www.amazon.com. The website should consist the following pages. Home page Registration User Login Books catalog PROCEDURE: Main page: <html> <head> <title>home page</title> </head> <body> <center><b><h1>welcome to amazon.com</h1></b><br><br> <form method="post"action="login.html"> <input type="submit"value="click">registration user login hear </center> </body> </html> Login page: <html> <head> <title>login page</title> </head> <body> <center> <form method="post" action="login.html"> <p><strong>name:</strong> <input type="text" name="username" size="25"> </p> <p><strong>password</strong> <input name="pass" type="password" size="6"></p> <p><strong>male</strong> <input type="radio" value="male"<hacked>  </p> <p><strong>female</strong> <option><input type="radio" value="female"<hacked> </p> <input type="submit" value="submit"> &nbps <input type="reset" value="reset"> <a href="registration.html">new users register hear </a> </form> </center> 1
</body> </html>
Registration: <html> <head> <title>registration</title> </head> <body> <center> <form method="post" action="login.html"> <p><strong>name:</strong> <input type="text" name="username" size="25"> </p> <p><strong>password</strong> <input name="pass" type="password" size="6"></p> <p><strong>male</strong> <input type="radio" value="male"<hacked>  </p> <p><strong>female</strong> <input type="radio" value="female" </p> <p><strong>address:</strong> <textarea name="address" row="6" cols="20"> </textarea> </p> <p><strong>mobile no:</strong> <input type="text" name="phno" size="10"> </p><br><br> <input type="submit" value="submit"> <input type="reset" value="reset"> </form> </center> </body> </html> Books Catalog: <html> <head> <title>books catalog</title> </head> <body> <center><h1><p>welcome to books catalog</p></h1> <table border="1"width="25%"height="50%"> <tr> <th>computers</th> <th>electronics</th> <th>biotech</th> <th>mechanical</th> 2
AIM: 2. Develop static pages (using only HTML) of an online Book store. The pages should resemble :www.amazon.com. The website should consist the following pages. Home page Registration and user Login User profile page Books catalog Shopping cart Payment by credit cardtt Order Conformation
<frame src=right.html name=right> </frameset> </frameset> Top.html: <html> <body bgcolor=pink> <br><br> <marquee><h1 align=center><b><u>ONLINE BOOK STORAGE</u></b></h1></marquee> </body> </html> Right.html: <html> <body bgcolor=pink> <br><br><br><br><br> <h2 align=center> <b><p> welcome to online book storage. Press login if you are having id otherwise press registration. </p></b></h2> </body> </html> Left.html: <html> <body bgcolor=pink> <h3> <ul> <li><a href=login.html target=right><font color=black> LOGIN</font></a></li><br><br> <li><a href=reg.html target=right><font color=black> REGISTRATION</font></a></li><br><br> <li><a href=profile.html target=right><fontcolor=black> USER PROFILE</font></a></li><br><br> <li><a href=catalog.html target=right><fontcolor=black> BOOKS CATALOG</font></a></li><br><br> <li><a href=scart.html target=right><font color=black> SHOPPINGCART</font></a></li><br><br> <li><a href=payment.html target=right><fontcolor=black> PAYMENT</font></a></li><br><br> <li><a href=order.html target=right><font color=black> ORDER CONFIRMATION</font></a></li><br><br> </ul> </body> 7
</html>
Login.html: <html> <body bgcolor=pink><br><br><br> <script language=javascript> function validate() { var flag=1; if(document.myform.id.value==||document.myform.pwd. value== ) { flag=0; } if(flag==1) { alert(VALID INPUT); } else { alert(INVALID INPUT); document.myform.focus(); } } </script> <form name=myform> <div align=center><pre> LOGIN ID:<input type=text name=id><br> PASSWORD:<input type=password name=pwd> </pre><br><br> </div> <br><br> <div align=center> <input type=submit value=ok onClick=validate()> <input type=reset value=clear> </form> </body> </html> Reg.html: <html> <body bgcolor=pink><br><br> <script language=javascript> function validate() { var flag=1; if(document.myform.name.value==|| 8
document.myform.addr.value==|| document.myform.phno.value==|| document.myform.id.value==|| document.myform.pwd.value==) { flag=0; } var str=document.myform.phno.value; var x; for(var i=0;i<str.length;i++) { x=str.substr(i,1) if(!(x<=9)) { flag=0; break; }
} if(flag==1) { alert("VALID INPUT"); } else { alert("INVALID INPUT"); document.myform.focus(); } } </script> <form name="myform"> <div align="center"><pre> NAME:<input type="text" name="name"><br> ADDRESS:<input type="type" name="addr"><br> CONTACT NUMBER:<iput type="text" name="phno"><br> LOGINID:<input type="text" name="id"><br> PASSWORD:<input type="password" name="pwd"></pre><br><br> </div> <br><br> <div align="center"> <input type="submit" value="ok" onClick="validate()"> <input type="reset" value="clear"> </form></body></html> Catalog.html: <html> <body bgcolor="pink"><br><br><br> 9
<div align="center"><pre> BOOK TITLE :<input type="text" name="title"><br> </pre><br><br> </div> <br><br> <div align="center"> <input type="submit" value="ok" name="button1"> <input type="reset" value="clear" name="button2"> </body> </html> Order.html: <html> <body bgcolor="pink"><br><br><br> <div align="center"><pre> LOGIN ID:<input type="text" name="id"><br> TITLE:<input type="text" name="title"><br> NO.OF BOOKS :<input type="text" name="no"><br> COST OF BOOK:<input type="text"name="cost"><br> DATE:<input tpe="text" name="date"><br></pre><br><br> </div> <br><br> <div align="center"> <input type="submit" value="ok" name="button1"> <input type="reset" value="clear" name="button2"> </body> </html> Payment.html: <html> <body bgcolor="pink"><br><br><br> <script language="javascript"> function validate() { var flag=1; if(document.myform.id.value==""|| document.myform.pwd.value==""|| document.myform.amount.value==""|| document.myform.num.value=="") { flag=0; } var str=document.myform.amount.value; var x; for(var i=0;i<str.length;i++) { x=str.substr(i,1); 10
} str=document.myform.num.value; for(var i=0;i<str.lenght;i++) { x=str.substr(i,1); if(!(x<=9)) { flag=0; break; } } if(flag==1) { alert("VALID INPUT"); } else { alert("INVALID INPUT"); document.myform.focus(); } } </script> <form name="myform"> <div align="center"><pre> LOGIN ID:<input type="text" name="id"><br> PASSWORD:<input type="password" name="pwd"><br> AMOUNT:<input type="text" name="amount"><br> CREDITCARDNUMBER:<input type="PASSWORD" name="num+"><br></pre><br><br> </div> <br><br> <div align="center"> <input type="submit" value="ok" onClick="validate()"> <input type="reset" value="clear" > </form> </body> </html> Profile.html: <html> <body bgcolor="pink"><br><br><br> <script language="javascript"> 11
function validate() { var flag=1; if(document.myform.id.value==""|| document.myform.pwd.value=="") { flag=0; } if(flag==1) { alert("VALID INPUT"); } else { alert("INVALID INPUT"); document.myform.focus(); } } </script> <form name="myform"> <div align="center"><pre>
LOGIN ID :<input type="text" name="id"><br> PASSWORD:<input type="password" name="pwd"></pre><br><br> </div> <br><br> <div align="center"> <input type="submit" value="ok" onClick="validate()"> <input type="reset" value="clear" > </form> </body> </html>
12
13
14
15
16
17
Validation.html <html> <head> <title>Registration</title> <script type="text/javascript"> function validate() { var c1=new RegExp("^[\\w]{6,}$"); var c2=new RegExp("^[\\w]{6,}$"); var c3=new RegExp("^[\\w]+@[\\w]+.com$"); var c4=new RegExp("^[\\d]{10}$"); var name=document.f.user.value; var pwd=document.f.pwd.value; var email=document.f.email.value; var phone=document.f.phone.value; var error=""; if(!name.match(c1)) { error+="should be min of 6 char\n" }
</head> <body bgcolor=#7D9EC0> <center> <form name=f> <table align=center border=0 width=65% height=100%> <caption align=center> REGISTRATION FORM </caption> <tr > <td>UserName :</td> <td > <input type=text name=user></td> </td> </tr> <tr> <td>Password:</td> <td ><input type=password name=pwd></td> </tr> <tr > <td>E-mail id:</td> <td ><input type=text name=email></td> </tr> <tr> <td>Phone Number:</td> <td ><input type=text name=phone></td> <td> <p id= "ph"> </p></td> </tr> <tr> <td><input type=button value="Submit" onclick=validate()></td> <td><input type=reset value="Reset"></td> </tr> </table> </form> </body> </html>
19
AIM 4. Write a program illustrating various methods in cascading style sheets. 1) 2) 3) 4) 20 Use different font, styles Set a background image Control the repetition of the image Define styles for links
DESCRIPTION: Design a web page using CSS (Cascading Style Sheets) which includes the following: 1) Use different font, styles: In the style definition you define how each selector should work (font, color etc.).Then, in the body of your pages, you refer to these selectors to activate the styles.
2) Set a background image for both the page and single elements on the page. You can define the background image for the page like this:
3) Control the repetition of the image with the background-repeat property. As background-repeat: repeat 4) Define styles for links as 5) Work with layers: 6) Add a customized cursor: Selector {cursor:value} .xlink {cursor:crosshair} .hlink{cursor:help}
1. Font syles
<style type="text/css">
b.headline {color:red;font-size:22pt;font-family:arial;text-decoration:none} </style> </head> <body> <b> this is normal bold</b> <br /> <b class="headline"> this is headline style bold</b> </body> </html>
Output Screen:
22
2. Background image
<html> <head> 23
<style>
body{ background-image:url("winter.jpg");} table { background-image:url(water lilies.jpg);} </style> <body > <table border=2 width=50% height=50% align=center> <tr> <td> slno</td> <td> name </td> </tr> <tr> <td> 1</td> <td> raj</td> </tr> <tr> <td> 2</td> <td> kumar</td> </tr> </table></body></html>
Output Screens:
24
3. Control repeat
<html> <head> 25
<style> body{
background-image:url(winter.jpg); background-repeat:no-repeat; } </style> </head> </body> <h2> this is the body of the page where the image is repeated</h2> </body> </html>
Output Screen:
26
<html> <head> 27
<style type="text/css">
a:link {text-decoration:underline;color=red} a:active{ text-decoration;OVERLINE;COLOR:green} a:visited{ text-decoration;COLOR:yellow} a:hover{ text-decoration:overline; color=green} </style></head> <body> <h2> these are links </h2> <a href=LINKSTYLES.html >LINK1</a><br /><br /><br /> <a href=LINKSTYLES.html >LINK2</a><br /><br /><br /> <a href=3.html >LINK3</a><br /><br /><br /> <a href=4.html >LINK4</a><br /> </body></html>
Output Screens:
28
<html> <head> 29
<div style="position: absolute;top: 50px;left: 5px;z-index: 2;fontsize:50PX;color:red">LAYER 1 </div> <div style="position: absolute;top: 70px;left: 50px;z-index: 1;fontsize:50PX;color:green">LAYER 2 </div> </body> </html>
Output Screens:
AIM : 5.Developing a Simple Bean Using the BDK: This section presents an example that shows how to develop a simple Bean and connect it to other components via the BDK. Our new component is called the Colors Bean. It appears as either a rectangle or ellipse that is filled with a color. A color is chosen at random when the Bean begins 30
execution. A public method can be invoked to change it. Each time the mouse is clicked on the Bean, another random color is chosen. There is one boolean read/write property that determines the shape. The BDK is used to lay out an application with one instance of the Colors Bean and one instance of the OurButton Bean. The button is labeled Change. Each time it is pressed, the color changes. SOFTWARE DEVELOPMENT USING JAVA Create a New Bean Here are the steps that you must follow to create a new Bean: 1. Create a directory for the new Bean. 2. Create the Java source file(s). 3. Compile the source file(s). 4. Create a manifest file. 5. Generate a JAR file. 6. Start the BDK. 7. Test. The following sections discuss each of these steps in detail. Create a Directory for the New Bean You need to make a directory for the Bean. To follow along with this example, create c:\bdk\demo\sunw\demo\colors. Then change to that directory. The Colors and OurButton Beans Create the Source File for the New Bean The source code for the Colors component is shown in the following listing. It is located in the file Colors.java. The import statement at the beginning of the file places it in the package named sunw.demo.colors. Recall from Chapter 9 that the directory hierarchy corresponds to the package hierarchy. Therefore, this file must be located in a subdirectory named sunw\demo\colors relative to the CLASSPATH environment variable. The color of the component is determined by the private Color variable color, and its shape is determined by the private boolean variable rectangular. The constructor defines an anonymous inner class that extends MouseAdapter and overrides its mousePressed( ) method. The change( ) method is invoked in response to mouse presses. The component is initialized to a rectangular shape of 200 by 100 pixels. The change( ) method is invoked to select a random color and repaint the component. The getRectangular( ) and setRectangular( ) methods provide access to the one property of this Bean. The change( ) method calls randomColor( ) to choose a color and then calls repaint( ) to make the change visible. Notice that the paint( ) method uses the rectangular and color variables to determine how to present the Bean.
31
} } }
Compile the Source Code for the New Bean Compile the source code to create a class file. Type the following: javac Colors.java. Create a Manifest File. You must now create a manifest file. First, switch to the c:\bdk\demo directory. This is the directory in which the manifest files for the BDK demos are located. Put the source code for your manifest file in the file colors.mft. It is shown here: Name: sunw/demo/colors/Colors.class Java-Bean: True This file indicates that there is one .class file in the JAR file and that it is a Java Bean. Notice that the Colors.class file is in the package sunw.demo.colors and in the subdirectory sunw\demo\colors relative to the current directory. Generate a JAR File Beans are included in the ToolBox window of the BDK only if they are in JAR files in the directory c:\bdk\jars. These files are generated with the jar utility. Enter the following: jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class This command creates the file colors.jar and places it in the directory c:\bdk\jars. (You may wish to put this in a batch file for future use.) Start the BDK Change to the directory c:\bdk\beanbox and type run. This causes the BDK to start. You should see three windows, titled ToolBox, BeanBox, and Properties. The ToolBox window should include an entry labeled Colors for your new Bean. Create an Instance of the Colors Bean After you complete the preceding steps, create an instance of the Colors Bean in the BeanBox window. Test your new component by pressing the mouse anywhere within its borders. Its color immediately changes. Use the Properties window to change the rectangular property from false to true. Its shape immediately changes. Create and Configure an Instance of the OurButton Bean Create an instance of the OurButton Bean in the BeanBox window. Then follow these steps: 1. Go to the Properties window and change the label of the Bean to Change. You should see that the button appearance changes immediately when this property is changed. 2. Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed. 3. Move the cursor so that it is inside the Colors Bean display area, and click the left mouse button. You should see the Event Target Dialog dialog box. 4. The dialog box allows you to choose a method that should be invoked when 33
this button is clicked. Select the entry labeled change and click the OK button. You should see a message box appear very briefly, stating that the tool is Generating and compiling adaptor class. 5. Click on the button. You should see the color change. You might want to experiment with the Colors Bean a bit before moving on. Output:
Create two Beans Traffic Light(implemented as a label with only three background colors-red, green, yellow) and Automobile(Implemented as a Text Box which states its state/movement). The state of the Automobile should depend on the following Light Transition table. /*<applet code="Sig.class" height=300 width=200></applet>*/ import java.awt.*; import java.lang.String; import java.awt.event.*; import java.applet.Applet; import java.applet.*; public class Sig extends Applet implements ItemListener{ boolean c1,c2,c3; String s1; Checkbox r1,r2,r3; CheckboxGroup cbg; public void init(){ cbg=new CheckboxGroup(); Panel p=new Panel(); 34
p.setLayout(new GridLayout()); add(r1=new Checkbox("red",cbg,false)); add(r2=new Checkbox("yellow",cbg,false)); add(r3=new Checkbox("green",cbg,false)); r1.addItemListener(this); r2.addItemListener(this); r3.addItemListener(this); } public void paint(Graphics g) { g.setColor(Color.red); g.drawOval(10, 30, 20, 20); //System.out.println(s1); g.setColor(Color.yellow); g.drawOval(10, 60, 20, 20); g.setColor(Color.green); g.drawOval(10, 90, 20, 20); s1=cbg.getSelectedCheckbox().getLabel(); if(s1=="red") { g.setColor(Color.red); g.fillOval(10, 30, 20, 20); } else if(s1=="yellow") { g.setColor(Color.yellow); g.fillOval(10, 60, 20, 20); } else if(s1=="green") { g.setColor(Color.green); g.fillOval(10, 90, 20, 20); } } public void itemStateChanged(ItemEvent ie) { repaint(); } }
35
AIM: 6. Install TOMCAT web server. Convert the static webpages of assignments 2 into dynamic webpages using servlets and cookies. Hint: Users information (user id, password, credit card number) would be stored in web.xml. Each user should have a separate Shopping Cart. PROCEDURE: First install the tomcat into the system. Then make a subdirectly(eg., tr) in the \tomcat\webapps. Under tr create WEB-INF directory and also place the html files in this tr directory only. Next under WEB-INF create two subclasses lib,classes and web.xml Next place all the class files under the classes and jar files(servlet-api.jar,classes12.jar etc) under lib subdirectories. After this start tomcat by giving the following command at the instll_dir>tomcat>bin Catalina.bat run At the I.E(web browser) give the url as http;//localhost:8080//tr/htmlfile or servlet url pattern Portno 8080 is assigned for the tomcat.
Web.xml <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 36
"http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Servlet 2.4 Examples</display-name> <description> Servlet 2.4 Examples. </description> <servlet> <servlet-name>reg</servlet-name> <servlet-class>reg</servlet-class> </servlet> <servlet> <servlet-name>login</servlet-name> <servlet-class>login</servlet-class> </servlet> <servlet> <servlet-name>profile</servlet-name> <servlet-class>profile</servlet-class> </servlet> <servlet> <servlet-name>catalog</servlet-name> <servlet-class>catalog</servlet-class> <servlet-mapping> <servlet-name>order</servlet-name> <url-p</servlet> <servlet> <servlet-name>order</servlet-name> <servlet-class>order</servlet-class> </servlet> attern>order</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>catalog</servlet-name> <url-pattern>catalog</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>profile</servlet-name> <url-pattern>profile</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>reg</servlet-name> <url-pattern>reg</url-pattern> </servlet-mapping> 37
</web-app>
Main.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body bgcolor="pink"> <br /><br /><br /><br /><br /> <h1 align="center"><U>ONLINE BOOK STORAGE</U></h1><br /><br /><br /> <h2 align="center"><pre> <b>Welcome to online book storage. Press LOGIN if you are having id otherwise press REGISTRATION </b></pre></h2> <br /><br /><pre> <div align="center"><a href="/tr/login.html">LOGIN</a> <a href="/tr/reg.html"> REGISTRATION</a></div></pre> </body> </html>
Login.html <html> <body bgcolor="pink"><br /><br /><br /> <form name="myform" method="post" action="/tr/login"> <div align="center"><pre> LOGIN ID :<input type="text" name="id" /><br /> PASSWORD :<input type="password" name="pwd" /></pre><br /><br /> </div> <br /><br /> <div align="center"> <input type="submit" value="ok" onclick="validate()" /> <input type="reset" value="clear" /> </div> </form> </body> </html> Reg.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body bgcolor="pink"><br /><br /> <form name="myform" method="post" action="/tr/reg"> <div align="center"><pre> NAME:<input type="text" name="name" /><br /> ADDRESS:<input type="text" name="addr" /><br /> 38
CONTACT NUMBER:<input type="text" name="phno" /><br /> LOGINID:<input type="text" name="id" /><br /> PASSWORD:<input type="password" name="pwd" /></pre><br /><br /> </div> <br /><br /> <div align="center"> <input type="submit" value="ok" onclick="validate()" /> <input type="reset" value="clear" /> </div> </form> </body> </html> Profile.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body bgcolor="pink"><br /><br /><br /> <form name="myform" method="post" action="/tr/profile"> <div align="center"><pre> LOGIN ID :<input type="text" name="id" /><br /> </pre><br /><br /> </div> <br /><br /> <div align="center"> <input type="submit" value="ok" onclick="validate()" /> <input type="reset" value="clear" /> </div> </form> </body> </html> Catalog.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body bgcolor="pink"><br /><br /><br /> <form method="post" action="/tr/catalog"> <div align="center"><pre> BOOK TITLE :<input type="text" name="title" /><br /> </pre><br /><br /> </div> <br /><br /> <div align="center"> <input type="submit" value="ok" name="button1"/> 39
Order.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body bgcolor="pink"><br /><br /> <form method="post" action="/tr/reg"> <div align="center"><pre> NAME:<input type="text" name="name" /><br /> PASSWORD:<input type="password" name="pwd" /> TITLE:<input type="text" name="title" /><br /> NO. OF BOOKS:<input type="text" name="no" /><br /> DATE:<input type="text" name="date" /><br /> CREDIT CARD NUMBER:<input type="password" name="cno" /><br /></pre><br /><br /> </div> <br /><br /> <div align="center"> <input type="submit" value="ok" name="button1"/> <input type="reset" value="clear" name="button2"/> </div> </form> </body> </html> Login.java import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class login extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { PrintWriter pw=resp.getWriter(); pw.println("<html><body bgcolor=\"pink\"); String id=req.getParamenter("id"); String pwd=req.getParameter("pwd"); try 40
Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1 521:orcl","scott","tiger"); Statement stmt=con.createStatement(); String sqlstmt="select id,password from login"; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0; while(rs.next()) { if(id.equal(rs.getString(1))&&pwd.equals(rs.getString(2))) { flag=1; } } if(flag==0) { pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>"); pw.println("<a href=\"/tr/login.html\">press LOGIN to RETRY</a>"); } else { pw.println("VALID LOGIN ID<br><br>"); pw.println("<h3><ul>"); pw.println("<li><ahref=\"profile.html\"><fontcolor=\"blac k\">USER PROFILE</font></a></li><br><br>"); pw.println("<li><ahref=\"catalog.html\"><fontcolor=\"black\">BO OKS CATALOG</font></a></li><br><br>"); pw.println("<li><ahref=\"order.html\"><fontcolor=\"black\">ORD ER CONFIRMATION</font></a></li><br><br>"); } pw.println("</body></html>"); } catch(Exception e) { resp.sendError(500,e.toString()); } } }
41
Reg.html import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class login extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { PrintWriter pw=resp.getWriter(); pw.println("<html><body bgcolor=\"pink\"); String name=req.getParamenter("name"); String addr=req.getParameter("addr"); String phno=req.getParameter("phno"); String id=req.getParamenter("id"); String pwd=req.getParameter("pwd");
int no=Integer.parseInt(phno); try { Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1 521:orcl","scott","tiger"); Statement stmt=con.createStatement(); String sqlstmt="select id,password from login"; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0; while(rs.next()) { if(id.equal(rs.getString(1))&&pwd.equals(rs.getString(2))) { flag=1; } } if(flag==1) { pw.println("SORRY INVALID ID ALREADY EXITS TRY AGAIN WITH NEW ID<br><br>"); pw.println("<a href=\"/tr/reg.html\">press REGISTER to RETRY</a>"); } else { 42
Statement stmt1=con.createStatement(); stmt1.executeUpdate("insertintologin values("+names","+addr+","+no+","+id+","+pwd+")"); pw.println("YOUR DETAILS ARE ENTERED<br><br>"); pw.println("<a href=\"/tr/login.html\">press LOGIN to login</a>"); } pw.println("</body></html>"); } catch(Exception e) { resp.sendError(500,e.toString()); } } }
Catlog.java import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class login extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { PrintWriter pw=resp.getWriter(); pw.println("<html><body bgcolor=\"pink\"); String title=req.getParameter("title"); try { Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1 521:orcl","scott","tiger"); Statement stmt=con.createStatement(); String sqlstmt="select id,password from login"; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0; while(rs.next()) { pw.println(",div align=\"center\">"); pw.println("TITLE:"+rs.getString(1)+"<br>"); pw.println("AUTHOR:"+rs.getString(2)+"<br>"); 43
pw.println("</div"); flag=1; } if(flag==0) { pw.println("SORRY INVALID TITLE TRY AGAIN <br><br>"); pw.println("<a href=\"/tr/catalog.html\">press HERE to RETRY</a>"); } pw.println("</body></html>"); } catch(Exception e) { resp.sendError(500,e.toString()); } } }
Profile.java import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class login extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { PrintWriter pw=resp.getWriter(); pw.println("<html><body bgcolor=\"pink\"); String id=req.getParamenter("id"); try { Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con=DriverManager.getConnection("jdbc:oracle:thin: @localhost:1521:orcl","scott","tiger"); Statement stmt=con.createStatement(); 44
String sqlstmt="select * from login where id="+id+""; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0; pw.println("<br><br><br>"); while(rs.next()) { pw.println("<div align=\"center\">"); pw.println("NAME :"+rs.getString(1)+"<br>"); pw.println("ADDRESS:"+rs.getString(2)+"<br>"); pw.println("PHONE NO:"+rs.getString(3)+"<br>"); pw.println("</div>"); flag=1; } if(flag==0) { pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>"); pw.println("<a href=\"/tr/profile.html\">press HERE to RETRY</a>"); } pw.println("</body></html>"); } catch(Exception e) { resp.sendError(500,e.toString()); } } }
Order.java import java.sql.*; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class login extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException { PrintWriter pw=resp.getWriter(); pw.println("<html><body bgcolor=\"pink\"); String id=req.getParamenter("id"); String pwd=req.getParameter("pwd"); String title=req.getParameter("title"); 45
String count1=req.getParameter("no"); String date=req.getParameter("date"); String cno=req.getParameter("cno"); int count=Integer.parseInt(count1); try { Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1 521:orcl","scott","tiger"); Statement stmt=con.createStatement(); String sqlstmt="select id,password from login"; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0,amount,x; while(rs.next()) { if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2))) { flag=1; } } if(flag==0) { pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>"); pw.println("<a href=\\"/tr/order.html\\">press HERE to RETRY</a>"); } else { Statement stmt2=con.createStatement(); String s="select cost from book where title="+title+""; ResultSet rs1=stmt2.executeQuery(s); int flag1=0; while(rs1.next()) { flag1=1; x=Integer.parseInt(rs1.getString(1)); amount=count*x; pw.println("AMOUNT :"+amount+"<br><br><br><br>"); Statement stmt1=con.createStatement(); stmt1.executeUpdate("insertintodetails values('"+id+",'"+title+"'+amount+'","'+cno+'")"'); pw.println("YOUR ORDER has taken<br>"); 46
Output :
47
48
49
AIM: 7. Redo the previous task using JSP by converting the static web pages of assignments 2 into dynamic web pages. Create a database with user information and books information and books information. The books catalogue should be dynamically loaded from the database. Follow the MVC architecture while doing the website. PROCEDURE: 1) Create your own directory under tomcat/webapps (e.g. tr1) 2) Copy the html files in tr1 3) Copy the jsp files also into tr1 4) Start tomcat give the following command Catalina.bat run 5) At install-dir/bin at I.E give url as http://localhost:8081/tr1/main.html Main.html: <html> <body bgcolor=pink> <br><br><br><br><br><br> <h1 align=center>>U>ONLINE BOOK STORAGE</u></h1><br><br><br> <h2 align=center><PRE> <b> Welcome to online book storage. Press LOGIN if you are having id Otherwise press REGISTRATION 50
Login.html: <html> <body bgcolor=pink><br><br><br> <form name="myform" method="post" action=/tr1/login.jsp"> <div align="center"><pre> LOGIN ID : <input type="passwors" name="pwd"></pre><br><br> PASSWORD : <input type="password" name="pwd"></pre><br><br> </div> <br><br> <div align="center"> <inputtype="submit"value="ok" onClick="validate()"> <input type="reset" value="clear"> </form> </body> </html>
Reg.html: <html> <body bgcolor="pink"><br><br> <form name="myform" method="post" action="/tr1/reg.jsp"> <div align="center"><pre> NAME:<input type="text" name="name"><br> ADDRESS :<input type="text" name="addr"><br> CONTACT NUMBER : <input type="text" name="phno"><br> LOGIN ID : <input type="text" name="id"><br> PASSWORD : <input type="password" name="pwd"></pre><br><br> </div> <br><br> <div align="center"> <inputtype="submit"value="ok" onClick="validate()">()"> <input type="reset" value="clear"> </form> </body> </html> Profile.html: <html> 51
<body bgcolor="pink"><br><br> <form name="myform" method="post" action="/tr1/profile.jsp"> <div align="center"><pre> LOGIN ID : <input type="text" name="id"><br> </pre><br><br> </div> <br><br> <div align="center"> <inputtype="submit"value="ok" onClick="validate()">()"> <input type="reset" value="clear"> </form> </body> </html> Catalog.html: <html> <body bgcolor="pink"><br><br><br> <form method="post" action="/tr1/catalog.jsp"> <div align="center"><pre> BOOK TITLE : <input type="text" name="title"><br> </pre><br><br> </div> <br><br> <div align="center"> <inputtype="submit"value="ok" name=button1> <inputtype="reset"value="clear" name=button2> </form> </body> </html> Order.html: <html> <body bgcolor="pink"><br><br><br> <form method="post" action="/tr1/order.jsp"> <div align="center"><pre> LOGIN ID:<input type="text" name="id"><br> PASSWORD : <input type="password" name="pwd"><br> TITLE:<input type="text" name="title"><br> NO. OF BOOKS : <input type="text" name="no"><br> DATE: <input type="text" name="date"><br> CREDIT CARD NUMBER : <input type="password" name="cno"><br></pre><br><br> </div> <br><br> <div align="center">
52
<input type="submit" value="ok" name=button1> <input type="reset" value="clear" name=button2> </form> </body> </html>
Login.jsp: %@page import=java.sql.*% %@page import=java.io.*% <% out.println(<html><body bgcolor=\pink\>); String id=request.getParameter(id); String pwd=request.getParameter(pwd); Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con=DriverManager.getConnection(jdbc:oracle:thin:@localhost:1521:orcl,scott,tiger); Statement stmt=con.createStatement(); String sqlstmt=select id,password from login where id=+id+ and password=+pwd+; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0; while(rs.next()) { flag=1; } if(flag==0) { out.println(SORRY INVALID ID TRY AGAIN ID<br><br>); out.println( <a href=\/tr1/login.html\>press LOGIN to RETRY</a>); } else { out.println(VALID LOGIN ID<br><br>); out.println(<h3><ul>); out.println(<li><ahref=\profile.html\><fontcolor=\black\>USER PROFILE</font></a></li><br><br>); out.println(<li><ahref=\catalog.html\><fontcolor=\black\>BOOKS CATALOG</font></a></li><br><br>); out.println(<li><ahref=\order.html\><fontcolor=\black\>ORDER CONFIRMATION</font></a></li><br><br>); out.println(</ul>); } out.println(<body></html>); %> Reg.jsp: 53
%@page import=java.sql.*% %@page import=java.io.*% <% out.println(<html><body bgcolor=\pink\>); String name=request.getParameter(name); String addr=request.getParameter(addr); String phno=request.getParameter(phno); String id=request.getParameter(id); String pwd=request.getParameter(pwd); int no=Integer.parseInt(phno); Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(d); Connection con= DriverManager.getConnection (jdbc:oracle:thin:@localhost:1521:orcl,scott,tiger); Statement stmt=con.createStatement(); String sqlstmt=select id from login; ResultSet rs=stmt.executeQuery(sqlstmt); int flag=0; while(rs.next()) { if(id.equals(rs.getString(1))) { flag=1; } } if(flag==1) { out.println(SORRY LOGIN ID ALREADY EXISTS TRY AGAIN WITH NEW ID <br><br>); out.println(<a href=\/tr1/reg.html\>press REGISTER to RETRY</a>); } else { Statement stmt1=con.createStatement (); stmt1.executeUpdate (insert into login values (+name+,+addr+,+no+,+id+,+pwd+)); out.println (YOU DETAILS ARE ENTERED <br><br>); out.println (<a href =\/tr1/login.html\>press LOGIN to login</a>); } out.println (</body></html>); %>
out.println (<html><body bgcolor=\pink\>); String id=request.getParameter(id); Driver d=new oracle.jdbc.driver.OracleDriver(); DriverManager.regiserDriver(d); Connection con= DriverManager.getConnection (jdbc:oracle:thin:@localhost:1521:orcl,scott,tiger); Statement stmt=con.createStatement (); String sqlstmt=select * from login where id=+id+; ResultSet rs=stmt.executeQuery (sqlstmt); int flag=0; while(rs.next()) { out.println (<div align=\center\>); out.println (NAME:+rs.getString(1)+<br>); out.println (ADDRESS :+rs.getString(2)+<br>); out.println (PHONE NO :+rs.getString(3)+<br>); out.println (</div>); flag=1; } if(flag==0) { out.println(SORRY INVALID ID TRY AGAIN ID <br><br>); out.println(<a href=\/tr1/profile.html\>press HERE to RETRY </a>); } out.println (</body></html>); %> Catalog.jsp: <%@page import=java.sql.*%> <%@page import=java.io.*%> <% out.println (<html><body bgcolor=\pink\>); String title=request.getParameter (title); Driver d=new oracle.jdbc.driver.OracleDriver (); DriverManager.regiserDriver (d); Connection con= DriverManager.getConnection (jdbc:oracle:thin:@localhost:1521:orcl,scott,tiger); Statement stmt=con.createStatement (); String sqlstmt=select * from book where title=+title+; ResultSet rs=stmt.executeQuery (sqlstmt); int flag=0; while(rs.next()) { out.println (<div align=\center\>); out.println (TITLE:+rs.getString(1)+<br>); out.println (AUTHOR :+rs.getString(2)+<br>); out.println (VERSION:+rs.getString(3)+<br>); out.println (PUBLISHER : +rs.getString(4)+<br>); 55
out.println (COST : +rs.getString(5)+<br>); out.println (</div>); flag=1; } if(flag==0) { out.println(SORRY INVALID ID TRY AGAIN ID <br><br>); out.println(<a href=\/tr1/catalog.html\>press HERE to RETRY </a>); } out.println (</body></html>); %> Order.jsp: <%@page import=java.sql.*%> <%@page import=java.io.*%> <% out.println (<html><body bgcolor=\pink\>); String id=request.getParameter (id); String pwd=request.getParameter (pwd); String title=request.getParameter (title); String count1=request.getParameter (no); String date=request.getParameter (date); String cno=request.getParameter (cno); int count=Integer.parseInt(count1); Driver d=new oracle.jdbc.driver.OracleDriver (); DriverManager.regiserDriver (d); Connection con= DriverManager.getConnection (jdbc:oracle:thin:@localhost:1521:orcl,scott,tiger); Statement stmt=con.createStatement (); String sqlstmt=select id, password from login; ResultSet rs=stmt.executeQuery (sqlstmt); int flag=0,amount,x; while(rs.next()) { if(id.equals(rs.getString(1))&& pwd.equals(rs.getString(2))) { flag=1; } } if(flag==0) { out.println(SORRY INVALID ID TRY AGAIN ID <br><br>); out.println(<a href=\/tr1/order.html\>press HERE to RETRY </a>); } else { Statement stmt2=con.createStatement(); 56
String s=select cost from book where title=+title+; ResultSet rs1=stmt2.executeQuery(s); int flag1=0; while(rs1.next()) {
flag1=1; x=Integer.parseInt(rs1.getString(1)); amount=count*x; out.println(AMOUNT :+amount+<br><br><br><br>); Statement stmt1=con.createStatement (); stmt1.executeUpdate (insert into details (+id+,+title+,+amount+,+date+,+cno+)); out.println (YOU ORDER HAS TAKEN<br>); } if(flag1==0) { out.println(SORRY INVALID BOOK TRY AGAIN <br><br>); out.println(<a href=\/tr1/order.html\>press HERE to RETRY </a>); } }out.println (</body></html>);%>
57
58
59
60
61
<head> <title> Sort </title> <script language="Javascript"> var b,i,j,temp; var a; a=[]; document.write("Enter array"); for(i=0;i<5;i++) { b=prompt("enter array element=","0"); a[i]=parseInt(b); } for(i=0;i<5;i++) { document.write("<br>"+a[i]); } document.write("<br>Sorted array is"); for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } for(i=0;i<5;i++) { document.write("<br>"+a[i]); } </script> </head></html>
62
OUTPUT
63
AIM : Write Java Script that inputs three integers from the user and outputs their sum, average, largest. Use alert dialog box to display results. PROGRAM : <html> <head> <title> MAX </title> <script language="javascript"> var a,b,c,n1,n2,n3,m1,m2,sum,avg; a=prompt("enter 1st no="," "); b=prompt("enter 2nd no="," "); c=prompt("enter 3rd no="," "); n1=parseInt(a); n2=parseInt(b); n3=parseInt(c); sum=n1+n2+n3; avg=sum/3; m1=Math.max(n1,n2); m2=Math.max(n3,m1); alert("the sum is= "+sum); alert("the avg is= "+m2); alert("the max no is "+m2); </script> </head></html> OUTPUT :
64
OUTPUT 66
67
<title>style sheets</title> <style type=text/css></head> em{backgroundcolor:#8000ff;color=white} h1{font family:Lucida console;color:red} p{font size:30pt} sp{color:red} </style> </head> <body> <h2class=sp>testing</h2> <em><h1>test</h1></em> <em>test1</em> <p>test2</p> </body> </html>
68
<title> STUDENT GRADES </title> <script language="java script"> var a,n,b,c,sum=0,avg=0; b=prompt("enter student no=","0"); n=parseInt(b); while(n<=10) { a=prompt("enter ur grade=","0"); c=parseInt(a); sum=sum+c; n++; } avg=sum/n; document.write("avg is"+avg); </script> </head></html>
69
70
OUTPUT
72
PROGRAM <! code to show the use of tables> <html> <head> <title> Using Tables</title> </head> <body><center> <table border="1" width="5" height="5" bordercolor="red"> <caption> Student Marks </caption> <thead> <tr> <th>Name</th> <th>Marks</th> </tr> </thead> <tbody> <tr> <td>alok</td> <td>90</td> </tr> 73
<tr>
<td>anuj</td> <td>90</td> </tr> <tr> <td>ankush</td> <td>90</td> </tr> </tbody> <tfoot> <tr> <th>Avg Marks</th> <th>90</th> </tr> </tfoot> </body> </html>
OUTPUT
74
AIM : Write A Program for implementation of List. PROGRAM <html> <body> <ul> <li>list1</li> </ul><ul> <li> JNTU WORLD </li> 75
<ol>
AIM : Write a java script to compute addition of two numbers. PROGRAM <html> <head> <title> Java Script Add </title> <script language="java script"> alert("Addition \n of two numbers!"); var num1, num2,n1,n2,sum; 76
num1=window. prompt("Enter the first number","0"); n1=parseInt(num1); n2=parseInt(num2); sum=n1+n2; alert("sum is"+sum); </script> </head> <body> </body> </html> OUTPUT
Addition of 10 and 20 : 30 AIM : Write A Program for implementation of Switch case. PROGRAM <html> <head> <title> USING THE SWITCH STATEMENT </title> <script language ="Javascript"> var num=true;var choice,ch,ft,lt,head; choice=window.prompt(" 1. BOLD 2. ITALICS 3. UNDERLINED ......","1"); ch =parseInt(choice); 77
switch(ch) { case 1:
case 2:
case 3:
default : num=false; break; } if(num==true) document.write(head+ft+" <h1>Text </h1>"+lt); else document.write(" <h1>Wrong Choice ! try again."); </script> </head> <body> </body> </html> OUTPUT
78
AIM : Write A Program to find maximum of 3 nos. PROGRAM <html> <head> <title> Maximum of Number </title> <script language ="Javascript"> var a,b,c,a1,b1,c1,max1,max2; a=window.prompt("Enter first number >>","0"); b=window.prompt("Enter second number >>","1"); c=window.prompt("Enter third number >>","2"); a1=parseInt(a); b1=parseInt(b); c1=parseInt(c); max1=Math.max(a,b); max2=Math.max(max1,c); 79
document.write("<br><br><br><center><h1>Maximum number of "+a1+", "+b1+" and "+c1+" is "+max2); </script> <body></body> </html>
80
Output
81
AIM : Write A Program for Entering a password. PROGRAM <html> <head> <title>event clicking</title> <script language="javascript"> function passwordCheck() { var pw=ole.pw.value; var cpw=ole.cpw.value; if(pw!=cpw) window.alert("Re-enter your password"); } </script></head>
<center> <body font size=5 bgcolor="cyan" leftmargin=120 topmargin=120 > <Form id="ole" onSubmit="passwordCheck(this)"> PASSWORD:   ; <input type=password name="pw"size=10 max length=8 > </input> <br><br><br> CONFIRM PASSWORD <input type=password name="cpw"size=10 max length=8 > </input> <br><br><br> <input type="submit" name="sub" value="submit"> <input type="reset" name="rt" value="reset"></center </body> </html>
82
OUTPUT :
83
PROGRAM <html> <head> <title> J Script </title> <script language="javascript"> var value; alert("thambola"); document.writeln("<table border=1 width=50% align=center> "); document.writeln("<caption> Thambola </caption><tr>"); for(var i=1;i<=15;i++) { value=Math.floor(1+Math.random()*100); document.writeln("<td>"+value+"</td>"); if(i%5==0 ) document.writeln("</tr><tr>"); } document.writeln("</tr></table>"); </script> </head> </html>
OUTPUT
<html> <head><u><center><b>RECURSIVE FACTORIAL</b><br></u> <title> factorial </title> <script lang="javascript"> var i,fact,a,n; a=prompt("enter any no","0"); n=parseInt(a); for (i=1;i<=n;i++) { res=fact(i); document.writeln("<br> THE FACTORIAL OF "+i+" IS >> "+res); } function fact(x) { if(x==0 || x==1) return(1); else return(x*fact(x-1)); } </script> </head> </html> OUTPUT
RECURSIVE FACTORIAL THE FACTORIAL OF 1 IS >> 1 THE FACTORIAL OF 2 IS >> 2 THE FACTORIAL OF 3 IS >> 6 THE FACTORIAL OF 4 IS >> 24 THE FACTORIAL OF 5 IS >> 120 AIM : Write A Program for addition of two matrices. PROGRAM <html> <head> <title>Array Addition</title> 85
<script language="javascript"> var a,n,i,j,b,c; a=[[],[]]; b=[[],[]]; c=[[],[]]; n=parseInt(window.prompt("Enter the array size"," ")); window.alert("Enter the elements in the first array"); for (i=0;i<n;i++) { for (j=0;j<n;j++) { a[i][j]=parseInt(window.prompt("Enter the element","")); } } window.alert("Enter the elements in the second array"); for (i=0;i<n;i++) { for (j=0;j<n;j++) { b[i][j]=parseInt(window.prompt("Enter the element","")); } } document.writeln("<br>The elements of Ist Matrix are.."); for (i=0;i<n;i++) { for (j=0;j<n;j++) { document.writeln(a[i][j]); } } document.writeln("<br>The elements of IInd Matrix are.."); for (i=0;i<n;i++) { for (j=0;j<n;j++) { document.writeln(b[i][j]); } } for (i=0;i<n;i++) { for (j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; } } document.writeln("<br>The addition of matrices is:"); for (i=0;i<n;i++) { for (j=0;j<n;j++) { document.writeln(c[i][j]); } } 86
OUTPUT
87
88
The elements of Ist Matrix are : 1 1 1 1 The elements of IInd Matrix are : 1 1 1 1 The addition of matrices is : 2 2 2 2
89
91
92
93
AIM : Write A Program for displaying an image on mouse click. PROGRAM <html> <head> <title>onclick image</title> </head>
<body leftmargin=100 topmargin=100 bgcolor="#111111"> <img name="images" width=200 height=200 src=1.gif "><p> <form><h3> <input type="button" value="image1" onClick="document.images.src='1.gif' "> <input type="button" value="image2" onClick="document.images.src='2.gif' "> <input type="button" value="image3" onClick="document.images.src='3.gif' "> </form></h3></body></html>
94
95
AIM : Write A Program Code To Exhibit Blending Effect. PROGRAM <html> <head> <title> J Script </title> <script language="javascript"> function blendOut() { //textInput.filters("blendTrans").apply(); //textInput.style.visibility="hidden"; textInput.filters("blendTrans").play(); } </script> </head> <body>
<div id="textInput" onmouseover="blendOut()" style="width:100; filter:blendTrans(duration=5)"> <h1> </div> </body> </html> MULTI MEDIA </h1>
96
97
else return 0; }
int second(char *str) { if((str[0]>= '0'&&str[0]<= '9')||(str[0] >='a' &&str[0]<='z')||(str[0] >= 'A' &&str[0] <='Z')) { return(second(str+1)); } else { if(str[0] == 10 ||str[0] == 13) { return(third()); } else { return 0; } } } int third() { return 1; }
OUTPUT:
99
AIM:
main() { int notv=0,count=0,com=0,valid=0,j=0,i,n; char ch[20]; printf("enter the comments:\t"); while((ch[j]=getchar())!='\n') j++; n=strlen(ch); for(i=0;i<=n;i++) { j=i+1; if(ch[i]=='/'&&ch[j]=='*') { com=1; } if(com==1&&ch[i]=='*'&&ch[j]=='/') { valid=1; count++; } } if(com==0) printf("no comments"); else { if(valid==1) { printf("valid comments \n"); printf("no of comments %d",count); } else { printf("invalid comments"); } } getch(); }
OUTPUT :
101
AIM: Write a c program to configure a CFG which will accept the user input string following the given CFG. S -> aBb/ccA A -> b/c 102
B -> a/b
#include<stdio.h> #include<conio.h> int i=0; char s[10]; void S(); void A(); void B(); void disp(); void error(); void main() { clrscr(); printf("Given grammar is \n"); printf("S -> aBb/ccA\n"); printf("A -> b/c\n"); printf("B -> a/b\n"); printf("Enter the string\n"); scanf("%s",&s); S(); if(s[i]==NULL) printf("string is valid\n"); else printf("string is invalid\n"); getch(); } void S() { if(s[i]=='a') { i++; B(); if(s[i]=='b') i++; else error() } else if(s[i]=='c') { i++; if(s[i]=='c') { i++; A(); } else error(); } 103
void A() { if(s[i]=='b'||s[i]=='c') i++; else error(); } void B() { if(s[i]=='a'||s[i]=='b') i++; else error(); } void error() { printf("string is invalid\n"); getch(); exit(0); }
OUTPUT :
104
AIM: 105
Write a C program to count the number of lines & spaces in a input string.
main() { char str[100],ch; int a=0,space= 0,newline = 1; printf("\n enter the string\n"); ch =getche(); while((ch!=27)&&(a<19)) { str[a] =ch; if(str[a] ==' ') space++; if(str[a]==13) { newline++; printf("\n"); } a++; ch = getche(); } printf("\n the number of lines usedis = %d",newline); printf("\n the number of space used is = %d",space); getch(); }
OUTPUT:
106
AIM: Write a C program which will accept the user input and classify them either as a integer or as a floating point. #include<stdio.h> #include<conio.h> 107
#include<string.h> char input[10]; int x, y, z,a, val1, val2, fl1, fl2; int dec, decloc, invfp,floatlit; int inttemp, integer, op; main() { integer = 0; floatlit = 0; fl1=0; fl2=0; val1=0; val2=0; dec=0; decloc=0; invfp=0; inttemp=0; op=0;
printf("Input: "); gets(input); /* For Floating Literals */ for (x=0; x<=strlen(input); x++) { if (input[x]=='.') { dec=dec+1; decloc=x; } } if (dec==1) { for (y=0; y<decloc; y++) { if ((input[y]=='0' || input[y]=='1' || input[y]=='2' || input[y]=='3' || input[y]=='4' || input[y]=='5' || input[y]=='6' || input[y]=='7' || input[y]=='8' || input[y]=='9' || input[y]=='.')) val1=val1+1; } for (z=decloc+1; z<=strlen(input); z++) { if ((input[z]=='0' || input[z]=='1' || input[z]=='2' || input[z]=='3' || input[z]=='4' || input[z]=='5' || input[z]=='6' || input[z]=='7' || input[z]=='8' || input[z]=='9')) val2=val2+1; } } else if (dec>=2) invfp=1; if (val1==decloc) { fl1=1; } 108
for (a=0; a<=strlen(input); a++) { if ((input[a]=='0' || input[a]=='1' || input[a]=='2' || input[a]=='3' || input[a]=='4' || input[a]=='5' || input[a]=='6' || input[a]=='7' || input[a]=='8' || input[a]=='9')) { inttemp=inttemp+1; } } /* End */ if ((fl1==1 && fl2==1 && dec!=0)) { floatlit=1; } if (floatlit==1) printf("Floating Literal"); if ((inttemp==strlen(input) && strlen(input)!=0)) { integer=1; printf("Integer Literal"); } else if ((floatlit==0 && integer==0 )) { if (invfp==0 && input[0]!=0) printf("INVALID"); else if ((invfp==0 && input[0]==0)) printf("Please Enter a Value!"); else if (invfp==1) printf("Invalid Floating Point Literal"); } getch(); } OUTPUT:
109
AIM:
#include<stdio.h> 110
main() { int i,j,flag=notfound,result; char keywords[10][10]= {"void","if","else","for","while","switch"}; char str[10]; printf("\n enter the string"); scanf("%s",str); gets(str); printf("\n string is %s",str); for(i =0;i<6;i++) { printf(" \n keyword is %s", keywords[i]); result= strcmp(&keywords[i][0],str); printf(" \t result is %d",result); if(result==0) { flag = found; printf("\nflag is %d",flag); break; } } if(flag == notfound) printf("\n\n string is not a keyword"); else printf("\n\n string is keyword"); getch(); }
OUTPUT :
111
112