CSD Java Lab Manual (2)
CSD Java Lab Manual (2)
1.a. write a java program to implement class mechanism and create object to access the
members of class.
import java.io.*;
class Addition
{
int sum = 0;
class GFG
{
public static void main (String[ ] args)
{
}
}
Output :
Sum of two integer values : 3
int a = 5;
int b = -10;
Output :
a<<2 = 20
b>>2 = -3
b>>>2 = 1073741821
2. a. Write a Java program to illustrate Type Casting of the datatype and type
conversion.
class Conversion
byte b;
int i = 257;
b = (byte) i;
i = (int) d;
Output:
2.b. write a java program to implement for-each loop to compute average of n natural
numbers.
Class ForEach
Intnums [ ] = { 1, 2, 3, 4 , 5 , 6, 7, 8, 9, 10 };
Int sum=0;
For(int x : nums )
System.out.println(“value is : “+ x);
Sum += x;
System.out.println(“summation: “+sum);
Output:Value is : 1
Value is : 2
Value is : 3
Value is : 4
Value is : 5
Value is : 6
Value is : 7
Value is : 8
Value is : 9
Value is : 10
Summation : 55
class MethodOverloadingExample
return x + y;
return x + y;
Output:
addition of integers: 16
OverloadingExample2()
rollNum =100;
OverloadingExample2(int rnum)
this();
*/
return rollNum;
this.rollNum = rollNum;
System.out.println(obj.getRollNum());
Output:
112
Class name
String name=”swathi”;
Int m1=30,m2=30,m3=30;
Int total;
Void calc(_
Total=m1+m2+m3;
Void show()
Class Multilevelinheritance
Ob.calc();
Ob.show( );
Output:
NAME: swathi
AGE:20
MARK1=30
MARK2=30
MARK3=30
TOTTAL:90
4.b . Write a Java program to implement method overriding that shows use of super
keyword.
class GFG
System.out.println("Int Parameter");
System.out.println("String Parameter");
// Main Class
g.m1("A");
Output :
String Parameter
5.a. Write a Java program to illustrate Dynamic Method Dispatch using hierarchical
inheritance
class A
void m1( )
class B extends A
// overriding m1( )
void m1( )
class C extends A
// overriding m1( )
void m1( )
// Driver class
class Dispatch
// object of type A
A a = new A();
// object of type B
B b = new B();
// object of type C
C c = new C();
A ref;
ref = a;
ref.m1();
ref =8 b;
ref.m1();
ref = c;
ref.m1();
5.b.Write a Java program for abstract class to find areas of different shapes.
import java.util.*;
int a,b;
a=s.nextInt( );
b=s.nextInt( );
area_rect=a*b;
double area_tri;
a=s.nextInt( );
b=s.nextInt( );
area_tri=(0.5*a*b);
double area_circle;
a=s.nextInt( );
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
r.printarea();
t.printarea();
r1.printarea( );
interface A
void funcA( );
interface B extends A
void funcB( );
class C implements B
System.out.println("This is funcA");
System.out.println("This is funcB");
obj.funcA();
obj.funcB();
Output
This is funcA
This is funcB
importjava.lang.*;
class Error2
{
public static void main(String args[ ])
{
int a=10;
int b=5;
int c=5;
intx,y;
try
{
x=a/(b-c);
}
catch (ArithmeticException e)
{
System.out.println("division by zero");
}
y=a/(b+c);
System.out.println("y=" +y);
}
}
Output:
C:\javaprg>javac Error2.java
2 errors
C:\javaprg>javac Error2.java
C:\javaprg>java Error2
Division by zero
y=1
class BreakandContinue
System.out.println("Break Statement\n....................");
for(inti=1;i<=5;i++)
if(i==4) break;
System.out.println(i);
System.out.println("Continue Statement\n....................");
for(inti=1;i<=5;i++)
if(i==1) continue;
System.out.println(i);
Output:
Break statement
…………………………………
Continue statement
……………………….
Process finished
Output:
SACHIN
sachin
Sachin
Thread t;
Frst()
t=new Thread(this);
System.out.println("Good Morning");
t.start();
for(inti=0;i<10;i++)
System.out.println("Good Morning"+i);
try
t.sleep(1000);
catch(Exception e)
System.out.println(e);
Thread t;
sec( )
t=new Thread(this);
System.out.println("hello");
t.start();
for(inti=0;i<10;i++)
System.out.println("hello"+i);
try
t.sleep(2000);
catch(Exception e)
System.out.println(e);
Thread t;
third( )
t=new Thread(this);
System.out.println("welcome");
t.start( );
for(int i=0;i<10;i++)
System.out.println("welcome"+i);
try
t.sleep(3000);
catch(Exception e)
System.out.println(e);
newFrst( );
new sec( );
new third( );
Output:
Good morning
Hello
Welcome
Hello 0
Welcome 0
Good morning 0
Good morning 1
Hello 1
Good morning 2
Welcome 1
Good morning 3
Hello 2
import java.util.LinkedList;
public class Threadexample
{
public static void main(String[ ] args)
throwsInterruptedException
{
// Object of a class that has both produce( )
// and consume( ) methods
final PC pc = new PC( );
{
e.printStackTrace();
}
}
});
t1.start( );
t2.start( );
// t1 finishes before t2
t1.join( );
t2.join( );
}
// and sleep
Thread.sleep(1000);
}
}
}
}
}
Output:
Break statement
……………………….
Continue statement
…………………………
5 Process finished