0% found this document useful (0 votes)
13 views8 pages

Unit 1 .1

Internet programming

Uploaded by

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

Unit 1 .1

Internet programming

Uploaded by

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

Date of Exam: Session:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


I UNIT TEST

Academic Year : 2018-2019 Class & Branch : III CSE A & B


Subject Code : CS6501 Subject Name : Internet Programming
Course Code (as per NBA) : C302 Total Marks : 50 Marks
Date : Time : 90 minutes
Staff Name : Mrs.T.Mahara Jothi & Mr.B.MuthukrishnaVinayagam AP/CSE
CO Covered in Cycle Test

Blooms
Marks
CO Code CO Statement Taxonomy
Level allotted

C302.1 Apply Java programming constructs to solve real time problems K3 C302.1

Blooms CO
Q. No: Questions Taxonomy Mapping
Level
Answer all the Questions :- Part A (5 x 2 = 10 Marks)
1 List out the features of Java K1 C302.1
2 Explain the different types of type casting in Java with examples K2 C302.1
3 Write a java program to find the Fibonacci series of the given number K2 C302.1
4 How to create an inner class object? give an example K3 C302.1
5 Write a multithreaded program that joins two threads. K1 C302.1
Answer all the Questions:- Part B (2 x 13 = 26 marks)
6.(a) Explain the types of constructors with examples. Illustrate how final- K2 C302.1
ize() method is used with suitable code snippet.
(OR)
6.(b) Explain in detail interfaces in java with suitable examples K2 C302.1
7.(a) Explain about the applet life cycle? How applets can be created? Write a K2 C302.1
java program to draw a smiley using applets
(OR)
7.(b) Explain different categories of the exception handling with example K2 C302.1
Answer all the Questions:- Part C (1 x 14 = 14 marks)
8 (a) Create a java class Shape, with constructor to initialize the one parameter K2 C302.1
“dimension”. Then Create three sub classes with following methods:
 “Circle” with the methods to calculate the area and circumfer-
ence of the circle with the given radius
 “Square” with the methods to calculate the area
 “Sphere” with the methods to calculate the volume and surface
area of the sphere

8(b) Write a java program to perform following operations on the matrix K2 C302.1
 Find the row and column sum
 Transpose of the matrix
Key:
1) List out the features of Java
a) Simple:Easy to learn, syntax are very similar to c++.
b) Object Oriented: In Java everything is treated as an object and easily extended.
c) Secure: It enables to develop virus or tamper free software.
d) Network savvy: Network Programming is easy (Extensive network libraries)
e) Robust: Eliminating error or bug in compiler and run time checking
f) High performance: is achieved by Just In Time Compiler(on the fly compilation)
g) Multi-Threading: Can do many task simultaneously

2) Explain the different types of type casting in Java with examples


# TYPE CASTING: changing of one data type to another type.

o Implicit Conversion: changing lower data type to higher data type is known as widening or implicit
conversion or automatic conversion.
int r=5;
double x=3.14 *r * r;

o Explicit Conversion: changing higher data type to lower data type is known as narrowing or explicit
conversion.
 Variable = (Cast DataType) variable;
 Ex: double r=7.553;
 int x=(int) r;
3) Write a java program to find the Fibonacci series of the given number
public class Fibonacci {

public static void main(String[] args) {

int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms:\n The Fibonacci Sequence: ");

for (int i = 1; i <= n; ++i)


{
System.out.print(t1 + " , ");

int sum = t1 + t2;


t1 = t2;
t2 = sum;
}
}
}
When you run the program, the output will be:
First 10 terms:
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21,34

4) How to create an inner class object? give an example


# Syntax rule for creating inner class object:
OuterClass.InnerClass innerObj=new OuterClass.InnerClass();
(or)
OuterClass outerObj=new OuterClass();
InnerClass innerObj=outerObj.new InnerClass();

5) Write a multithreaded program that joins two threads.


join method() waits for a thread to die. It cause the currently thread to stop executing until the thread it joins
with completes its task.

class A implements Runnable


{
public void run()
{
for(int i=1;i<=50;i++)
System.out.pritln("A:"+i);
System.out.pritln("Exit from A");
}
}
class B extends Thread{
public void run(){
for(int i=1;i<=50;i++)
System.out.pritln("B:"+i);
System.out.pritln("Exit from B");
}
}
class MainClass
{
public static void main(String args[])
{
Runnable r=new A();
Thread t1=new Thread(r);
B t2=new B();
t1.start();
t1.join(); // first completes thread A
t2.start();
}
}

