Java Lab Programs... (1)
Java Lab Programs... (1)
J
(For B.Tech 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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
G.PULLA 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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
G.PULLA 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()
{
System.out.println("Account number is : "+acNumber);
System.out.println("Account balance is : "+acBalance);
System.out.println("Account type is : "+acType);
}
public void Withdraw(float i)
{
acBalance=acBalance-i;
}
public void deposit(float i)
Page5of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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();
a1.setDetails(1000,10000,"savings");
System.out.println("Details of the first account are\n");
a1.getDetails();
a1.Withdraw(20000);
System.out.println("Balance of the first account is : "+a1.getBalance());
Account a2=new Account();
a2.setDetails(1000,2000,"current");
System.out.println("Details of second account are\n");
a2.getDetails();
a2.deposit(10000);
System.out.println("Balance of second account is : "+a2.getBalance());
Account a3=new Account(1002,30000,"current");
System.out.println("Details of third account are\n");
a3.getDetails();
}
}
utput:
O
Page6of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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=b1.Volume(10);
System.out.println("volume of cube is : "+v1);
v2=b1.Volume(10,7);
System.out.println("volume of cylinder is : "+v2);
v3=b1.Volume(3,4,5);
System .out.println("volume of cuboid is : "+v3);
}
}
Output:
Page7of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
G.PULLA 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()
{
System.out.println("ename= "+ename);
System.out.println("eid= "+eid);
System.out.println("econtact= "+econtact);
}
}
class TStaff extends Staff
{
Page8of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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();
System.out.println("Qualification="+Qualification);
System.out.println("yoexp="+yoexp);
System.out.println("salary="+salary);
System.out.println("dept="+dept);
}
void updateSalary(int t)
{
salary=t;
System.out.println("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();
System.out.println("yoexp="+yoexp);
}
}
class RNStaff extends NStaff
{
float Salary;
Page9of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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;
System.out.println("salary="+Salary);
}
void display()
{
super.display();
System.out.println("salary="+Salary);
}
void updateSalary(int v)
{
Salary=v;
System.out.println("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;
System.out.println("dailywage="+dailywage);
}
void display()
{
super.display();
System.out.println("dailywage="+dailywage);
}
void updateWage(int x)
{
dailywage=x;
System.out.println("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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
NStaff c=new ANStaff("priya",453,9876,14,500);
A
a.display();
a.updateSalary(3100);
System.out.println();
b.display();
b.updateSalary(4500);
System.out.println();
c.display();
c.updateWage(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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
G.PULLA 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()
{
System.out.println("Name of the student is : "+name);
System.out.println("Roll num of the student is : "+rno);
Page12of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
ystem.out.println("Marks in four subjects are : sub1= "+m1+" sub2= "+m2+"
S
sub3= "+m3+" sub4= "+m4);
System.out.println("Total marks are : "+total);
}
public char setgrade(char c)
{
grade=c;
return grade;
}
}
p ackage std.grd;
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 java.io.*;
import std.*;
import std.grd.*;
class StudentResult
{
public static void main(String arg[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Name of the student : ");
String bs=br.readLine();
System.out.println("Enter Roll num of the student : ");
Page13of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
tring bn=br.readLine();
S
System.out.println("Enter Marks in first subject : ");
String i=br.readLine();
System.out.println("Enter Marks in second subject: ");
String j=br.readLine();
System.out.println("Enter Marks in third subject : ");
String k=br.readLine();
System.out.println("Enter Marks in fourth subject: ");
String l=br.readLine();
int m=Integer.parseInt(i);
int n=Integer.parseInt(j);
int o=Integer.parseInt(k);
int p=Integer.parseInt(l);
Student s=new Student(bs,bn,m,n,o,p);
int t=s.gettotal();
Grade g=new Grade(t);
char d=g.getgrade();
s.display();
System.out.println("Grade is : "+s.setgrade(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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
G.PULLA 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 java.io.*;
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(System.in));
double gross()
{
Page15of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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()
{
System.out.println("Name of the candidate is : "+name);
System.out.println("Age of the candidate : "+age);
System.out.println("Gender of the candidate : "+gender);
System.out.println("sid of the candidate : "+sid);
System.out.println("Course name name of the candidate : "+couname);
System.out.println("Employee ID of the candidate : "+eid);
System.out.println("Basic salary of the candidate : "+basicsal);
System.out.println("Gross salary of the candidate : "+gross());
}
}
class Tdemo
{
public static void main(String arg[])
{
TA t=new TA();
t.getNAG("Ramana Maharsi",35,"male");
t.getSC("s290","cse");
t.getEid_Sal("E709",9000);
t.display();
}
}
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. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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 java.io.*;
class Sdemo
{
public static void main(String arg[]) throws Exception
{
System.out.println("Type the given statement:");
String s,s1,s2,s3,fn,ln,Fn;
BufferedReader br=new BufferedReader(new InputStreamReader
(System.in));
s=br.readLine();
System.out.println("the given statement is :\n"+s);
s1=s.toLowerCase();
System.out.println("After converting to lowercase letters :\n"+s1);
s2=s.toUpperCase();
System.out.println("After converting to uppercase letters :\n"+s2);
int sj=s.indexOf("Course");
System.out.println("index of the word course is : "+sj);
s3=s.replace("CS212","CSE212");
System.out.println("statement after replacing CS212 with CSE212 is
\n"+s3);
int sum=0;
for(int i=0;i<s.length();i+=2)
sum+=s.charAt(i);
System.out.println("sum of ASCII values at even position is : "+sum);
Page17of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
ystem.out.println("type your name is:");
S
BufferedReader br1=new BufferedReader(new InputStreamReader
(System.in));
tring name=br1.readLine();
S
int sp=name.indexOf(" ");
int nl=name.length();
fn=name.substring(0,sp);
ln=name.substring(sp+1,nl);
Fn=ln+","+fn.substring(0,1)+".";
System.out.println("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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
G.PULLA 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 java.io.*;
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);
System.out.println("Details of student are:");
System.out.println("Name of the student : "+i);
System.out.println("Registered number : "+k);
System.out.println("Course name : "+j);
}
public static void main(String arg[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the student : ");
String name=br.readLine();
System.out.println("Enter course name : ");
String course=br.readLine();
System.out.println("Enter Registered number (<=1520) : ");
String ren=br.readLine();
int rno=Integer.parseInt(ren);
try
{
studentDetails(name,course,rno);
Page19of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
}
catch(SeatsFilledException e)
{
System.out.println("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. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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=Integer.parseInt(a[1]);
int n=Integer.parseInt(a[2]);
if(a[0].equals("+"))
{
int s=m+n;
System.out.println("sum of "+a[1]+" and "+a[2]+" is
:"+s);
}
else if(a[0].equals("-"))
{
int r=m-n;
System.out.println("Difference of "+a[1]+" and "+a
[2]+" is :"+r);
}
else if(a[0].equals("/"))
{
float q=(float)m/n;
System.out.println("divison of "+a[1]+" and "+a[2]+"
is :"+q);
}
else if(a[0].equals("x"))
{
int e=m*n;
System.out.println("multiplication of "+a[1]+" and
"+a[2]+" is :"+e);
}
else
Page21of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
System.out.println("invalid operation");
}
catch(ArithmeticException e)
{
System.out.println("divison by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index exception"+e);
}
finally
{
System.out.println("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. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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)
{
System.out.println(""+n+"'s Multiplication table is :");
for(int i=1;i<=10;i++)
{
System.out.println(n+" * "+i+" = "+n*i);
try
{
Thread.sleep(200);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
}
class Thread1 extends Thread
{
multitable mt;
Thread1(multitable ob)
{
mt=ob;
}
public void run()
{
mt.displayTable(2);
}
}
class Thread2 extends Thread
{
multitable mt;
Thread2(multitable ob)
{
mt=ob;
}
public void run()
{
mt.displayTable(5);
}
Page23of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen 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);
t1.start();
t2.start();
}
}
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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Q q;
consumer(Q ob)
{
q=ob;
new Thread(this,"consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
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. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D
g ot0
put1
got1
put1
//import java.lang.*;
import java.io.*;
import java.util.*;
c lass UniqueElements
{
public static void main(String args[ ])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int n= Integer.parseInt(br.readLine());
String s[ ]= br.readLine().split(",");
int n1=s.length;
HashSet<Integer> hs= new HashSet<Integer>();
for(int i=0; i<n1; i++)
hs.add(Integer.parseInt(s[i]));
System.out.println(hs.size()-n);
}
}
import java.util.*;
class PostfixEval
{
public static int evaluate(String pexp)
{
Deque<Integer> res = new LinkedList<Integer>();
String symbols[ ] = pexp.split(",");
for (String token : symbols)
{
if("+-/*".contains(token))
{
int y = res.removeFirst();
int x = res.removeFirst();
switch (token.charAt(0))
{
case '+': res.addFirst(x + y); break ;
case '-': res.addFirst(x - y); break ;
case '*': res.addFirst(x * y); break ;
case '/': res.addFirst(x / y); break ;
}
}
else
{
res.addFirst(Integer.parseInt(token));
}
}
return res.removeFirst();
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Postfix Expression:");
String st=sc.nextLine();
System.out.println("The result of evaluated postfix expression is:"+ evaluate(st));
}
}
Page30of30
Prepared by: Approved by:
ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam Revision 0
Sri.K. Srikanth H.O.D