Unit 1 .1
Unit 1 .1
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
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 {
int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms:\n The Fibonacci Sequence: ");
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:
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)
}
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());
}
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;
}
}