6) A. Explain the types of constructors with examples. Illustrate how finalize() method is used with suitable code snip -
pet.
Characteristic of constructor:
• A constructor has the same name as the class.
• A class can have m0ore than one constructor.
• A constructor can take zero, one, or more parameters.
• A constructor has no return value.
• A constructor is always called with the new operator.
Three way of initialization:
Initialization through constructor:
Default constructor
Classname obj=new classname();
Parameter constructor
Classname obj1=new classname(10,102.54);
Constructors call another constructor
Classname()
{
this(“s”); // must be first line of constructor

}
Example
class ConsEx
{
String name;
double salary;

ConsEx()//default
{
this("vinu",1000.0);
System.out.println("Default Constructor");
}
ConsEx(String aname,double asalary)
{
this.name=aname;
this.salary=asalary;
}
public static void main(String a1[])
{
ConsEx a=new ConsEx();
System.out.println("Main Method object values are.............");
System.out.println("The object of value a is " + a.name + "&its Salary"+a.salary);
}
}
b) Explain in detail interfaces in java with suitable examples
INTERFACE
#Def: It is consist of method declartion. That is methods have no body statements that must be overridden in subclass.
 It contains set of requirements for a class
 It informs only what to do?
 The information about How to do? Is placed inside a class which is implementing the interface.
#Syntax:
interface InterfaceName
{
fields initialization;
methods declaration();
}
 No Constructor can be used in interface.
 To achieve multiple inheritance; we use interface concept.
 Fields in the interface default final.
 Methods in the interface default abstract methods.
#Syntax:
class ClassName implements InterfaceName
{
... method must be overridden.
}
# Example:
interface Area
{
public static final double PI=3.14;
public abstract void compute(double a , double b );
}
class Rectangle implements Area{
public void compute(double a, double b)
{
System.out.println("The Rectangle Area is:"+ a*b);
}
}
class Triangle implements Area
{
public void compute(double a, double b)
{
System.out.println("The triangle Area is:"+ 0.5*a*b);
}
}
class Volume implements Area
{
public void compute(double r, double h)
{
System.out.println("The volume of cylinder is:"+ Math.PI*r*r*h);
}
}
class MainClass
{
public static void main(String arg[])
{
Rectangle r=new Rectangle();
r.compute(10,20);
Triangle t=new Triangle();
t.compute(5,15);
Volume v=new Volume();
t.compute(8,10);
}
}
 Interface doesn’t have object but it stores reference type.
 The overriding method tag with public access specifier otherwise compiler informs weaker access specifier
#Extending Interface:
interface InterfaceName1
{
fields initialization;
methods declaration();
}
interface InterfaceName2 extends InterfaceName1
{
fields initialization;
methods declaration();
}
class ClassName implements InterfaceName
{
... methods must be overridden.
}
Program
interface IF1
{
void add(int, int);
}
interface IF2 extends IF1
{
void sub(int, int);
}
class Calculation implements IF2{
public void add(int x,int y){
System.out.println("The Addition is:"+(x+y));
}
public void sub(int x,int y){
System.out.println("The Subtraction is:"+(x-y));
}
public static void main(String arg[]){
Calculation obj=new Calculation();
obj.add(10,20);
obj.sub(50,25);
}
}
# Multiple Inheritance:

Super class Interface1 Interface2

7. a. Explain about the applet life cycle? How applets can be created? Write a java program to draw a smiley
using applets
/* Appletextends
program in java */ implements
 No need of main method Subclass
 Life cycle of applet is init(), start(), paint(), stop() and destroy()
Program.
import java.awt.*;
import java.applet.*;
public class AppletProg extends Applet
{
public void paint(Graphics g)
{
g.drawString("Smiley",20,20);
g.drawOval(50,50,150,150);
g.fillOval(75,75,10,10);
g.fillOval(175,75,10,10);
g.drawArc(100,100,50,50,0,-180);

}
/*
<applet code="AppletProg.class" width="100" height="200">
</applet>
*/
}
7. b. Explain different categories of the exception handling with example
Exceptions are such anomalous conditions (or typically an event) which changes the normal flow of execution of a
program. Exceptions are used for signaling erroneous (exceptional) conditions which occur during the run time
processing. Exceptions may occur in any programming language.

 What is an exception?
o Undesirable control flow, unexcepted error, abnormal program condition, unusal behaviour of
the program an improper termination that leads to program crash.
 Need of Exception handling
o Notify the user error, save their work and allow user to graceful exit and seek robust program.
o Suppose programmer doesn’t handle the exception, then Operating System or Kernal takes the
responsibility and kills the application program that is improper exit.
 Types of Error:
