Java Record Programs
Java Record Programs
AY 2024-25 Sem-1
2. Write a program to find min, max and average of array elements read from
report.
overloading
MethodDispatch(DMD)concept.
19.Write a program to handle any two predefined exceptions using try catch
blocks.
interface.
22. Write a Program to create Two threads by extending thread class. (One
thread finding the square of given array, other thread converts every character
of string to uppercase)
24. Develop an AWT program to demonstrate List control with Event Handling.
handling.
27. Write an AWT program to implement a simple calculator with two TextFields,
four Buttons (named Add, Sub, Mul, Div) and a Label to display result. Use
29. Write a Java program to illustrate collection classes like Array List,
30. Write a Java program to implement iteration over Collection using Iterator
interface and Listlterator interface.
31. Write a Java program that reads a file name from the user, and then
displays information about whether the file exists, whether the file is readable,
whether the file is writable, the type of file and the length of the file in bytes. 32.
if (isPrime)
System.out.println(num);
}
}
}
2. Write a program to find min, max and average of array elements read
from the user dynamically.
import java.util.*;
class GroupOper
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("enter the number of elements");
int n= sc.nextInt();
int min=32767, max=-32768;
int sum=0;
int avg=0;
for(int i=0;i<n;i++)
{
System.out.println("enter element:"+(i+1));
int a = sc.nextInt();
if(a<min)
min=a;
if(a>max)
max=a;
sum+=a;
avg=avg+(sum/n);
}
System.out.println("MAX value :"+max);
System.out.println("MIN value:"+min);
System.out.println("SUM=" +sum);
System.out.println("AVG=" +avg);
}
}
import java.util.Scanner;
return result;
}
import java.util.*;
class Array3
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int a[][]=new int[3][];
a[0]=new int[3];
a[1]=new int[2];
a[2]=new int[4];
System.out.println("enter elemnts");
for(int i=0; i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
a[i][j]= sc.nextInt();
}
}
System.out.println("elemnts are");
for(int i=0; i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
5. Write a Program to read n numbers and display them in sorted order.
import java.util.*;
class BubbleSort
{
int temp=0;
for(int i=0;i<n;i++)
{
for(int j=1;j<(n-i);j++)
{
if(a[j-1]>a[j])
{
temp= a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
}
System.out.println("After sorting");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]+" ");
}
}
}
import java.util.Scanner;
// Bitwise AND
int andResult = num1 & num2;
System.out.println("Bitwise AND of " + num1 + " and " + num2 + " is: " +
andResult);
// Bitwise OR
int orResult = num1 | num2;
System.out.println("Bitwise OR of " + num1 + " and " + num2 + " is: " +
orResult);
// Bitwise XOR
int xorResult = num1 ^ num2;
System.out.println("Bitwise XOR of " + num1 + " and " + num2 + " is: " +
xorResult);
// Bitwise NOT
int notResult1 = ~num1;
int notResult2 = ~num2;
System.out.println("Bitwise NOT of " + num1 + " is: " + notResult1);
System.out.println("Bitwise NOT of " + num2 + " is: " +
notResult2);
// Left Shift
int leftShiftResult = num1 << 2;
System.out.println("Left Shift of " + num1 + " by 2 positions is: " +
leftShiftResult);
// Right Shift
int rightShiftResult = num1 >> 2;
System.out.println("Right Shift of " + num1 + " by 2 positions is: " +
rightShiftResult);
import java.util.Scanner;
class Stu
{
String name;
int rno;
double per;
int marks[];
void calcper()
{
double sum=0;
for(int p:marks)
sum=sum+p;
per=sum/marks.length;
}
void disp()
{
calcper();
System.out.println("Student name:"+name);
System.out.println("Roll no:"+rno);
System.out.println("Percentage:"+per);
}
}
class TestStu2
{
public static void main(String[] args)
{
int m[]={76,66,75,85,88};
Stu x=new Stu("teststu",111,m);
x.disp();
}
}
9. Write a program to create five student objects and display details as a
report.
class Student {
String name;
int rno;
Student() {
name="abc";
rno=101;
}
void display() {
System.out.println("name is"+name);
System.out.println("roll no is"+rno);
}
}
class Constructor {
public static void main(String args[]) {
Student s1= new Student();
s1.display();
}
}
10. Write a program to demonstrate method overloading.
class MethodOverload {
void add(int a, int b) {
System.out.println("The sum of two numbers is:"+(a+b));
}
void add(int a[],int b[]) {
int c[]=new int[a.length];
for(int i=0;i<a.length;i++)
c[i]=a[i]+b[i];
Product()
{
pid=999;
pname="keyboard";
price=100;
qty=50;
}
void purchase(int x)
{
if(qty>=x)
{
double t=x*price;
qty=qty-x;
System.out.println("-----ITEM-----:"+pname);
System.out.println("YOUR BILL AMOUNT FOR QTY-"+x+" IS:"+t);
}
else
System.out.println("Purchase not possible..Available qty:"+qty);
}
}
class ProdTest
{
public static void main(String[] args)
{
Product p=new Product(1001,"pendrive",250,25);
p.purchase(10);
p.purchase(5);
p.purchase(20);
Product p1=new Product();
p1.purchase(5);
p1.purchase(20);
Vehicle(String x)
{
brand=x;
}
}
class SingleInh
{
class Employee
{
String name;
int id;
float sal;
Employee(String n, int r, float s)
{
name=n;
id=r;
sal=s;
}
void empdet()
{
System.out.println("Name:"+name);
System.out.println("id is:"+id);
System.out.println("salary is:"+sal);
}
}
super(a,b,c);
dept=d;
}
void display()
{
System.out.println("Name:"+name);
System.out.println("id is:"+id);
System.out.println("salary is:"+sal);
System.out.println("department
is:"+dept); }
}
void show()
{
System.out.println("Name:"+name);
System.out.println("id is:"+id);
System.out.println("salary is:"+sal);
System.out.println("programming lang
is:"+proglang); }
}
class HierarchialInh
{
public static void main(String args[])
{
class Person
{
String name;
int age;
Person(String a,int b)
{
name=a;
age=b;
}
void disp()
{
System.out.println("Name:"+name);
System.out.println("Age:"+age);
}
}
class Emp extends Person
{
double sal;
Emp(String a, int b, double c)
{
super(a,b);
sal=c;
}
void disp()
{
super.disp();
System.out.println("Salary:"+sal);
}
}
class Manager extends Emp
{
int teamsize;
Manager(String a, int b, double c,int d)
{
super(a,b,c);
teamsize=d;
}
void disp()
{
super.disp();
System.out.println("Teamsize:"+teamsize);
}
}
class MultiLevel
{
public static void main(String[] args)
{
Manager m=new Manager("Rajiv",43,120000,23);
m.disp();
}
}
class Figure
{
double pi=3.14156;
void area()
{
System.out.println("Area is undefined");
}
}
class Circle extends Figure
{
double r=4.5;
//override area for Circle
void area()
{
System.out.println("The area of circle
is:"+pi*r*r); }
}
class Rectangle extends Figure
{
double l=4.5,b=7.8;
//override area for rectangle
void area()
{
System.out.println("The area of rectangle is:"+l*b);
}
}
class PolyEx
{
public static void main(String[] args)
{
Figure f=new Figure();
f.area();
f= new Circle();
f.area();
f=new Rectangle();
f.area();
}
}
16. Program to demonstrate Abstract
class. abstract class Figure
{
double pi=3.14156;
abstract void area();
void xyz()
{
System.out.println("other method");
}
}
class Circle extends Figure
{
double r=4.5;
//override area for Circle
void area()
{
System.out.println("The area of circle
is:"+pi*r*r); }
}
class Rectangle extends Figure
{
double l=4.5,b=7.8;
//override area for rectangle
void area()
{
System.out.println("The area of rectangle
is:"+l*b); }
}
class AbstractEx
{
public static void main(String[] args)
{
Figure f;
f= new Circle();
f.area();
f.xyz();
f=new Rectangle();
f.area();
}
interface MyStack
{
void push(int x);
int pop();
}
class StackArr implements Arr, MyStack
{
int st[];
int n,top;
StackArr()
{
n=5;
top=-1;
st=new int[n];
}
public void push(int x)
{
if(top==4)
{
System.out.println("Stack Full");
return;
}
else
st[++top]=x;
}
public int pop()
{
return st[top--];
}
public void print()
{
System.out.print("[");
for(int i=0;i<=top;i++)
System.out.print(st[i]+" ");
System.out.println("]");
}
}
class MultiInh
{
public static void main(String[] args)
{
StackArr ob=new StackArr();
System.out.println("Empty stack: " );
ob.print();
ob.push(10);
ob.push(20);
ob.push(30);
System.out.println("Contents of stack after push
operations"); ob.print();
System.out.println("popped element is "+ob.pop());
System.out.println("contents of Stack after pop operation:");
ob.print();
System.out.println("popped element is " +ob.pop());
System.out.println("Contents of stack after pop
operation:"); ob.print();
}
}
//BankTest.java
import bnk.Operations;
import java.util.Scanner;
class BankTest
{
public static void main(String[] args)
{
Operations x=new Operations();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the birth year:");
int yr=sc.nextInt();
System.out.println("Age="+x.getAge(yr));
19.Write a program to handle any two predefined exceptions using try catch blocks.
class Excep5
{
public static void main(String args[])
{
int a[]={1,2,3};
int sum;
int c=20,d=0,x;
try
{
sum=0;
sum=a[0]+a[1]+a[2]+a[3];
System.out.println("The array sum is:"+sum);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index exceeded");
System.out.println(e);
}
import java.util.Scanner;
class AgeException extends Exception
{
AgeException(String desc)
{
super(desc);
}
}
class UserExc
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
try
{
System.out.println("enter age:");
int age=sc.nextInt();
if(age<18) throw new AgeException("Invalid age for Vaccination");
System.out.println("Your are eligible for COVID vaccination");
System.out.println("Vaccination Given");
}
catch (AgeException e)
{
System.out.println(e.getMessage());
}
}
}
String s="Main";
for(int i=0;i<10;i++)
System.out.println(s+":"+i);
}
}
22. Write a Program to create Two threads by extending thread class. (One
thread finding the square of given array, other thread converts every
character of string to uppercase)
String y="Welcome";
Thr2 t2=new Thr2(y);
t1.start();
t2.start();
System.out.println("Bye from main");
}
}
class Resource {
int data=-1;
public synchronized void produce() throws InterruptedException {
if(data!=-1)
{
System.out.println("Producer is waiting");
wait();
}
System.out.println("Item Produced:200");
data=200;
notify(); // Notify the waiting thread that data is available
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Consumer extends Thread
{
Resource r;
Consumer(Resource x)
{
r=x;
}
public void run()
{
try {
for(int i=0;i<10;i++)
r.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class WaitNotifyExample1 {
public static void main(String[] args) {
Resource s = new Resource();
Producer p=new Producer(s);
Consumer c=new Consumer(s);
p.start();
c.start();
}
}
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ListDemo" width=600 height=600>
</applet>
*/
public class ListDemo extends Applet implements ActionListener {
Label l;
List dm;
String msg = "";
public void init() {
Font f= new Font("Courier", Font.BOLD, 20);
setFont(f);
l=new Label("Select the hobbies:");
dm = new List(3,true);
dm.add("Watching TV");
dm.add("Reading ");
dm.add("Online games");
dm.add("Cricket");
dm.add("Music");
add(l);
add(dm);
dm.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
repaint();
}
// Display current selections.
public void paint(Graphics g) {
int v[];
msg = "Selected value(s): ";
v = dm.getSelectedIndexes();
for(int i=0; i<v.length; i++)
msg += dm.getItem(v[i]) + " ";
g.drawString(msg, 6, 120);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="RadioButton" width=500 height=500>
</applet>
*/
public class RadioButton extends Applet implements ItemListener
{ String msg = "";
Font f;
Checkbox c1,c2,c3,c4;
CheckboxGroup cb;
public void init() {
Font f= new Font("Courier", Font.BOLD, 20);
setFont(f);
cb=new CheckboxGroup();
add(c1);
add(c2);
add(c3);
add(c4);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
c4.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie) {
repaint();
}
// Display current state of the check boxes.
public void paint(Graphics g) {
public AdapterDemo1() {
Font f= new Font("Courier", Font.BOLD, 20);
setFont(f);
setLayout(new FlowLayout());
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
mouseX = me.getX();
mouseY = me.getY();
msg = "Clikced";
repaint();
}
});//anonymous class, object is created for the mouse
adapter class.+
import java.awt.*;
import java.awt.event.*;
TextField t1,t2;
Button b1,b2,b3,b4;;
public Calc()
setSize(400,400);
l1=new Label("Number-1:");
l2=new Label("Number-2:");
l3=new Label("---------------------------------");
t1=new TextField(30);
t2=new TextField(30);
b1=new Button("ADD");
b2=new Button("SUB");
b3=new Button("MUL");
b4=new Button("DIV");
setLayout(new FlowLayout());
add(l1); add(t1);
add(l2); add(t2);
add(b1); add(b2);
add(b3); add(b4);
add(l3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setVisible(true);
ae) {
String
s=ae.getActionCommand(); int
a=Integer.parseInt(t1.getText()); int
b=Integer.parseInt(t2.getText()); int
c=0;
if(s.equals("ADD"))
c=a+b;
if(s.equals("SUB"))
c=a-b;
if(s.equals("MUL"))
c=a*b;
if(s.equals("DIV"))
c=a/b;
l3.setText(c+"");
args) {
Calc x=new Calc();
}
}
Mouse Events:
import java.awt.*;
import java.awt.event.*;
Label st;
Font f;
public MouseEvents() {
setFont(f);
setLayout(new FlowLayout());
add(st);
addMouseListener(this);
addMouseMotionListener(this);
st.setText("Mouse clicked.");
}
public void mouseEntered(MouseEvent me) {
st.setText("Mouse entered.");
}
st.setText("Mouse exited.");
st.setText("Mouse Down");
st.setText("Mouse Up");
// save coordinates
mouseX = me.getX();
mouseY = me.getY();
msg = "*";
mouseY); repaint();
mouseX = me.getX();
mouseY = me.getY();
msg = "Demo";
st.setText("Moving mouse at " + me.getX() + ", " +
me.getY()); repaint();
s.setSize(800,800);
s.setVisible(true);
Key Events:
import java.awt.*;
import java.awt.event.*;
Label st;
public SimpleKey() {
super("SimpleKey Frame");
setLayout(new FlowLayout());
add(st);
setSize(700, 700);
addKeyListener(this);
System.out.println("Key Pressed");
System.out.println("Key released");
System.out.println("Key typed");
msg += ke.getKeyChar();
repaint();
// Display keystrokes.
frame.setVisible(true);
}
29. Write a Java program to illustrate collection classes like Array List,
LinkedList, TreeMap and HashMap.
a) ArrayList
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListDemo {
System.out.println("List=" + list);
list.add(10);
list.add(20);
list.add(45.6);
list.add(5.7);
list.add("abc");
list.add("xyz");
item:"); i = sc.nextInt();
list.remove(i);
list.set(i, 100);
b) LinkedList
import java.util.LinkedList;
import java.util.Scanner;
list.add(10);
list.add(20);
list.add(45.6);
list.add(5.7);
list.add("abc");
list.add("xyz");
list.addLast("last item");
list.add(3, 50);
System.out.println("Linked List after inserting at first, last and 3rd
int i = sc.nextInt();
i = sc.nextInt();
list.remove(i);
list.removeFirst();
list.removeLast();
System.out.println("Linked List after removing " + i + ", first, last items=" + list +
list.set(i, 100);
list.clear();
c) TreeMap
import java.util.Scanner;
import java.util.TreeMap;
tm.put(121, "john");
tm.put(156, "smith");
tm.put(101, "Dennis");
tm.put(145, "James");
tm.lowerEntry(145));
tm.replace(101, "Rossum");
tm.clear();
d) HashMap
import java.util.*;
hm);
hm.put(121, "John");
hm.put(156, "Smith");
hm.put(101, "Dennis");
hm.put(145, "James");
hm.replace(101, "Rossum");
hm.remove(101);
hm.clear();
import java.util.*;
list.add(i);
while (it.hasNext()) {
System.out.print(it.next() + "\t");
ListIterator\n===========");
while (lit.hasNext()) {
System.out.print(lit.next() + "\t");
ListIterator\n===========");
while (lit.hasPrevious()) {
System.out.print(lit.previous() + "\t");
System.out.println();
}
}
31. Write a Java program that reads a file name from the user, and then
displays information about whether the file exists, whether the file is
readable, whether the file is writable, the type of file and the length of
the file in bytes.
import java.util.Scanner;
import java.io.File;
if (file.exists()) {
+ file.canRead());
if (index > 0) {
}
} else {
import java.io.*;
String name;
int age;
long mobileno;
Person(String name, int age, long mobileno) {
this.name = name;
this.age = age;
this.mobileno = mobileno;
class SerializationDemo {
// Serialization
oos.writeObject(p1);
oos.writeObject(p2);
serialize.txt"); }
}
// Deserialization
import java.io.*;
System.out.println("Performing
t1 = (Person) ois.readObject();
t2 = (Person) ois.readObject();
use test;
Class.forName("com.mysql.cj.jdbc.Driver");
"marks DOUBLE)";
Statement st = con.createStatement();
st.execute(query);
con.close();
} catch (Exception e) {
e.getMessage()); }
Procedure:
Step-1: Save the file as StudentDBCreation.java in the jdbc folder.
import java.sql.*;
import java.util.*;
public class StudentDBInsertion {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
pst.setInt(1, rno);
pst.setString(2, name);
pst.setString(3, branch);
pst.setDouble(4, marks);
pst.close();
con.close();
} catch (Exception e) {
e.getMessage()); }
Procedure:
javac StudentDBInsertion.java
import java.util.*;
import java.sql.*;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
pst.setInt(1, rno);
ResultSet rs = pst.executeQuery();
Details:\n==========="); if (result) {
System.out.println("Roll
No\tName\tBranch\tMarks\n======\t=====\t======\t=====")
; System.out.printf("%d\t%s\t%s\t%.2f%n",
rs.getInt(1),
rs.getString(2),
rs.getString(3),
rs.getDouble(4));
} else {
// Close resources
rs.close();
pst.close();
con.close();
} catch (Exception e) {
e.getMessage()); }
}
Procedure:
javac StudentDBRetreival.java