Java Programming Lab Curriculum Guide
Java Programming Lab Curriculum Guide
J
(For [Link] III Semester CSE(AI&ML) and CSE(DS))
T
L /D P
C
cheme
S : 2020 0 0 3 1.5
Internal Assessment : 40
End Exam : 60
End Exam Duration : 3 Hrs
List of experiments:
Page1of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Example:
E nter your name: Ramana Maharshi
Your name is : Maharshi, R.
Page4of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Problem Statement:
1. a . Write an Account class having members like Account number, balance, account type,
methods likesetDetails(),getDetails(),getBalance(),withdraw()andDeposit()byConstructor
Overloading.
c lass Account
{
float acNumber;
float acBalance;
String acType;
Account()
{
}
Account(float i, float j)
{
acNumber=i;
acBalance=j;
acType="savings";
}
Account(float i, float j, String s)
{
acNumber=i;
acBalance=j;
acType=s;
}
public void setDetails(float i, float j, String s)
{
acNumber=i;
acBalance=j;
acType=s;
}
public void getDetails()
{
[Link]("Account number is : "+acNumber);
[Link]("Account balance is : "+acBalance);
[Link]("Account type is : "+acType);
}
public void Withdraw(float i)
{
acBalance=acBalance-i;
}
public void deposit(float i)
Page5of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
{
acBalance=acBalance+i;
}
public float getBalance()
{
return acBalance;
}
}
c lass BankAccount
{
public static void main(String arg[])
{
Account a1=new Account();
[Link](1000,10000,"savings");
[Link]("Details of the first account are\n");
[Link]();
[Link](20000);
[Link]("Balance of the first account is : "+[Link]());
Account a2=new Account();
[Link](1000,2000,"current");
[Link]("Details of second account are\n");
[Link]();
[Link](10000);
[Link]("Balance of second account is : "+[Link]());
Account a3=new Account(1002,30000,"current");
[Link]("Details of third account are\n");
[Link]();
}
}
utput:
O
Page6of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
1.b. Write a Volume class with Method Overloading for calculating volume of Cube, Cylinder and
Cuboids.
c lass ObjectVolume
{
float h,r,b,l;
public float Volume(float h)
{
return(h*h*h);
}
public double Volume(float r, float h)
{
return(3.14*r*r*h);
}
public float Volume(float l, float b, float h)
{
return(l*b*h);
}
}
class DemoOverloading
{
public static void main(String arg[])
{
float v1,v3;
double v2;
ObjectVolume b1=new ObjectVolume();
v1=[Link](10);
[Link]("volume of cube is : "+v1);
v2=[Link](10,7);
[Link]("volume of cylinder is : "+v2);
v3=[Link](3,4,5);
System .[Link]("volume of cuboid is : "+v3);
}
}
Output:
Page7of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Problem Statement:
2. W
rite Staff class having members ename, eid, econtact and getDetails() method with
parameterized constructor. TstaffclasshavingQualification,yoe(yearsofexperience),salary,
department as members and Display(), updateSalary() as methods with paramerized
Constructor. Nstaff class having yoe (years of experience), display() as method with
parameterizedconstructor.RNstaffclasshaving salary,yoe(yearsofexperience)asmembers
anddisplay(),updateSalary()asmethodswithparameterizedconstructor.ANstaffclasshaving
dailywages as member and display(), updateWage() as methods with parameterized constructor.
Write a Java Program to implement the following INHERITANCE hierarchy.
c lass Staff
{
String ename;
int eid, econtact;
Staff(String s,int i,int j)
{
ename=s;
eid=i;
econtact=j;
}
void getDetails()
{
[Link]("ename= "+ename);
[Link]("eid= "+eid);
[Link]("econtact= "+econtact);
}
}
class TStaff extends Staff
{
Page8of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
tring Qualification;
S
int yoexp;
int salary;
String dept;
TStaff(String s,int i,int j,String q,int y,int r,String c)
{
super(s,i,j);
Qualification=q;
yoexp=y;
salary=r;
dept=c;
}
void display()
{
getDetails();
[Link]("Qualification="+Qualification);
[Link]("yoexp="+yoexp);
[Link]("salary="+salary);
[Link]("dept="+dept);
}
void updateSalary(int t)
{
salary=t;
[Link]("updatesalary="+salary);
}
}
class NStaff extends Staff
{
int yoexp;
NStaff(String s,int i, int j,int k)
{
super(s,i,j);
yoexp=k;
}
void display()
{
getDetails();
[Link]("yoexp="+yoexp);
}
}
class RNStaff extends NStaff
{
float Salary;
Page9of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
NStaff(String s,int i,int j,int k,int l)
R
{
super(s,i,j,k);
Salary=l;
[Link]("salary="+Salary);
}
void display()
{
[Link]();
[Link]("salary="+Salary);
}
void updateSalary(int v)
{
Salary=v;
[Link]("updatesalary="+Salary);
}
}
class ANStaff extends NStaff
{
int dailywage;
ANStaff(String s,int i,int j,int k,int p)
{
super(s,i,j,k);
dailywage=p;
[Link]("dailywage="+dailywage);
}
void display()
{
[Link]();
[Link]("dailywage="+dailywage);
}
void updateWage(int x)
{
dailywage=x;
[Link]("dailywage="+dailywage);
}
}
class Salary
{
public static void main(String arg[])
{
TStaff a=new TStaff("sree",132,9456,"Btech",10,4000,"cse");
RNStaff b=new RNStaff("sruthi",111,8765,15,000);
Page10of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
NStaff c=new ANStaff("priya",453,9876,14,500);
A
[Link]();
[Link](3100);
[Link]();
[Link]();
[Link](4500);
[Link]();
[Link]();
[Link](600);
}
}
Output:
salary=0.0
dailywage=500
ename= sree
eid= 132
econtact= 9456
Qualification=Btech
yoexp=10
salary=4000
dept=cse
updatesalary=3100
e name= sruthi
eid= 111
econtact= 8765
yoexp=15
salary=0.0
updatesalary=4500.0
e name= priya
eid= 453
econtact= 9876
yoexp=14
dailywage=500
dailywage=600
Page11of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Problem Statement:
3. W
rite a Student classinSTDpackageandGradeclassinGRDpackagewhichisavailablein
STD package. Student class should have properties like name, rollno, four subject marks i.e
M1,M2,M3,M4,totalandgradeattributes.Parameterizedconstructorhastoinitializename,
rollno and four subject marks.setGrade()methodtosetthegrade,getTotal()tosetthevalue
fortotalfieldanditwillreturntotalmarks,display()methodtodisplaythestudentinformation.
Gradeclassshouldhavemembercalledtotal,getGrade()method.Valueforthetotalfieldisset
bytheConstructorofthegradeclass,getGrade()methodwillreturnthegradeforthestudents
based on total.
Based on the above information , Write StudentResults class by importing above two packages.
p ackage std;
public class Student
{
String name;
String rno;
int m1,m2,m3,m4;
char grade;
int total;
public Student(String s,String t,int a,int b,int c,int d)
{
name=s;
rno=t;
m1=a;
m2=b;
m3=c;
m4=d;
}
public int gettotal()
{
total=m1+m2+m3+m4;
return total;
}
public void display()
{
[Link]("Name of the student is : "+name);
[Link]("Roll num of the student is : "+rno);
Page12of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link]("Marks in four subjects are : sub1= "+m1+" sub2= "+m2+"
S
sub3= "+m3+" sub4= "+m4);
[Link]("Total marks are : "+total);
}
public char setgrade(char c)
{
grade=c;
return grade;
}
}
p ackage [Link];
public class Grade
{
int total;
char grade;
public Grade(int i)
{
total=i;
}
public char getgrade()
{
if(total>35)
grade='A';
else if(total>30)
grade='B';
else
grade='C';
return grade;
}
}
import [Link].*;
import std.*;
import [Link].*;
class StudentResult
{
public static void main(String arg[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter Name of the student : ");
String bs=[Link]();
[Link]("Enter Roll num of the student : ");
Page13of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
tring bn=[Link]();
S
[Link]("Enter Marks in first subject : ");
String i=[Link]();
[Link]("Enter Marks in second subject: ");
String j=[Link]();
[Link]("Enter Marks in third subject : ");
String k=[Link]();
[Link]("Enter Marks in fourth subject: ");
String l=[Link]();
int m=[Link](i);
int n=[Link](j);
int o=[Link](k);
int p=[Link](l);
Student s=new Student(bs,bn,m,n,o,p);
int t=[Link]();
Grade g=new Grade(t);
char d=[Link]();
[Link]();
[Link]("Grade is : "+[Link](d));
}
}
Output:
nter Name of the student :
E
Gopal
Enter Roll num of the student :
23X1056
Enter Marks in first subject :
56
Enter Marks in second subject:
34
Enter Marks in third subject :
54
Enter Marks in fourth subject:
37
Name of the student is : Gopal
Roll num of the student is : 23X1056
Marks in four subjects are : sub1= 56 sub2= 34 sub3= 54 sub4= 37
Total marks are : 181
Grade is : A
Page14of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Problem Statement:
4. a .WriteaPersoninterfacehavinggetNAG()toreadname,age,genderanddisplay()method.
Write Student interface having getSC() to read sid, courseName and it has to extend Person
interface.WriteStaffinterfacehavinggetEid_Sal()toreadeidandbasicSalary.Declarehraas
10% and da as 50%. StaffinterfacehastoextendPersoninterface.WriteTeachingAssistant
class,ithastoimplementbothStudentandStaffinterfaces.TeachingAssistantclassmusthave
gross() method to compute grosssalary.
import [Link].*;
interface Person
{
void getNAG(String n,int a,String g);
void display();
}
interface Student extends Person
{
void getSC(String i,String j);
}
interface Staff extends Person
{
double HRA=0.1;
double DA=0.5;
void getEid_Sal(String e,int bs);
}
class TA implements Student,Staff
{
String name,gender,sid,eid,couname;
int age,basicsal;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
double gross()
{
Page15of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
return(basicsal+basicsal*HRA+basicsal*DA);
}
public void getNAG(String n,int a,String g)
{
name=n;
age=a;
gender=g;
}
public void getSC(String i,String j)
{
sid=i;
couname=j;
}
public void getEid_Sal(String e,int bs)
{
eid=e;
basicsal=bs;
}
public void display()
{
[Link]("Name of the candidate is : "+name);
[Link]("Age of the candidate : "+age);
[Link]("Gender of the candidate : "+gender);
[Link]("sid of the candidate : "+sid);
[Link]("Course name name of the candidate : "+couname);
[Link]("Employee ID of the candidate : "+eid);
[Link]("Basic salary of the candidate : "+basicsal);
[Link]("Gross salary of the candidate : "+gross());
}
}
class Tdemo
{
public static void main(String arg[])
{
TA t=new TA();
[Link]("Ramana Maharsi",35,"male");
[Link]("s290","cse");
t.getEid_Sal("E709",9000);
[Link]();
}
}
Output:
Name of the candidate is : Ramana Maharsi
Age of the candidate : 35
Gender of the candidate : male
sid of the candidate : s290
Course name name of the candidate : cse
Employee ID of the candidate : E709
Page16of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
asic salary of the candidate : 9000
B
Gross salary of the candidate : 14400.0
5. D
esign and implement a javaprogramthatwilldothefollowingoperationstothestringread
by the user.
“Welcome! This is “cs212” Java Course.”
G. Convert all alphabets to capital, lower case letters and print out the result.
H. Find the length of the string.
I. Find the index of the word ‘Course’.
J. Replace cs212 to cse212.
K. Find the sum of ASCII codes of all characters at even positions.
L. Addcodetotheprogramsothatitreadstheusersfirstnameandlastname asasinglestring,
then print the last name followed by a comma and first initial.
Example:
E nter your name: Ramana Maharshi
Your name is : Maharshi, R.
import [Link].*;
class Sdemo
{
public static void main(String arg[]) throws Exception
{
[Link]("Type the given statement:");
String s,s1,s2,s3,fn,ln,Fn;
BufferedReader br=new BufferedReader(new InputStreamReader
([Link]));
s=[Link]();
[Link]("the given statement is :\n"+s);
s1=[Link]();
[Link]("After converting to lowercase letters :\n"+s1);
s2=[Link]();
[Link]("After converting to uppercase letters :\n"+s2);
int sj=[Link]("Course");
[Link]("index of the word course is : "+sj);
s3=[Link]("CS212","CSE212");
[Link]("statement after replacing CS212 with CSE212 is
\n"+s3);
int sum=0;
for(int i=0;i<[Link]();i+=2)
sum+=[Link](i);
[Link]("sum of ASCII values at even position is : "+sum);
Page17of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link]("type your name is:");
S
BufferedReader br1=new BufferedReader(new InputStreamReader
([Link]));
tring name=[Link]();
S
int sp=[Link](" ");
int nl=[Link]();
fn=[Link](0,sp);
ln=[Link](sp+1,nl);
Fn=ln+","+[Link](0,1)+".";
[Link]("your name is : "+Fn);
}
}
Output:
ype the given statement:
T
Welcome! This is "cs212" Java Course.
the given statement is :
Welcome! This is "cs212" Java Course.
After converting to lowercase letters :
welcome! this is "cs212" java course.
After converting to uppercase letters :
WELCOME! THIS IS "CS212" JAVA COURSE.
index of the word course is : 30
statement after replacing CS212 with CSE212 is
Welcome! This is "cs212" Java Course.
sum of ASCII values at even position is : 1565
type your name is:
Ramana Maharshi
your name is : Maharshi,R.
Page18of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Problem Statement:
6 a . Design a StudentRegistration class, provide your own Exception namely
SeatsFilledException.Theexceptionisthrownwhenstudentregistrationnumberisgreaterthan
XX20(whereXX islasttwodigitsofyearofjoining).TheClassshouldcontainregNum,name
and Course of the Student.
import [Link].*;
class SeatsFilledException extends Exception
{
int a;
SeatsFilledException(int i)
{
a=i;
}
public String toString()
{
return "seats are filled, limit exceeded! "+a;
}
}
class StudentRegistration
{
public static void studentDetails(String i,String j,int k) throws SeatsFilledException
{
if(k>1520) throw new SeatsFilledException(k);
[Link]("Details of student are:");
[Link]("Name of the student : "+i);
[Link]("Registered number : "+k);
[Link]("Course name : "+j);
}
public static void main(String arg[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the name of the student : ");
String name=[Link]();
[Link]("Enter course name : ");
String course=[Link]();
[Link]("Enter Registered number (<=1520) : ");
String ren=[Link]();
int rno=[Link](ren);
try
{
studentDetails(name,course,rno);
Page19of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
}
catch(SeatsFilledException e)
{
[Link]("Exception:"+e);
}
}
}
Output:
Enter the name of the student :
ramana maharshi
Enter course name :
cse
Enter Registered number :
1518
Details of student are:
Name of the student : ramana maharshi
Registered number (<=1520): 1518
Course name : cse
:\vishnu\ivsem>java StudentRegistration
C
Enter the name of the student :
ramana maharshi
Enter course name :
cse
Enter Registered number (<=1520):
1521
Exception:seats are filled, limit exceeded! 1521
Page20of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
6 b .WriteaJavaProgramusingCommandlineargumentsforperformingaddition,subtraction,
multiplication and division operations.
Note:-
(i). your program must have
● try,
● multiple catch blocks and
● finally block.
(ii). your program must throw ArithmeticException and ArrayIndexOutOfBoundsException.
c lass Arithmatic
{
public static void main(String a[])
{
try
{
int m=[Link](a[1]);
int n=[Link](a[2]);
if(a[0].equals("+"))
{
int s=m+n;
[Link]("sum of "+a[1]+" and "+a[2]+" is
:"+s);
}
else if(a[0].equals("-"))
{
int r=m-n;
[Link]("Difference of "+a[1]+" and "+a
[2]+" is :"+r);
}
else if(a[0].equals("/"))
{
float q=(float)m/n;
[Link]("divison of "+a[1]+" and "+a[2]+"
is :"+q);
}
else if(a[0].equals("x"))
{
int e=m*n;
[Link]("multiplication of "+a[1]+" and
"+a[2]+" is :"+e);
}
else
Page21of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
[Link]("invalid operation");
}
catch(ArithmeticException e)
{
[Link]("divison by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("array index exception"+e);
}
finally
{
[Link]("Finally block was executed at any cost");
}
}
}
Output:
java Arithmatic + 2 3
sum of 2 and 3 is :5
Finally block was executed at any cost
java Arithmatic - 2 3
Difference of 2 and 3 is :-1
Finally block was executed at any cost
java Arithmatic x 2 3
multiplication of 2 and 3 is :6
Finally block was executed at any cost
java Arithmatic / 2 3
divison of 2 and 3 is :0.6666667
Finally block was executed at any cost
java Arithmatic $ 2 3
invalid operation
Finally block was executed at any cost
Page22of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)
JAVA PROGRAMMING LAB (JP(P))
Problem Statement:
7 a. Write a MultiThread program to print Second andFifth table using Synchronization concept.
c lass multitable
{
synchronized void displayTable(int n)
{
[Link](""+n+"'s Multiplication table is :");
for(int i=1;i<=10;i++)
{
[Link](n+" * "+i+" = "+n*i);
try
{
[Link](200);
}
catch(InterruptedException e)
{
[Link](e);
}
}
}
}
class Thread1 extends Thread
{
multitable mt;
Thread1(multitable ob)
{
mt=ob;
}
public void run()
{
[Link](2);
}
}
class Thread2 extends Thread
{
multitable mt;
Thread2(multitable ob)
{
mt=ob;
}
public void run()
{
[Link](5);
}
Page23of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
}
class Mtable
{
public static void main(String a[])
{
multitable ob=new multitable();
Thread1 t1=new Thread1(ob);
Thread2 t2=new Thread2(ob);
[Link]();
[Link]();
}
}
Output:
2's Multiplication table is:
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
5's Multiplication table is:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Page25of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
}
public void run()
{
int i=0;
while(true)
{
[Link](i++);
}
}
}
class consumer implements Runnable
{
Q q;
consumer(Q ob)
{
q=ob;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
[Link]();
}
}
}
class ITC
{
public static void main(String arg[])
{
Q q=new Q();
new producer(q);
new consumer(q);
new producer(q);
new consumer(q);
new consumer(q);
}
}
Output:
p ut0
got0
put0
Page26of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
g ot0
put1
got1
put1
//import [Link].*;
import [Link].*;
import [Link].*;
c lass UniqueElements
{
public static void main(String args[ ])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader([Link]));
int n= [Link]([Link]());
String s[ ]= [Link]().split(",");
int n1=[Link];
HashSet<Integer> hs= new HashSet<Integer>();
for(int i=0; i<n1; i++)
[Link]([Link](s[i]));
[Link]([Link]()-n);
}
}
import [Link].*;
class PostfixEval
{
public static int evaluate(String pexp)
{
Deque<Integer> res = new LinkedList<Integer>();
String symbols[ ] = [Link](",");
for (String token : symbols)
{
if("+-/*".contains(token))
{
int y = [Link]();
int x = [Link]();
switch ([Link](0))
{
case '+': [Link](x + y); break ;
case '-': [Link](x - y); break ;
case '*': [Link](x * y); break ;
case '/': [Link](x / y); break ;
}
}
else
{
[Link]([Link](token));
}
}
return [Link]();
}
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a Postfix Expression:");
String st=[Link]();
[Link]("The result of evaluated postfix expression is:"+ evaluate(st));
}
}
Page30of30
Prepared by: Approved by:
ri. [Link] Reddy
S [Link] Sam Revision 0
Sri.K. Srikanth H.O.D
Effective handling of arrays and lists in Java involves using their respective methods and operations, such as iterating, adding, removing, or altering data. For instance, using an ArrayList provides the flexibility of dynamic resizing and easier element insertions, while arrays offer fixed-size collection manipulation. This allows data processing and manipulation according to application needs .
Exception handling in command-line arithmetic operations ensures program stability by catching possible runtime errors like ArithmeticException or ArrayIndexOutOfBoundsException. This prevents abrupt disruptions and allows for specified corrective actions or recovery paths, thus improving program reliability .
Synchronization in multithreading controls how threads access shared resources to prevent data inconsistency. For instance, the example of printing multiplication tables for numbers 2 and 5 uses synchronization to ensure that each thread completes its task without interference from another thread .
Method overloading in Java occurs when two or more methods in the same class have the same name but different parameters (different type, number, or both), whereas constructor overloading occurs when a class has multiple constructors with different parameters. Both allow flexibility in object creation and method invocation .
Access protection in Java packages aids in encapsulating the internal class details, making components accessible only within specified boundaries. This is crucial when implementing a StudentResults class as it ensures that only necessary details from the Student and Grade classes are exposed, improving modularity and security .
Custom exceptions in Java allow developers to handle specific application logic errors in a controlled manner. They extend the Exception class and can be thrown using the 'throw' keyword to signal error conditions specific to business logic, enhancing error specificity and clarity .
Multiple inheritance using interfaces is used when a class needs to implement multiple sets of functionalities that are defined in separate interfaces. The TeachingAssistant class implements this by simultaneously implementing both Student and Staff interfaces, allowing it to inherit different sets of methods from each .
An inheritance hierarchy in Java is implemented by abstracting common characteristics into a base class and progressively extending classes to represent detailed and specialized scenarios. For instance, classes like TStaff, NStaff, RNStaff, and ANStaff share common staff details but extend their functionality with specific attributes, representing a structured and natural inheritance order .
Exception propagation in Java occurs when an exception thrown in a method is not caught, thus getting passed up the call stack to find an appropriate catch block. This mechanism simplifies error handling by allowing centralized management but requires vigilance to prevent unhandled exceptions that could terminate the application unexpectedly, impacting robustness .
A BufferedReader in Java I/O operations is used to read text from a character-input stream efficiently by buffering characters to provide efficient reading of characters, arrays, and lines. It minimizes the number of I/O interactions, improving the performance of input operations, especially when reading large volumes of text data from files or other systems .