Sunday, September 3, 2017

5. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication14
{
    class Program
    {
        public static void Main(string[] args)
       {
            try
            {
           
                int zero = 0;
                Console.WriteLine("In try block: attempting division by zero");
                int myInt = 1 / zero;
                Console.WriteLine("You never see this message!");
            }
            catch
            {
             
                Console.WriteLine("In catch block: an exception was thrown");
            }
            finally
            {
                Console.WriteLine("In finally block: do any cleaning up here");
                Console.ReadKey();
            }

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplicationavi11
{
    class Lab11
    {
        public static void Main()
        {
            try
            {
                int zero = 0;
                Console.WriteLine("In try block: attempting division by zero");
                int myInt = 1 / zero;
                Console.WriteLine("You never see this message!");
            }
            catch
            {
                Console.WriteLine("In catch block:   an exception was thrown");
            }
            finally
            {
                Console.WriteLine("In finally block:   do any cleaning up here");
            }
            Console.ReadLine();
        }
    }
}

4. Find the sum of all the elements present in a jagged array of 3 inner arrays.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace lab4
{
    public class prgm4
    {
        public static void Main()
        {
            int[][] jag = new int[3][];
            jag[0] = new int[2];
            jag[1] = new int[4];
            jag[2] = new int[3];
            int[] sum = new int[3];
            int result = 0;
            int i, j;
            for (i = 0; i < 3; i++)
                sum[i] = 0;
            Console.WriteLine("Enter the size of the array ");
            for (i = 0; i < jag.Length; i++)
            {
                Console.WriteLine("Enter the values off array " + (i + 1) + " jag:");
                for (j = 0; j < jag[i].Length; j++)
                {
                    jag[i][j] = int.Parse(Console.ReadLine());
                }
            }
            for (i = 0; i < jag.Length; i++)
            {
                for (j = 0; j < jag[i].Length; j++)
                {
                    sum[i] = sum[i] + jag[i][j];
                }
            }
            for (i = 0; i < 3; i++)
                result = result + sum[i];
            Console.WriteLine("The result of individual jags row-wise: ");
            for (i = 0; i < 3; i++)
                Console.Write(sum[i] + "\t");
            Console.WriteLine();
            Console.WriteLine("The final sum of all the elements in the jagged array:" + result);
            Console.Read();
        }
    }
}

Tuesday, August 29, 2017

3. Write a program to demonstrate Operator overloading.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace program
{
 class OpOver
{
int x, y, z; // 3-D coordinates
public OpOver() { x = y = z = 0; }
public OpOver(int i, int j, int k) { x = i; y = j; z = k; }
// Overload binary + for object + object.
public static OpOver operator +(OpOver op1, OpOver op2)
{
OpOver result = new OpOver();
 /* This adds together the coordinates of the two points  and returns the result. */
result.x = op1.x + op2.x;
result.y = op1.y + op2.y;
result.z = op1.z + op2.z;
return result;
}
// Overload binary + for object + int.
public static OpOver operator +(OpOver op1, int op2)
{
OpOver result = new OpOver();
result.x = op1.x + op2;
result.y = op1.y + op2;
result.z = op1.z + op2;
return result;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}
public class Prog3
{
public static void Main()
{
OpOver a = new OpOver(1, 2, 3);
OpOver b = new OpOver(10, 10, 10);
OpOver c = new OpOver();
Console.Write("Here is a: ");
a.show();
Console.WriteLine();
Console.Write("Here is b: ");
b.show();
Console.WriteLine();
c = a + b; // object + object
Console.Write("Result of a + b: ");
c.show();
Console.WriteLine();
c = b + 10; // object + int
Console.Write("Result of b + 10: ");
c.show();
Console.Read();
}
}
}

2. Write a Program in C# to demonstrate boxing and Unboxing.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace boxing
{
class Program
{
static void Main(string[] args)
{
int m = 10;
object a = m;
try
{
Console.WriteLine("Value of m is :" + a);
object n = 20;
int b = (int)n;
Console.WriteLine("Value of n is :" + b);
System.Console.WriteLine("Boxing and unboxing is done");
Console.ReadLine();
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("Error Incorrect unboxing" + e.Message);
}
}
}
}
//converting from value type to reference type is known as BOXING
//converting from reference type to value type is known as UNBOXING

1. Write a Program in C# to demonstrate Command line arguments processing.

 namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
 {
  System.Console.WriteLine("\nNumber of command line arguments : " + args.Length);
  System.Console.WriteLine("Command line arguments are : \t");
  for (int i = 0; i < args.Length; i++)
  {
  System.Console.WriteLine(args[i]+"\t");
 }
 System.Console.ReadKey(); //system is built in package
  }      
  }
}
 //writeline -- curson will move on to nextline || write -- execte in  current line
//readline --  || readkey --
// try to do without any argument in string and in Main M must be capital

Sunday, July 9, 2017

4. Build a Rails application to accept book information viz. accession number, title, authors, edition and publisher from a web page and store the information in a database and to search for a book with the title specified by the user and to display the search results with proper headings.

After Installing rails application, you have to start the servers both Apache and MySql when you open exe file of rails application, the servers are automatically started

Open a ruby console window

C :\Rails\rails_apps>

Type the following command
Creating Books Database
> mysql -u root

This will now show you mysql> prompt

At mysql prompt type the following

>create database book_development;

>use book_development;

>create table books(id int not null auto_increment,accno varchar(40) not null,title varchar(60) not null,author varchar(60) not null,edition varchar(60) not null,publisher varchar(5) not null,primary key(id));

Creating book Project

Now open another console window type the following command

>rails -d mysql book
>cd book

Creating controller, model and view from database

>ruby script/generate scaffold Book accno:string title:string author:string
edition:string publisher:string

Starting rails server
>ruby script/server

Executing in the web browser
http://localhost:3000/books

Creating main (new) controller for searching book
>ruby script/generate controller main

a controller with a name main will be created..
Opening main controller program
>edit app\controllers\main_controller.rb

Create two views welcome and result

class MainController < ApplicationController
def welcome
@num_books = Book.count
end
def result
@titlename = params[:title]
@bookz = Book.find(:all, :conditions => "title = #{@titlename}")
end
end

Create Result view file
>notepad app\views\main\result.rhtml
<html>
<title> Welcome template for books </title>
<body>
<p> Entered book title is <%= @titlename %> </p>
<table border=1>
<tr><th>Book Id</th> <th>Accesion Number</th><th>Book
Title</th><th>Author</th> <th>Edition </th> <th>Publisher</th></tr>
<% @names.each do |bk|
@id = bk.id
@accno = bk.accno
@title = bk.title
@author = bk.author
@edition = bk.edition
@publisher = bk.publisher%>
<tr>
<td> <%= @id %></td>
<td><%= @accno %> </td>
<td><%= @title %></td>
<td> <%= @author %></td>
<td><%= @edition %></td>
<td> <%= @publisher %></td>
</tr>
<% end %>
</table>
</form></body></html>

Create Welcome view file

>notepad app\views\main\welcome.rhtml
<html>
<title> Welcome template for books </title>
<body>
<p> Total number of books = <%= @num_books %> </p>
<form action = "result" >
Enter Searching Element: <input type="text" name="title" />
<input type=submit value="Search" />
</form></body></html>

Starting Rails Server
ruby script/server

Executing in the web browser
http://localhost:3000/main/welcome

3.Write a PHP program to read student data from an XML file and store into the MySQL database. Retrieve and display using SEARCH function.

lab3.xml

<?xml version="1.0" ?>
<student>
<stud>
<name>BBBB</name>
<usn>1234</usn>
</stud>
<stud>
<name>CCCC</name>
<usn>4567</usn>
</stud>
</student>

lab3.html

<html>
<body>
<center>
<table width="50%" border="2"><tr>
<form action="http://localhost/lab3.php">
<tr><th> ENTER NAME TO SEARCH </th></tr><br/>
<td align="center"><br/>Name: <input type="text" name="name"><br /><br/>
submit: <input type="submit"><br/><br/></td>
</form></tr>
</body>
</html>

lab3.php

<?php
$link=mysqli_connect("localhost","root","","test");
if(!$link)
{
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$name=$_GET['name'];
$studs=simplexml_load_file("http://localhost/lab3.xml");
foreach($studs as $stud)
{
$name = $stud->name;
$usn = $stud->usn;
mysqli_query($link,"insert into stud3 values('$name','$usn')");
}
$name=$_GET['name'];
$res=mysqli_query($link,"select * from stud3 where name='$name'");
if(mysqli_num_rows($res)==0)
{
echo "$name doesn't exist in the table";
}
else
{
echo "<table border='1' align=center width=60%>";
echo "<tr><td width=15% align=center>NAME</td><td width=15%
align=center>USN</td></tr>";
while($arr=mysqli_fetch_row($res))
echo "<tr><td>$arr[0]</td><td>$arr[1]</td></tr>";
echo "</table>";
}
?>