USE OF OBJECTS
PROGRAM :
Bank.java
package LAB;
public class Bank
{
String name,ano;
float amount;
void insert(String ac, String n, float amt)
{
ano=ac;
name=n;
amount=amt;
}
void deposit(float amt)
{
amount=amount+amt;
System.out.println(" Rs."+amt+" Deposited");
}
void withdraw(float amt)
{
if(amount < amt)
{
System.out.println("\n Insufficient Balance Amount \n");
}
else
{
amount=amount-amt;
System.out.println(" Rs."+amt+" Withdrawn");
}
}
void checkBalance()
{
System.out.println(" Balance is : Rs."+amount+"\n");
}
void display()
{
System.out.println("\n\t BANK DETAILS ");
System.out.println(" Account Number : "+ano);
System.out.println(" Account Holder Name : "+name);
System.out.println(" Balance : Rs."+amount+"\n");
}
public static void main(String[] args)
{
Bank obj=new Bank();
obj.insert("SBI35361220482","Karthik Krishnan R",5000.50f);
obj.display();
obj.deposit(4000);
obj.checkBalance();
obj.withdraw(5000);
obj.checkBalance();
obj.display();
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
PASSING AN ARGUMENTS
PROGRAM :
Area of Shapes.java
package LAB;
import java.util.Scanner;
class Rectangle
{
int area(int a,int b)
{
return a*b;
}
}
class Triangle
{
double area(double b,double h)
{
return b*h*0.5;
}
}
class Circle
{
double area(double r)
{
return r*r*3.14;
}
}
public class Area
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int l,w;
double b,h,r;
// TODO Auto-generated method stub
System.out.println("\n\t\t Area of Shapes\n");
System.out.print(" Enter the Value of Length and Width of Rectangle : ");
l=sc.nextInt();
w=sc.nextInt();
Rectangle R = new Rectangle();
System.out.println(" Area of Rectangle : "+R.area(l,w));
Triangle t = new Triangle();
System.out.print("\n Enter the Value of Breadth and Height of Triangle : ");
b=sc.nextDouble();
h=sc.nextDouble();
System.out.printf(" Area of Triangle : %.2f \n",t.area(b,h));
Circle c = new Circle();
System.out.print("\n Enter the Radius Value of Circle :
"); r=sc.nextDouble();
System.out.printf(" Area of Circle : %.2f \n",c.area(r));
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
METHOD OVERLOADING
PROGRAM :
Volume.java
package LAB;
import java.util.Scanner;
public class Volume
{
int shape(int a)
{
return a*a*a;
}
int shape(int l,int w,int h)
{
return l*w*h;
}
float shape(float r,int h)
{
return 3.14f*r*r*h;
}
double shape(double r)
{
return (4/3f)*3.14f*r*r*r;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Volume ob = new Volume();
System.out.println("\n\tVolume of Shapes");
System.out.print("\n Enter the Size of Cubic : ");
int a=sc.nextInt();
System.out.println(" Volume of Cubic : "+ob.shape(a));
System.out.print("\n Enter the Length Value of Cuboid : ");
int l=sc.nextInt();
System.out.print(" Enter the Width Value of Cuboid : ");
int w=sc.nextInt();
System.out.print(" Enter the Height Value of Cuboid :
"); int h=sc.nextInt();
System.out.println(" Volume of Cuboid :
"+ob.shape(l,w,h)); System.out.print("\n Enter the Radius
Value of Cylinder : "); float r=sc.nextFloat();
System.out.print(" Enter the Height Value of Cylinder : ");
int H=sc.nextInt();
System.out.printf(" Volume of Cylinder : %.2f \n",ob.shape(r,H));
System.out.print("\n Enter the Radius Value of Sphere : ");
double R=sc.nextDouble();
System.out.printf(" Volume of Sphere : %.2f \n",ob.shape(R));
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
CONSTRUCTOR OVERLOADING
PROGRAM :
Constructor.java
package LAB;
public class Constructor {
private String accno,name;
private double balance;
public Constructor() { System.out.println("\n\
tBANK DETAILS ");
}
public Constructor(String accno, String n, double balance)
{ this.accno = accno;
this.name=n;
this.balance = balance;
}
public String getaccname() {
return name;
}
public void setaccname(String name)
{ this.name = name;
}
public String getaccno() {
return accno;
}
public void setaccno(String accno){
this.accno = accno;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void deposit(int amount) {
balance += amount;
}
public void withdraw(int amount) {
if (balance >= amount)
balance -= amount;
else
System.out.println(" Insufficient Balance");
}
public Constructor(Constructor acc) {
System.out.println(" Holder Name : "+acc.getaccname()+"\n Account Number = " + acc.getaccno()
+"\n Balance = Rs." + acc.getBalance());
}
public static void main(String[] args)
{
Constructor acc = new Constructor();
Constructor acc1 = new Constructor("TMB98765432107"," KARTHIK KRISHNAN
R",1000.0); System.out.println("\n Before Deposit :");
Constructor a1 = new Constructor(acc1);
System.out.println("\n After Deposit :");
acc1.deposit(1000);
Constructor acc2 = new Constructor(acc1);
acc1.withdraw(500);
System.out.println("\n After Withdrwan :");
Constructor acc3 = new Constructor(acc1);
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
PASSING ARRAY AND OBJECT
PROGRAM :
PointsTable.java
package LAB;
import java.util.Scanner;
public class PointsTable
{
int m,w,l,t,wp,lp,p;
String tn;
PointsTable(String tn,int a[])
{
this.tn=tn;
this.m=a[0];
this.w=a[1];
this.l=a[2];
this.t=a[3];
this.wp=a[4];
this.lp=a[5];
this.p=a[6];
}
PointsTable(PointsTable obj)
{
System.out.println("\n\t\t TEAM STATS \n");
System.out.println(" Team Name : "+obj.tn);
System.out.println(" No.of.Matches Won : "+obj.w);
System.out.println(" No.of.Matches Lose : "+obj.l);
System.out.println(" No.of.Matches Tie : "+obj.t);
System.out.println(" Total Points : "+obj.p);
System.out.println(" Won Percentage :
"+obj.wp+"%");
System.out.println(" Lose Percentage : "+obj.lp+"%");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print(" Enter the Team Name :
"); String s=sc.nextLine();
int a[]=new int[7];
System.out.print(" Enter the Team Performances :
"); for(int i=0;i<4;i++)
{
a[i]=sc.nextInt();
}
a[4]=(a[1]*100/a[0]);
a[5]=(a[2]*100/a[0]);
a[6]=(a[1]*3)+(a[3]*1);
PointsTable ob = new PointsTable(s,a);
PointsTable obj = new PointsTable(ob);
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
INHERITANCE OVERRIDING
PROGRAM :
InOver.java
package LAB;
import java.util.Scanner;
class Player {
Scanner sc = new Scanner(System.in);
String name,tname;
int age;
void getdata()
{
System.out.print(" Enter the Player Name : ");
name=sc.nextLine();
System.out.print(" Enter the Team Name : ");
tname=sc.nextLine();
System.out.print(" Enter the Player Age : ");
age=sc.nextInt();
}
void display()
{
System.out.println(" Name : "+name);
System.out.println(" Age : "+age);
System.out.println(" Team Name : "+tname);
}
}
class Batting extends Player
{
int m,r,no,hs,hc,c,bf;
void getdata()
{
super.getdata();
System.out.print(" Enter the No.of.Innings : ");
m=sc.nextInt();
System.out.print(" Enter the Runs : ");
r=sc.nextInt();
System.out.print(" Enter the No.Of.Balls Faced : ");
bf=sc.nextInt();
System.out.print(" Enter the High Score : ");
hs=sc.nextInt();
System.out.print(" Enter the No.of.Half-Centuries : ");
hc=sc.nextInt();
System.out.print(" Enter the No.of.Centuries : ");
c=sc.nextInt();
System.out.print(" Enter the No.of.NotOuts : ");
no=sc.nextInt();
}
void display()
{
double sr;
System.out.println("\n\tPlayer Batting Performance\n");
super.display();
System.out.println(" Innings : "+m);
System.out.println(" Runs : "+r);
System.out.println(" High Score : "+hs);
System.out.println(" Half-Centuries : "+hc);
System.out.println(" Centuries : "+c);
System.out.println(" NotOuts : "+no);
System.out.printf(" Average : %.2f \n",r/(m-no+0.0));
sr=((r+0.0)/(bf+0.0))*100;
System.out.printf(" Strike Rate : %.2f",sr);
}
}
public class InOver{
public static void main(String args[])
{
Batting ob = new Batting();
ob.getdata();
ob.display();
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
ABSTRACT CLASS AND METHODS
PROGRAM :
MarkList.java
package LAB;
import java.util.Scanner;
abstract class Student{
Scanner sc = new Scanner(System.in);
abstract void getmark();
abstract void display();
}
class Core extends Student
{
int tot=0,i;
int a[]=new int[5];
void getmark()
{
for(i=0;i<5;i++)
{
System.out.print(" Subject "+(i+1)+" : ");
a[i]=sc.nextInt();
tot+=a[i];
}
}
void display()
{
for(i=0;i<5;i++)
System.out.println(" Subject "+(i+1)+" : "+a[i]);
System.out.println(" Total : "+tot);
}
}
class Physical extends Student
{
int tot=0,m1,m2,m3,m4;
void getmark(){
System.out.print(" Enter the Marks for 1600m Running : ");
m1=sc.nextInt();
System.out.print(" Enter the Marks for 400m Running : ");
m2=sc.nextInt();
System.out.print(" Enter the Marks for Long Jump : ");
m3=sc.nextInt();
System.out.print(" Enter the Marks for High Jump : ");
m4=sc.nextInt();
tot=m1+m2+m3+m4;
}
void display() {
System.out.println(" 1600m Running : "+m1);
System.out.println(" 400m Running : "+m2);
System.out.println(" Long Jump : "+m3);
System.out.println(" High Jump : "+m4);
System.out.println(" Total : "+tot);
}
}
class MarkList
{
Scanner sc = new Scanner(System.in);
String name,rno,dep;
void getdata(){
System.out.print(" Enter the Student Name : ");
name=sc.nextLine();
System.out.print(" Enter the Student Reg.No : ");
rno=sc.nextLine();
System.out.print(" Enter the Student Department : ");
dep=sc.nextLine();
}
void setdata(){
System.out.println(" Name : "+name);
System.out.println(" Reg.No : "+rno);
System.out.println(" Department : "+dep+"\n");
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
MarkList d=new MarkList();
Core ob = new Core();
Physical ob2 = new Physical();
d.getdata();
ob.getmark();
ob2.getmark();
System.out.print("\n Enter 1-> Subject MARKLIST or 2-> Physical MARKLIST : ");
int n=sc.nextInt();
if(n==1){
System.out.println("\n\t STUDENT SUBJECT MARKLIST \n");
d.setdata();
ob.display();
}
else if(n==2){
System.out.println("\n\t STUDENT PHYSICAL MARKLIST \n");
d.setdata();
ob2.display();
}}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
EXCEPTION HANDLING
PROGRAM :
Date.java
import java.util.Scanner;
import java.lang.*;
class exp1 extends Exception{
}
class exp2 extends Exception{
}
class exp3 extends Exception{
}
class exp4 extends Exception{
}
class exp5 extends Exception{
}
class exp6 extends Exception{
}
public class Date
{
public static void main(String args[])
{
int d,m,y;
Scanner sc = new Scanner(System.in);
try
{
System.out.print(" Enter the Year : ");
y=sc.nextInt();
if(y<1)
throw new exp1();
System.out.print(" Enter the Month : ");
m=sc.nextInt();
if(m<1)
throw new exp1();
else if(m>12)
throw new exp2();
System.out.print(" Enter the Date : ");
d=sc.nextInt();
if(d<1)
throw new exp1();
else if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
{
if(d>31)
throw new exp3();
}
else if(m==4 || m==6 || m==9 || m==11)
{
if(d>30)
throw new exp4();
}
else if((y%100!=0 && y%4==0) || (y%400==0))
{
if(m==2)
{
if(d>29)
throw new exp5();
}
}
else if(m==2)
{
if(d>28)
throw new exp6();
}
System.out.println(" Given Date "+d+"/"+m+"/"+y+" is Valid Date");
}
catch(exp1 ob)
{
System.out.println(" Date Can't be Negative Numbers");
}
catch(exp2 ob)
{
System.out.println(" Month Should be Between 1 to 12");
}
catch(exp3 ob)
{
System.out.println(" Date Should be Between 1 to 31");
}
catch(exp4 ob)
{
System.out.println(" Date Should be Between 1 to 30");
}
catch(exp5 ob)
{
System.out.println(" Date Should be Between 1 to 29");
}
catch(exp6 ob)
{
System.out.println(" Date Should be Between 1 to 28");
}
}
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
FILE HANDLING
PROGRAM :
FileStats.java :
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileStats
private static int countWords(String fileName) throws IOException
try (BufferedReader br = new BufferedReader(new FileReader(fileName)))
int wordsCount = 0;
String line;
while ((line = br.readLine()) != null)
String[] words = line.split("\\s+");
wordsCount += words.length;
return wordsCount;
}
public static void main(String[] args)
String fileName = "Login.txt";
try (BufferedReader br = new BufferedReader(new FileReader(fileName)))
String line;
int wordsCount = 0;
int charactersCount = 0;
int linesCount = 0;
int specialCharCount = 0;
while ((line = br.readLine()) != null) {
linesCount++;
charactersCount += line.length();
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (!Character.isLetterOrDigit(c) && !Character.isWhitespace(c)) {
specialCharCount++;
wordsCount = countWords(fileName);
System.out.println("\n File Name : Login.txt \n");
System.out.println(" Number of Words : " + wordsCount);
System.out.println(" Number of Characters : " + charactersCount);
System.out.println(" Number of Lines : " + linesCount);
System.out.println(" Number of Special Characters : " + specialCharCount);
}
catch (IOException e) {
System.err.println("Error reading the File : " + e.getMessage());
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
MULTI-THREADING
PROGRAM :
BankTransaction.java
class BankAccount {
private int balance;
public BankAccount(int initialBalance) {
this.balance = initialBalance;
public synchronized void deposit(int amount) {
balance += amount;
System.out.println("\n Deposit: " + amount + "\n New Balance: " + balance);
public synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("\n Withdrawal : " + amount + "\n New Balance: " + balance);
else {
System.out.println("\n Insufficient Balance for Withdrawal : " + amount);
public int getBalance() {
return balance;
}
public class BankTransaction {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
System.out.println("\n Account Balance : " + account.getBalance());
Thread depositThread = new Thread(() -> {
for (int i = 0; i < 4; i++) {
account.deposit(300);
try {
Thread.sleep(1200);
catch (InterruptedException e) {
e.printStackTrace();
});
Thread withdrawThread = new Thread(() -> {
for (int i = 0; i < 3; i++) {
account.withdraw(500);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
});
depositThread.start();
withdrawThread.start();
try {
depositThread.join();
withdrawThread.join();
catch (InterruptedException e) {
e.printStackTrace();
System.out.println("\n Final Account Balance : " + account.getBalance());
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
COLLECTIONS
PROGRAM :
TicketBook.java :
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class CinemaTicketBooking
private Queue<String> ticketQueue;
public CinemaTicketBooking() {
ticketQueue = new LinkedList<>();
public void bookTicket(String name) throws Exception {
if (ticketQueue.size() >= 3) {
throw new Exception(" Queue is Full!, Cannot Book more Tickets.");
ticketQueue.offer(name);
System.out.println(" "+name + " has Booked a Ticket.");
public void cancelTicket() {
if (ticketQueue.isEmpty()) {
System.out.println(" No Tickets to Cancel.");
else {
String cancelled = ticketQueue.poll();
System.out.println(" "+cancelled + " has Cancelled their Ticket.");
}}
public static void main(String[] args) {
CinemaTicketBooking cinema = new CinemaTicketBooking();
Scanner scanner = new Scanner(System.in);
int choice;
System.out.println("\n 1. Book Ticket");
System.out.println(" 2. Cancel Ticket");
System.out.println(" 3. Exit");
do
System.out.print("\n Enter your Choice : ");
choice = scanner.nextInt();
switch (choice)
case 1:
try {
System.out.print("\n Enter the Name to Book Ticket : ");
String name = scanner.next();
cinema.bookTicket(name);
catch (Exception e) {
System.out.println(e.getMessage());
break;
case 2:
cinema.cancelTicket();
break;
case 3:
System.out.println("\n Exiting...");
break;
default:
System.out.println("\n Invalid Choice!, Enter the Valid Choice");
} while (choice != 3);
scanner.close();
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.