for(j=0;j<3 j="j" p="p">
{
System.out.print("
"+d[i][j]);
}
System.out.println();
}
}
}
4. Output
Enter the elements of 1st matrix 1 2 3 4 1 2 3 4 1
Enter the elements of 2nd matrix 2 3 4 5 6 1 2 3 4
The sum of the matrices is
3 5 7
9 7 3
5 7 5
The productof the matrices is
18 24 18
17 24 25
28 36 20
The difference of the matrices is
-1 -1 -1
-1 -5 1
1 1 -3
14. Experiment no. 14: Program To
implement abstract keyword
1. AIM:
Write a java program to implement the abstract keyword
and method overriding.
2. Algorithm:
1.
Start the
program.
2.
Create a classes
and variables with data types.
3.
Assigns the
values of rectangle and triangle.
4.
Create a object
of relevant class, to call the procedure.
5.
Given a formula
of area.
6.
Print the
concatenation of string.
7.
Stop the program.
3. PROGRAM:
abstract
class Figure
{
double dim1,dim2;
Figure(double a,double b)
{
dim1=a;
dim2=b;
}
abstract double area();
}
class
Rectangle extends Figure
{
Rectangle(double a,double b)
{
super(a,b);
}
double area()
{
return(dim1*dim2);
}
}
class
Triangle extends Figure
{
Triangle(double a,double b)
{
super(a,b);
}
double area()
{
return((dim1*dim2)/2);
}
}
class Abstract
{
public
static void main(String args[])
{
Rectangle r=new Rectangle(5,5);
double ar=r.area();
Triangle t=new Triangle(5,5);
double at=r.area();
System.out.println("area
of rectangle " + ar);
System.out.println("area
of triangle " + at );
}
}
4. OUTPUT:
area of rectangle 25
area of triangle 12.5*/
15. Experiment
no. 15: Program to implement constructor OverLoading
1. AIM:
Write a java program to implement the constructor and
overloading method program.
2. Algorithm:
1.
Start the program.
2.
Create a class
and variables with data types.
3.
Declare the
methods in same name with different parameters.
4.
Create a object
to call the procedure.
5.
Print the
concatenation of values.
6.
Stop the program.
3. PROGRAM:
class A
{
int a=2,b=3,c=4;
A(int c1)
{
a=c1;
}
A(int c1,int c2)
{
a=c1;
b=c2;
}
A(int c1,int c2,int c3)
{
a=c1;
b=c2;
c=c3;;
}
void show()
{
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
System.out.println("The value of c is " + c);
}
}
class Consover
{
public static void main(String args[])
{
A a=new A(6);
A a2=new A(7,8);
A a1=new A(9,10,11);
a.show();
a1.show();
a2.show();
}
}
4. OUTPUT:
The value of a is 6
The value of a is 7
The value of b is 8
The value of a is 9
The value of b is 10
The value of c is 11
16. Experiment no. 16: Program To
Implement Dynamic(super class) method dispatch
1. AIM:
Write a java program to implement
the super class and sub class method.
2. Algorithm:
1.
Start the
program.
2.
Create a classes
and variables with data types.
3.
Create a methods
void show ().
4.
Create an object
of relevant class, to call the procedure.
5.
Print the
concatenation of string.
6.
Stop the program.
3. PROGRAM:
import
java.io.*;
import
java.lang.*;
class
A
{
void show()
{
System.out.println("You are in
superclass");
}
}
class
B extends A
{
void show()
{
System.out.println("You are in
subclass");
}
}
class
Dmd
{
public static void main(String
args[])
{
A a; //Creating reference variable
B b=new B(); //creating object for class B
b.show();
a=b; //Assigning object b to a;
a.show();
}
}
4. OUTPUT:
You
are in superclass
You
are in subclass
17.
Experiment no. 17: Declaring and implementing an interface
1. AIM:
Write a java program to implement the Interface
command on multiple class.
2. Algorithm:
1.
Start the
program.
2.
Create a classes
and variables with data types.
3.
Create a command
of interface( ).
4.
Create an object
of relevant class, to call the procedure.
5.
Calculate the
formula in a program.
6.
Print the
concatenation of string.
7.
Stop the program.
3. Program:
interface Pet
{
void speak();
void legs(int x);
String move(String how);
double weight(double x);
}
public class interfaceDemo implements Pet{
public void speak(){
System.out.println("Dog braks");
}
public void legs(int x){
System.out.println("Dog has got"
+x+"legs");
}
public String move(String how_to_ move){
System.out.println("Dog moves on"+how_to_move+"on land);
return"";
}
public double weight(double x){
return x;
}
public static void main(string args[]){
double wt;
InterfaceDemo id= new InterfaceDemo();
id.speak();
id.legs(4);
id.move("four legs");
wt=id.weight(24.58);
System.out.println("Dog weights" +wt +
"kgs");
}
}
4. Output:
Dog barks
Dog has got 4 legs
dog moves on four legs on land
Dog weights 24.58 kgs
18. Experiment
no. 18: Program To
Implement "Inheritance"
1. AIM:
Write a java program to implement the Inheritance
program.
2. Algorithm:
1.
Start the
program, import the packages.
2.
Create a class
and variables with data types.
3.
Declare the
methods in different names in Arithmetic operations.
4.
Arguments give a
throws IOExceptions.
5.
Read a string
with inputstreamReader(System.in).
6.
convert the
string into Integer.parseInt(stdin.readLine());
7.
Create a object
to call the procedure.
8.
Print the
concatenation of string.
9.
Stop the program.
3. PROGRAM:
import
java.io.*;
import
java.lang.*;
class
Add
{
int c;
void add(int a,int b)
{
c=a+b;
System.out.println("Result of
adding is "+ c);
}
}
class
Sub extends Add
{
void sub(int a,int b)
{
c=a-b;
System.out.println("Result of
subtracting is "+ c);
}
}
class
Mul extends Sub
{
void mul(int a,int b)
{
c=a*b;
System.out.println("Result of
multiplying is "+ c);
}
}
class
Inherit
{
public static void main(String
args[]) throws IOException
{
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 2
numbers to perform add,sub and mul");
int i=Integer.parseInt(stdin.readLine());
int
j=Integer.parseInt(stdin.readLine());
Mul m=new Mul();
m.mul(i,j);
m.add(i,j);
m.sub(i,j);
}
}
4. Output
Enter
2 numbers to perform add,sub and mul
10 20
Result
of multiplying is 300
Result
of adding is 30
Result
of subtracting is -10
Enter
2 numbers to perform add,sub and mul
5 10
Result
of multiplying is 50
Result
of adding is 15
Result
of subtracting is -5
Enter
2 numbers to perform add,sub and mul
2 1
Result
of multiplying is 2
Result
of adding is 3
Result
of subtracting is 1
19. Experiment no. 19: Program to show the Use of This Keyword and
Constructor Overloading
1. AIM:
Write a java program to show the use of
This Keyword And Constructor Overloading.
2. Algorithm:
1.
Start the
program, import the packages.
2.
Create a class
and variables with data types.
3.
Declare the
methods in same name with different parameters.
4.
Arguments give a
throws IoExceptions.
5.
Read a string
with inputstreamReader(System.in).
6.
convert the string
into Integer.parseInt(stdin.readLine());
7.
Create a object
to call the procedure.
8.
Print the
concatenation of string.
9.
Stop the program.
3. PROGRAM:
import
java.io.*; //importing io package
import
java.lang.*; //importing Lang package
class
A
{
int Square( int x )
{
int s=x*x;
return(s);
} //End of
constructor Square with int as return type
float Square( float x )
{
float s=x*x;
return(s);
} //End of
constructor Square with float as return type
double Square( double x )
{
double s=x*x;
return(s);
} //End of
constructor Square with double as return type
} //End
of class A
class Methover
{
public static void main(String args[]) throws IOException
{
System.out.println("Enter the
integer number you want to calculate square");
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
int
n=Integer.parseInt(stdin.readLine());
System.out.println("Enter the floating point number you want to
calculate square");
float m=Float.parseFloat(stdin.readLine());
System.out.println("Enter the double data type number uou want to
calculate square");
double
p=Double.parseDouble(stdin.readLine());
A a=new A();
int sq1=a.Square(n);
System.out.println("The square
of integer number is " + sq1);
A a1=new A();
float sq2=a1.Square(m);
System.out.println("The square
of float number is " + sq2);
A a2=new A();
double sq3=a2.Square(p);
System.out.println("The square
of double number is " + sq2);
}
}
4. OUTPUT:
Enter
the integer number you want to calculate square 5
Enter the floating point number you want to
calculate square 2.5
Enter the double data type number uou want to
calculate square 3.44
The
square of integer number is 25
The
square of float number is 6.25
The
square of double number is 11.56
20. Experiment no. 20: Program to invoke constructors using "Super" Keyword
1. AIM:
Write a java program to Invoke
constructors using "Super" Keyword.
2. Algorithm:
1.
Start the
program.
2.
Create a class
and variables with data types.
3.
Declare the
method of void show ();
4.
Print the
concatenation of values.
5.
Stop the program.
3. PROGRAM:
class A
{
int a,b,c;
A(int d1,int d2) //Constructor of class A
{
a=d1;
b=d2;
}
void show()
{
System.out.println("The value of
a is " + a);
System.out.println("The value of
b is " + b);
}
} //End of class A
class B
extends A
{
B(int d1,int d2,int d3) //Constructor of class B
{
super(d1,d2); //super calls constructor A
c=d3;
}
void show()
{
System.out.println("The value of
a is " + a);
System.out.println("The value of
b is " + b);
System.out.println("The value of
c is " + c);
}
} //End of class
B
class
Super1
{
public static void main(String args[])
{
A a=new A(10,20); //Creating object for class A
a.show();
B b=new B(10,20,30); //Creating object for class B
b.show();
}
//End of main
} //End of class super1
4. OUTPUT:
The value
of a is 10
The value
of b is 20
The value
of a is 10
The value
of b is 20
The value
of c is 30*/
21. Experiment
no. 21: Program to show
The Separate Window multiplication
1. AIM:
Write a java program to show the separate window of
multiplication of three numbers.
2. Algorithm:
1.
Start the
program, import the packages.
2.
Create a class
and variables with data types.
3.
Declare the
string() values.
4.
JOptionPane.showInputDialog
command is a window.
5.
JOptionPane.showMessageDialog dialog box.
6.
Print the windows
of out put.
7.
Stop the program.
3. PROGRAM:
import
java.io.*;
import
javax.swing.JOptionPane;
public
class Product
{
public static void main( String args[] )
{
int x; // first number
int y; // second number
int z; //
third number
int result; // product of numbers
String xVal; // first string input by user
String yVal; // second string input by user
String zVal; // third string input by user
xVal = JOptionPane.showInputDialog(
"Enter first integer:" );
yVal = JOptionPane.showInputDialog(
"Enter second integer:" );
zVal = JOptionPane.showInputDialog(
"Enter third integer:" );
x = Integer.parseInt( xVal );
y = Integer.parseInt( yVal );
z = Integer.parseInt( zVal );
result = x * y * z;
JOptionPane.showMessageDialog( null,
"The product is " + result );
System.exit( 0 );
} // end method main
}
// end class Product
4. OUTPUT:
22. Experiment no. 22: Program To show The MULTITHREADING
1. AIM:
Write
a java program to show the multithreading using try and catch()
methods.
2. Algorithm:
1.
Start the
program.
2.
Create a class
and variables User Thread ().
3.
Using try catch
() methods.
4.
For loop
executions.
5.
Print the try statements
and catch () statements.
6.
Print the
concatenation of string.
7.
Stop the program.
3. PROGRAM:
class UserThread extends Thread
{
UserThread()
{
super("UserThread");
System.out.println("it is a UserThread");
start();
}
public void main()
{
try
{
for(int i=10;i>0;i--)
{
System.out.println("Uservalue"+i);
Thread.sleep(500);
}
}
catch(InterruptedException ie)
{
System.out.println("User thread Exception");
}
System.out.println("User thread completed");
}
}
public class
ThreadDemo
{
public static void main(String args[])
{
int i;
UserThread ut=new UserThread();
try
{
for(i=1;i==2;i++)
{
System.out.println("main Thread value is" +i);
Thread.sleep(100);
}
}
catch(InterruptedException ie)
{
System.out.println("main thread interreupted");
}
System.out.println("main thread completed");
}
}
4. OUTPUT:
It is UserThread
Main thread completed
23. Experiment no. 23: Program to implement Packages
1. AIM:
Write a java program to implement Arithmetic
operations the packages.
2. Algorithm:
1.
Start the
program.
2.
Import the Mathc
packages.
3.
Create a classes
and variables with data types.
4.
Create an object
of relevant class, to call the procedure.
5.
Create an object
to call the packages.
6.
Return arithmetic
operations.
7.
Print the
concatenation of string.
8.
Stop the program.
3. PROGRAM:
package Mathc;
public class Arithmetic
{
public int a,b;
public Arithmetic(int p,int q)
{
a=p;
b=q;
}
public int add()
{
return(a+b);
}
public int sub()
{
return(a-b);
}
public int mul()
{
return(a*b);
}
public int div()
{
return(a/b);
}
}
import java.io.*;
import Mypack.*;
class Usepack
{
public static void main(String args[])
throws IOException
{
BufferedReader stdin=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter 2 numbers to perform add,sub and
mul");
int i=Integer.parseInt(stdin.readLine());
int j=Integer.parseInt(stdin.readLine());
Mul m=new Mul();
Addsub a=new Addsub();
a.add(i,j);
m.mul(i,j);
m.add(i,j);
m.sub(i,j);
}
}
4. Output:
Enter 2 numbers to perform add,sub and mul
10
20
Result of multiplying is 300
Result of adding is 30
Result of subtracting is -10
Enter 2 numbers to perform add,sub and mul
5 10
Result of multiplying is 50
Result of adding is 15
Result of subtracting is -5
Enter 2 numbers to perform add,sub and mul
2 1
Result of multiplying is 2
Result of adding is 3
Result of subtracting is 1
24. Experiment no. 24 : Program which imports the package Mathc
1. AIM:
Write a java program to implement Arithmetic
operations the packages.
2. Algorithm:
1.
Start the
program.
2.
Import the Mathc
packages.
3.
Create a classes
and variables with data types.
4.
Create an object
of relevant class, to call the procedure.
5.
Create an object
to call the packages.
6.
Return arithmetic
operations.
7.
Print the
concatenation of string.
8.
Stop the program.
3. PROGRAM:
import Mathc.*;
public class PackDemo
{
public static void main(String args[])
{
Arithmetic a=new Arithmetic(10,30);
System.out.println(a.add());
System.out.println(a.sub());
System.out.println(a.mul());
System.out.println(a.div());
}
}
4. OUTPUT:
40
-20
300
0
25. Experiment no. 25 : Program to print JAVA IS SIMPLE in
different styles and fonts
1. AIM:
Write a java program to implement the APPLET PACKAGES
programs.
2.
Algorithm:
1.
Start the
program.
2.
Import the
packages of applet,awt.
3.
Create a classes
public void paint(Graphics g).
4.
Assume the values
of string, color and font.
5.
g.drawString()
application of GUI.
6.
Printing in the
separated Applet viewer window.
7.
Stop the program.
3. PROGRAM:
import java.awt.*;
import java.applet.*;
import javax.swing.*;
3>