JAVA Lab Manual
JAVA Lab Manual
QuadraticEquationExample1
double a = input.nextDouble();
double b = input.nextDouble();
else if (d == 0.0)
else
}
Output 1:
Output 2:
Class fibonacci
System.out.println(n1+''\t''+n3) for(i=2;i<n;i++)
n3=n1+n2;
System.out.println(''\t''+n3);
n1=n2; n2=n3;
Output:
5
3.Implement a java program to multiply two given matrices?
a[][]={{1,1,1},{2,2,2},{3,3,3}}; int
b[][]={{1,1,1},{2,2,2},{3,3,3}};
for(int k=0;k<3;k++)
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
}//end of j loop
System.out.println();//new line
}}
Output:
666
12 12 12
18 18 18
4.Implement a Java program to check whether the given string is palindrome or not.
palindrome
System.out.print/n(''Enter a string'');
i>=0;i--) rev=rev+str.charAt(i);
if(Str.equals(rev))
System.out.println(str+'' is a
Output:
Enter a string
HANNAH
HANNAH is a palindrome
5.Implement a java program for finding the total numbers of objects created of a class.
Number_Objects()
count++;
Output:
class StaticDemo
display(int x)
static
b = a * 4;
class StaticKeyword
StaticDemo.display(42);
Output:
x = 42 a = 3 b = 12
7.Implement a java program that illustrates simple inheritance and multi-level inheritance
Simple inheritance:
class A
class B extends A
class SimpleInheritance
local method
}
Output:
Multi-level inheritance:
class X
System.out.println("class X method");
class Y extends X
System.out.println("class Y method");
class Z extends Y
System.out.println("class Z method");
class T
method
Output:
Y method class Z
method
9. Implement a java program demonstrating the difference between methodoverloading,
constructor overloading and method overriding.
Method Overloading:
class A
System.out.println("sum is"+(a+b)) ;
System.out.println("sum is"+(a+b));
Cricketer
String name;
{
name ="";
= 0;
name = n; team =
t; age = a;
= ckt.team; age =
ckt.age;
class A
System.out.println(c2);
System.out.println(c3);
c1.name = "Virat";
c1.team= "India"; c1.age
= 32;
Output:
virat of india
Animal
class MethodOverriding
Output:
class Animal
String color="white";
printColor()
class TestSuper
d.printColor();
Output:
black white
11. Implement a java program to give a simple example for abstract class abstract
class Shape
//In real scenario, implementation is provided by others i.e. unknown by end user class
void draw()
System.out.println("drawing rectangle");
void draw()
System.out.println("drawing circle");
TestAbstraction1
//In real scenario, object is provided through method e.g. getShape() method s.draw();
}
Output:
drawing circle
12. Implement a java program illustrating multiple inheritance using interfaces.
interface Printable
interface Showable
void show();
System.out.println("Hello");
System.out.println("Welcome");
class MultipleInher
{
obj.print(); obj.show();
Program 1: package mypack; public class A { public void area() { int radius=5; double area=3.14*5*5;
System.out.println("Area of circle is"+area); } }
Program 2:
package qis; import mypack.*; class Circle { public static void main(String args[]) { A obj = new A();
obj.area(); } }
Output:
14 Implement a java program for example of try, catch and finally block. In this check whether
the given array size is negative or not.
NegArraySize
i=0;i<arr.length;i++)
}
System.out.println("Elements of Array"); for(int
i=0;i<arr.length;i++)
System.out.println(arr[i]);
catch(NegativeArraySizeException e)
} finally
Output1:
4 enter 0 th
element 2
enter 1 th element
2 enter 2 th
element
1 enter 3 th
element
Elements of Array
3
Statements of finally block gets executed irrespective of exception
Output2:
-4
class JavaException
try
catch(MyException e)
System.out.println(e) ;
int a;
MyException(int b)
a=b;
for(int i=1;i<=4;i++)
System.out.println(''My Thread1:''+i);
for(int i=1;i<=4.i++)
System.out.println(''My Thread2:''+i);
t1. start();
t2. start();
}
Output:
Output may be charged from run to run. Because both threads executes simultaneously
17.Implement a Java program correctly implements producer consumer problem using the concept
of inter thread communication.
int total;
synchronized(this)
for(int i=1;i<=100;i++)
total = total+i;
this. notify();
t1. start();
synchronized (t1)
Output:
Total is :5050.
18.implement a java program to add and display the group of objects using array?
1:"); obj[0].display();
Product
int pro_Id;
String pro_name;
pro_Id = pid;
pro_name = n;
System.out.println();
Output:
Product Object 1:
Object 2:
Object 3:
Product Object 4:
Product Object 5:
import java.io.*;
import java.util.*;
class Wordcount
{
public static void main(String args[]) throws Exception
{
FileInputStream fis=new FileInputStream("Wordcount .java");
LineNumberReader lnr=new LineNumberReader(new InputStreamReader(fis));
String data;
int chars=0,words=0;
while((data=lnr.readLine())!=null)
{
StringTokenizer st =new StringTokenizer(data);
chars=chars+ data.length();
words=words+st.countTokens();
}
}
}
Output
20. Implement a java program for creation of buttons and labels.
Program 1
/*
Create AWT Button Example
This java example shows how to create a Button using AWT Button class.
*/
import java.applet.Applet;
import java.awt.Button;
/*
<applet code="CreateAWTButtonExample" width=200 height=200>
</applet>
*/
/*
* To create a button use
* Button() constructor.
*/
/*
* Set button caption or label using
* void setLabel(String text)
* method of AWT Button class.
*/
/*
* To create button with the caption use
* Button(String text) constructor of
* AWT Button class.
*/
Example Output
Program 2
/*
Create AWT Label Example
This java example shows how to create a label using AWT Label class.
*/
import java.applet.Applet;
import java.awt.Label;
/*
<applet code="CreateLabel" width=200 height=200>
</applet>
*/
public class CreateLabel extends Applet{
/*
* To create a new label use
* Label() constructor of AWT Label class.
*/
/*
* To set label text use,
* void setText(String text) method of AWT Label class
*/
/*
* To create label with the text use
* Label(String text) constructor.
*/
/*
* To add label use
* void add(Component c) method.
*/
add(label1);
add(label2);
}
}
Example Output
21 .Implement a java program to create a border layout control and grid layout control.
Border Layout:
import java.awt.*;
public class Border1 extends Frame
{
public Border1()
{
super ("Border Layout 1");
setFont (new Font ("Helvetica", Font. PLAIN, 16 ) );
setLayout (new BorderLayout (5, 5));
add ( new Button ("Button 1"),BorderLayout.WEST);
add ( new Button ("Button 2"),BorderLayout.NORTH);
add ( new Button ("Button 3"),BorderLayout.EAST);
add ( new Button ("Button 4"),BorderLayout.SOUTH);
add ( new Button ("Button 5" ),BorderLayout.CENTER);
setBounds(100, 100, 300, 200);
setVisible(true);
}
Grid Layout:
import java.awt.*;
public class Grid1 extends Frame
{
public Grid1()
{
super(" Grid Layout 1");
setLayout (new GridLayout (3, 3, 30, 5) );
add (new Button ("Button 1") );
add (new Button ("Button 2") );
add (new Button ("Button 3") );
add (new Button ("Button 4") );
add (new Button ("Button 5") );
setBounds(100,100,200,100);
setVisible (true);
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="Appletdemo.class" height=300 width=300>
</applet>
*/
else if (s.equals("C"))
{
no1 = no2 = 0;
txt = "";
t1.setText("");
}
else if (s.equals("="))
{
no2 = Integer.parseInt(t1.getText());
if (oprt.equals("+"))
t1.setText((no1 + no2) + "");
if (oprt.equals("-"))
t1.setText((no1 - no2) + "");
if (oprt.equals("*"))
t1.setText((no1 * no2) + "");
if (oprt.equals("/"))
t1.setText((no1 / no2) + "");
txt = "";
}
else
{
txt = txt + s;
t1.setText(txt);
}
}
}
}
Output: