23Y009 - Java Record
23Y009 - Java Record
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;
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(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+"%");
}
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;
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;
int wordsCount = 0;
String line;
wordsCount += words.length;
return wordsCount;
}
public static void main(String[] args)
String line;
int wordsCount = 0;
int charactersCount = 0;
int linesCount = 0;
int specialCharCount = 0;
linesCount++;
charactersCount += line.length();
char c = line.charAt(i);
specialCharCount++;
wordsCount = countWords(fileName);
}
catch (IOException e) {
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.
MULTI-THREADING
PROGRAM :
BankTransaction.java
class BankAccount {
this.balance = initialBalance;
balance += amount;
balance -= amount;
else {
return balance;
}
public class BankTransaction {
account.deposit(300);
try {
Thread.sleep(1200);
catch (InterruptedException e) {
e.printStackTrace();
});
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();
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 CinemaTicketBooking() {
if (ticketQueue.size() >= 3) {
ticketQueue.offer(name);
if (ticketQueue.isEmpty()) {
else {
}}
public static void main(String[] args) {
int choice;
System.out.println(" 3. Exit");
do
choice = scanner.nextInt();
switch (choice)
case 1:
try {
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:
scanner.close();
}
SAMPLE OUTPUT :
RESULT :
Thus the given program was written and output was verified successfully.