Java Lab Record 2024
Java Lab Record 2024
AIM:
To convert Fahrenheit to Celsius .
ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable
Step 3: Read the temperature value using scanner object as sc.nextInt() and store it in the
variable a.
Step 4: In the main method, call the celsius method as celsius (a), then celsius(double f)
method returns the Celsius temperature.
Step 6: Print the output
Step 7: End the program
PROGRAM:
import java.util.Scanner;
class
FahrenheittoCelsius
{
public static void main(String arg[])
{
double a,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Fahrenheit
temperature");
a=sc.nextDouble();
System.out.println("Celsius temperature is = "+celsius(a));
}
static double celsius(double f)
{
return (f-32)*5/9;
}
}
OUTPUT:
AIM:
To convert Fahrenheit to Celsius .
ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable a,d and initialize it.
Step 3: Perform type conversion of integer to long by assigning a to l , long to float
by assigning l to f, and float to double by assigning d1 = f.
Step 6: Print the output variables
Step 7: End the program
PROGRAM:
import
java.util.*; public
class Main
{
public static void main(String[] args)
{
int a = 50;
double d =
100;
// Automatic type
conversion long l = a;
// Automatic type
conversion float f = l;
double d1 = f;
AIM:
To convert the binary no to gray.
ALGORITHM:
PROGRAM:
import java.io.*;
import java.util.Scanner;
import static
java.lang.StrictMath.pow; public class
Main {
public static void main(String[] args)
{ int n, result = 0;
Scanner sc = new
Scanner(System.in);
n=sc.nextInt();
Main obj = new Main();
result =
obj.Graycode(n,0);
System.out.println(result);
}
int Graycode(int x,int i)
{
int a,b,
result=0; if(x!=
0)
{
a=x % 10;
x=x / 10;
b=x % 10;
4
OUTPUT:
AIM:
To check for Equilateral Triangle
ALGORITHM:
PROGRAM:
import java.io.*;
import
java.util.Scanner; public
class Main
{
public static void main(String[] args)
{ int a,b,c;
Scanner sc= new Scanner (System.in);
System.out.println("Enter the angles of
Triangle"); a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
sc.close();
if((a==b)&&(b==c)
)
System.out.println("Yes, equilateral Triangle");
else
System.out.println("Not a equilateral Traingle");
}
}
OUTPUT:
EX.NO: 03 NAME :
DATE: REG.NO:
AIM:
ALGORITHM:
Step 1: Start the program
Step 2: Declare the variable as array
Step 3: Assign the values
Step 4: Start looping
Step 5: Check the condition
Step 6: Print the value
Step 7: End the program
PROGRAM:
OUTPUT:
RESULT:
EX NO: 4 NAME :
DATE: REG.NO:
PROGRAM TO CALCULATE SIMPLE
AIM: INTEREST USING CLASS
PROGRAM:
import
java.util.Scanner; class
interest
{
public void cal(float p,float r,float t)
{
float si;
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);
} }
public class Main
{
public static void main(String[] args)
{
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal :
"); p = s.nextFloat();
System.out.print("Enter the Rate of interest :
"); r = s.nextFloat();
System.out.print("Enter the Time period :
"); t = s.nextFloat();
interest i = new interest(); i.cal(p,r,t);
}}
OUTPUT:
Result : Hence Simple Interest have been successfully calculated using Class.
9
8
EX NO: 5 NAME :
DATE: REG.NO:
AIM:
To display students using constructor overloading concept
PROGRAM:
OUTPUT:
java -cp /tmp/RIfvVJfARx Student
EX NO: 6 NAME :
DATE: REG.NO:
AIM:
ALGORITHM:
PROGRAM:
class strMethod
int str1=str.length();
System.out.println("length of string:"+str1);
System.out.println("Modified string:
System.out.println("String :"+str3)
System.out.println("CharAt :"+ch);
byte x[]={66,67,68,69,70};
String s4=new
String(x);
System.out.println(s4);
String str4="Hello".replace('l','w');
System.out.println(str4);
String
s1="HELLO";
String s2="hello";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2))
System.out.println(s5.startsWith("foot"));
System.out.println(s5.endsWith("ball"));
}}
OUTPUT:
To create a program to calculate the area and perimeter of the circle using the single
inheritance concept .
PROGRAM:
import java.io.*;
import
java.util.Scanner; class
Base
{
double r;
public void getInput(){
Scanner in=new Scanner(System.in);
System.out.println("\nEnter the radius of
Circle :"); r=in.nextDouble();
}
public void area()
{
double area=3.14*r*r;
System.out.println("Area of Circle :"+String.format("%.2f",area));
}
}
class Derived extends Base
{
public void perimeter()
{ double
perimeter=2*3.14*r;
System.out.println("Perimeter of Circle :"+String.format("%.2f",perimeter));
}
}
public class Main {
public static void main(String[] args)
{
Derived obj = new
Derived(); obj.getInput();
obj.area();
obj.perimeter()
;
}
12
}
13
OUTPUT:
AIM:
To create a program to calculate employee gross pay and net pay using the multilevel
inheritance concept .
PROGRAM:
import java.io.*;
import
java.util.Scanner; class
EmpDetails
{
Scanner in = new
Scanner(System.in); private String
name,id;
public void details()
{
System.out.println("\nEnter Employee
ID :"); id=in.next();
System.out.println("Enter Employee
Name :"); name=in.next();
}
public void printdetails()
{
System.out.println("\nEmployee Details :-\
n"); System.out.println("ID : "+id);
System.out.println("Name : "+name);
}
}
class Salary extends EmpDetails
{
protected double
bp,DA,HRA,PF; public void
Sal()
{
System.out.println("Enter Basic
Pay :"); bp=in.nextDouble();
DA=25*bp/100;
HRA=10*bp/100;
PF=12*bp/100;
}
public void printSal()
{
System.out.println("Basic Pay : $"+bp);
System.out.println("DA :
$"+DA);
15
System.out.println("HRA :
$"+HRA); System.out.println("PF
: $"+PF);
}
}
class Pay extends Salary
{
private double gp,np;
void pay()
{
gp=bp+DA+HRA+PF;
np=bp-PF;
System.out.println("Gross Pay :
$"+gp);
System.out.println("Net Pay : $"+np);
}
}
public class Emp
{
public static void main(String[] args)
{
Pay obj = new
Pay(); obj.details();
obj.Sal();
obj.printdetails();
obj.printSal();
obj.pay();
}
}
OUTPUT
EX NO: 8 NAME :
DATE: REG.NO:
AIM:
To calculate area of the rectangle and square using default method and to implement the
concept of multiple interface in java.
PROGRAM:
interface Polygon
{
void getArea();
// default method
default void getSides()
{
System.out.println("To get sides of a polygon.");
}
}
// implements the interface
class Rectangle implements Polygon
{ public void getArea() {
int length = 6;
int breadth = 5;
int area = length * breadth;
System.out.println("The area of the rectangle is " + area);
}
// overrides the
getSides() public void
getSides() {
System.out.println("I have 4 sides.");
}
}
// implements the interface
class Square implements Polygon
{ public void getArea() {
int length = 5;
int area = length * length;
System.out.println("The area of the square is " + area);
}
}
class Main {
public static void main(String[] args) {
// create an object of Rectangle
Rectangle r1 = new
Rectangle(); r1.getArea();
r1.getSides();
// create an object of
Square Square s1 = new
Square(); s1.getArea();
17
s1.getSides();
}
}
OUTPUT:
EX NO: 9 NAME :
DATE: REG.NO:
BLOCK AIM:
To implement the exception handling mechanism in java using nested try block.
PROGRAM:
System.out.println("other statement");
}
//catch block of outer try
block catch(Exception e)
{
System.out.println("handled the exception (outer catch)");
}
19
System.out.println("normal flow..");
}
}
OUTPUT:
To set priority of the threads and to implement the concept of java multithreading.
PROGRAM:
}
public static void main(String args[])
{
TestMultiPriority1 m1=new
TestMultiPriority1(); TestMultiPriority1
m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
OUTPUT
PROGRAM:
class MultithreadingDemo implements Runnable
{ public void run()
{
try {
// Displaying the thread that is
running System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
}
catch (Exception e) {
// Throwing an exception
System.out.println("Exception is caught");
}
}
}
class Main {
public static void main(String[] args)
{
int n = 8; // Number of
threads for (int i = 0; i < n; i+
+) {
Thread object
= new Thread(new
MultithreadingDemo()); object.start();
}
}
}
22
OUTPUT
To create a dynamic array of strings and implement various vectors methods using vector
class in java.
PROGRAM
import java.util.*;
public class VectorExample1 {
public static void main(String args[]) {
//Create an empty vector with initial capacity 4
Vector<String> vec = new Vector<String>(4);
//Adding elements to a
vector vec.add("RED");
vec.add("BLUE");
vec.add("GREEN");
vec.add("ORANGE");
//Check size and capacity
System.out.println("Size is:
"+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
//Display Vector elements
System.out.println("Vector element is:
"+vec); vec.addElement("YELLOW");
vec.addElement("WHITE");
vec.addElement("BLACK");
//Again check size and capacity after two insertions
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());
//Display Vector elements again
System.out.println("Elements are:
"+vec);
//Checking if Tiger is present or not in this
vector if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
//Get the first element
System.out.println("The first animal of the vector is = "+vec.firstElement());
//Get the last element
System.out.println("The last animal of the vector is = "+vec.lastElement());
}
}
24
OUTPUT:
Size is: 4
Default capacity is: 4
Vector element is: [RED, BLUE, GREEN, ORANGE]
Size after addition: 7
Capacity after addition is: 8
Elements are: [RED, BLUE, GREEN, ORANGE, YELLOW, WHITE, BLACK]
Tiger is not present in the list.
The first animal of the vector is = RED The
last animal of the vector is = BLACK
To create a dynamic array of integers and to implement various vectors methods using
vector class in java.
PROGRAM:
import
java.util.*; public
class Main
{
public static void main(String args[])
{
//Create an empty Vector
Vector < Integer > in = new Vector < > ();
//Add elements in the
vector in.add(100);
in.add(200);
in.add(300);
in.add(200);
in.add(400);
in.add(500);
in.add(600);
in.add(700);
//Display the vector elements
System.out.println("Values in vector: "
+in);
//use remove() method to delete the first occurence of an element
System.out.println("Remove first occourence of element 200: "+in.remove((Integer)200));
//Display the vector elements afre remove()
method System.out.println("Values in vector: "
+in);
//Remove the element at index 4
System.out.println("Remove element at index 4: "
+in.remove(4)); System.out.println("New Value list in vector: "
+in);
//Remove an element
in.removeElementAt(5)
;
//Checking vector and displays the element
System.out.println("Vector element after removal: " +in);
//Get the hashcode for this vector
System.out.println("Hash code of this vector = "+in.hashCode());
//Get the element at specified index
System.out.println("Element at index 1 is =
"+in.get(1));
}
}
26
OUTPUT:
AIM:
PROGRAM:
import
java.util.*; public
class Main
{
public static void main(String[] args)
{
//creating an instance of Stack
class Stack stk= new Stack();
// checking stack is empty or
not boolean result =
stk.empty();
System.out.println("Is the stack empty? " + result);
// pushing elements into
stack stk.push(78);
stk.push(113)
;
stk.push(90);
stk.push(120)
;
//prints elements of the stack
System.out.println("Elements in Stack: " + stk);
result = stk.empty();
System.out.println("Is the stack empty? " + result);
}
}
OUTPUT:
EX NO: 12 NAME :
DATE: REG.NO:
AIM:
To convert date between a specific instant in time and a set of calendar fields such as MONTH,
YEAR, HOUR, etc using Java Calendar class.
PROGRAM:
import
java.util.Calendar; public
class Main {
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance(); System.out.println("The
current date is : " + cal.getTime()); cal.add(Calendar.DATE, -
15); System.out.println("15 days ago: " + cal.getTime());
cal.add(Calendar.MONTH, 4);
System.out.println("4 months later: " +
cal.getTime()); cal.add(Calendar.YEAR, 2);
System.out.println("2 years later: " + cal.getTime());
}
}
OUTPUT:
AIM:
To perform event handling in java using ActionListener interface.
PROGRAM:
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener
{
TextField
tf; AEvent()
{
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click
me");
b.setBounds(100,120,80,30);
b.addActionListener(this);
add(b);add(tf);
setSize(300,300)
;
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ tf.setText("Welcome");
}
public static void main(String args[]){
new AEvent();
}}
OUTPUT:
AIM:
To perform event handling in java using MouseListener interface.
PROGRAM:
import java.awt.*;
import
java.awt.event.*;
public class MouseListenerExample extends Frame implements
MouseListener{ Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20)
; add(l);
setSize(300,300)
; setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{ l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{ l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{ l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{ l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{ l.setText("Mouse Released");
}
public static void main(String[] args)
{ new MouseListenerExample();
}
OUTPUT:
31
AIM:
PROGRAM:
import java.awt.*;
import
java.applet.*;
/*
<applet code = "LabelDemo" width=300 height=200>
</applet>
*/
public class LabelDemo extends Applet
{
public void init ()
{
Label one = new Label ("One");
Label two = new Label ("Two");
Label three = new Label
("Three"); add (one);
add (two);
add (three);
}
}
OUTPUT:
PROGRAM:
/*
<applet code="CreateAWTButton" width=200 height=200>
</applet>
*/
import
java.applet.Applet;
import java.awt.Button;
public class CreateAWTButton extends Applet
{
public void init()
{
Button b1 = new
Button();
b1.setLabel("Submit");
Button b2 = new Button("Cancel");
add(b1);
add(b2);
}
}
OUTPUT:
BORDER LAYOUT
AIM:
To arrange the components in five regions. The five regions can be north, south, east, west
and the centre using BorderLayout.
PROGRAM:
import java.awt.*;
import
javax.swing.*;
public class BorderDemo1
{
JFrame frame;
BorderDemo1(
)
{
frame=new JFrame();
JButton box1=new JButton("**NORTH**");;
JButton box2=new JButton("**SOUTH**");;
JButton box3=new JButton("**EAST**");;
JButton box4=new JButton("**WEST**");;
JButton box5=new
JButton("**CENTER**");;
frame.add(box1,BorderLayout.NORTH);
frame.add(box2,BorderLayout.SOUTH);
frame.add(box3,BorderLayout.EAST);
frame.add(box4,BorderLayout.WEST);
frame.add(box5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
{
36
new BorderDemo1();
}
}
OUTPUT:
RESULT: Thus the required output has been acquired successfully.
37
FLOW LAYOUT
AIM:
PROGRAM:
import java.awt.*;
import
javax.swing.*;
public class
FlowDemo1{ JFrame
frame1; FlowDemo1()
{ frame1=new JFrame();
JButton box1=new JButton("1");
JButton box2=new JButton("2");
JButton box3=new JButton("3");
JButton box4=new JButton("4");
JButton box5=new JButton("5");
JButton box6=new JButton("6");
JButton box7=new JButton("7");
JButton box8=new JButton("8");
JButton box9=new JButton("9");
JButton box10=new
JButton("10"); frame1.add(box1);
frame1.add(box2);
frame1.add(box3);
frame1.add(box4);
frame1.add(box5);
frame1.add(box6);
frame1.add(box7);
38
frame1.add(box8);
frame1.add(box9);
frame1.add(box10);
frame1.setLayout(new
FlowLayout(FlowLayout.LEFT))
; frame1.setSize(400,400);
frame1.setVisible(true);
}
OUTPUT