o Syntax Error: (This error because of user doesn’t follow the syntax rules of the language.
those errors are intimated by compiler during compilation.
o Logical Error: (this is because of programmers careless mistake in the logical of the program.
For an example we want to find sum of two numbers but result is shown multiplication of these numbers.
o Runtime Error: due to incorrect or wrong data is given by user.
o Device Error: (due to improper device connection ).
o Physical limitation: lack of memory capacity.
 Example: Compiler informs the error message just like that
o ClassName.MethodName (FileName:LineNumber)

 There are two types of exception;


1. Checked Exception: These are the exceptions which occur during the compile time of the program. The com-
piler checks at the compile time that whether the program contains handlers for checked exceptions or not. For
this an exmples are IOException, FileNotFoundException., NoSuchFieldException, InstantiationException, Ille-
galAccessException, ClassNotFoundException, NoSuchMethodException and CloneNotSupportedException
2. Unchecked Exception: Unchecked exceptions are the exceptions which occur during the runtime of the program.
Compiler doesn’t catch the exception during compile. Example exception are RuntimeException , Error, Index-
OutOfBoundsException, ArrayIndexOutOfBoundsException, ClassCastException, ArithmeticException, Null-
PointerException, IllegalStateException and SecurityException

 Simple Exception handling:


A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed
around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and
the syntax for using try/catch looks like the following
Syntax:
try
{
//Protected code
}catch(ExceptionName e1)
{
//Catch block to handle exception or remedial of action
}

Example program for ArrayIndexOutofBoundsException

// File Name : ExcepTest.java


import java.io.*;
public class ExcepTest{
public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}

Program for Divide by zero Exception:


class ExceptionSample
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the numerator and denominator:");
int num=in.nextInt();
int deno=in.nextInt();
try
{
System.out.println("The Division of Numerator by Denominator is:" num/deno);
}
catch(ArithmethicException e)
{
System.out.println(e.getMessage());//getMessage that informs compiler error
or
System.out.println("Denominator cannot be Zero");
}
}
}
8. a.
Create a java class Shape, with constructor to initialize the one parameter “dimension”. Then Create three sub classes with
following methods:
 “Circle” with the methods to calculate the area and circumference of the circle with the given radius
 “Square” with the methods to calculate the area
 “Sphere” with the methods to calculate the volume and surface area of the sphere

// Using abstract methods and classes.


abstract class Figure {
double dim1;
Figure(double a) {
dim1 = a;
}
// area is now an abstract method
abstract double area();
}
class Circle extends Figure {
Circle(double ) {
super(a, b);
}
// override area for rectangle
double area() {
System.out.println("Inside Area for Circle.");
return 3.14*dim1*dim2;
}
double circumference(){
return 2*3.14*dim1;
}
}
class Square extends Figure {
Square(double a) {
super(a);
}
// override area
double area() {
System.out.println("Inside Area for square.");
return dim1 * dim2;
}
}
class Sphere extends Figure {
Sphere(double a) {
super(a);
}
// override area
double area() {
System.out.println("Inside Area for sphere.");
return 4*3.14*dim1*dim1 ;
}
double volume() {
System.out.println("Inside Volume for sphere.");
return 4/3*3.14*dim1*dim1*dim1 ;
}

}
class AbstractAreas {
public static void main(String args[]) {
Circle c = new Circle (9);
System.out.println("Area is " +c.area());
System.out.println("Circumference is " +c.circumference());

Sphere s = new Sphere (5);


System.out.println("Area is " +s.area());
System.out.println("Circumference is " +s.circumference());

Square sq=new Square(6);


System.out.println("Area is " + sq.area());
}
}
(Or)
8.b. Write a java program to perform following operations on the matrix
 Find the row and column sum
 Transpose of the matrix

public class MatrixTransposeExample{


public static void main(String args[]){
//creating a matrix
int original[][]={{1,3,4},{2,4,3},{3,4,5}};
//creating another matrix to store transpose of a matrix
int transpose[][]=new int[3][3]; //3 rows and 3 columns
//Code to transpose a matrix
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
transpose[i][j]=original[j][i];
}
}
System.out.println(Arrays.deepToString(transpose));
System.out.println(" Row Sum is " +sumRow(original,2));
System.out.println(" Column Sum is " +sumCol(original,2));

}
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix[row].length; i++)
{
sum += matrix[row][i];
}
return sum;
}
public int sumCol(int[][] matrix, int col)
{
int sum = 0;
for(int i = 0; i < matrix[col].length; i++)
{
sum += matrix[i][col];
}
return sum;
}
}

You might also like