0% found this document useful (0 votes)
2 views

Java Lab Programs... (1)

The document outlines the Java Programming Lab course for B.Tech III Semester students in CSE(AI&ML) and CSE(DS) at G.Pulla Reddy Engineering College. It details the course outcomes, internal assessment, end exam structure, and a comprehensive list of experiments covering various Java programming concepts such as method overloading, inheritance, exception handling, multithreading, and collections. Each experiment includes specific programming tasks to reinforce the learning objectives.

Uploaded by

INDU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Lab Programs... (1)

The document outlines the Java Programming Lab course for B.Tech III Semester students in CSE(AI&ML) and CSE(DS) at G.Pulla Reddy Engineering College. It details the course outcomes, internal assessment, end exam structure, and a comprehensive list of experiments covering various Java programming concepts such as method overloading, inheritance, exception handling, multithreading, and collections. Each experiment includes specific programming tasks to reinforce the learning objectives.

Uploaded by

INDU
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

‭ ava Programming Lab (JP(P))‬

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‬

‭Course Outcomes:‬‭At the end of the course students‬‭will be able to‬


‭CO1: Implement Method overloading and Constructor overloading.‬
‭CO2: Implement Inheritance, Packages and Interfaces concepts.‬
‭CO3: Implement String handling and Exception handling.‬
‭CO4: Implement Multithreading and Collections.‬

‭List of experiments:‬

‭1. Programs on Method overloading and Constructor overloading.‬


‭2. Program to implement Multilevel and Hierarchical Inheritance.‬
‭3. Program to implement Packages with access protection.‬
‭4. Program to implement Multiple inheritance using interfaces.‬
‭5. Programs on String Handling methods.‬
‭6. Programs to implement built-in exceptions and customized exceptions.‬
‭7.‬ ‭Programs‬ ‭to‬ ‭implement‬ ‭Synchronization‬ ‭and‬ ‭Inter‬ ‭Thread‬ ‭Communication‬ ‭in‬
‭Multi-threading.‬
‭8. Programs to implement ArrayList, LinkedList and HashSet collections.‬

‭Page‬‭1‬‭of‬‭30‬
‭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))‬

‭S.No‬ ‭Lab experiments‬


‭1.‬ ‭Implementing Method overloading and Constructor overloading.‬
1‭ a‬‭.‬ ‭Write‬ ‭an‬ ‭Account‬ ‭class‬ ‭having‬ ‭members‬ ‭like‬ ‭Account‬ ‭number,‬ ‭balance,‬ ‭account‬ ‭type,‬
‭methods‬ ‭like‬ ‭setDetails(),‬ ‭getDetails(),‬ ‭getBalance(),‬ ‭withdraw()‬ ‭and‬ ‭Deposit()‬ ‭by‬
‭Constructor Overloading.‬
‭1b.‬‭Write‬‭a‬‭Volume‬‭class‬‭with‬‭Method‬‭Overloading‬‭for‬‭calculating‬‭volume‬‭of‬‭Cube,‬‭Cylinder‬
‭and Cuboids.‬

‭2.‬ ‭Program to implement multi level and Hierarchical Inheritance.‬


‭ rite‬ ‭Staff‬ ‭class‬ ‭having‬ ‭members‬ ‭ename,‬ ‭eid,‬ ‭econtact‬ ‭and‬ ‭getDetails()‬ ‭method‬ ‭with‬
W
‭parameterized‬ ‭constructor.‬ ‭Tstaff‬ ‭class‬ ‭having‬ ‭Qualification,‬ ‭yoe‬ ‭(years‬ ‭of‬ ‭experience),‬
‭salary,‬ ‭department‬ ‭as‬ ‭members‬ ‭and‬ ‭Display(),‬ ‭updateSalary()‬ ‭as‬‭methods‬‭with‬‭paramerized‬
‭Constructor.‬ ‭Nstaff‬ ‭class‬ ‭having‬ ‭yoe‬ ‭(years‬ ‭of‬ ‭experience),‬ ‭display()‬ ‭as‬ ‭method‬ ‭with‬
‭parameterized‬ ‭constructor.‬ ‭RNstaff‬ ‭class‬ ‭having‬ ‭salary,‬ ‭yoe‬ ‭(years‬ ‭of‬ ‭experience)‬ ‭as‬
‭members‬‭and‬‭display()‬‭,‬‭updateSalary()‬‭as‬‭methods‬‭with‬‭parameterized‬‭constructor.‬‭ANstaff‬
‭class‬ ‭having‬ ‭dailywages‬ ‭as‬ ‭member‬ ‭and‬ ‭display(),‬ ‭updateWage()‬ ‭as‬ ‭methods‬ ‭with‬
‭parameterized constructor.‬
‭Write a Java Program to implement the following INHERITANCE hierarchy.‬

‭3.‬ ‭Program to implement Packages with access protection.‬


‭ rite‬‭a‬‭Student‬‭class‬‭in‬‭STD‬‭package‬‭and‬‭Grade‬‭class‬‭in‬‭GRD‬‭package‬‭which‬‭is‬‭available‬‭in‬
W
‭STD‬‭package.‬‭Student‬‭class‬‭should‬‭have‬‭properties‬‭like‬‭name,‬‭rollno,‬‭four‬‭subject‬‭marks‬‭i.e‬
‭M1,‬‭M2,‬‭M3,‬‭M4,‬‭total‬‭and‬‭grade‬‭attributes.‬‭Parameterized‬‭constructor‬‭has‬‭to‬‭initialize‬‭name‬
‭,‬ ‭rollno‬ ‭and‬ ‭four‬ ‭subject‬ ‭marks.‬ ‭setGrade()‬ ‭method‬ ‭to‬ ‭set‬ ‭the‬ ‭grade‬ ‭,‬ ‭getTotal()‬ ‭to‬ ‭set‬ ‭the‬
‭value‬ ‭for‬ ‭total‬ ‭field‬ ‭and‬ ‭it‬ ‭will‬ ‭return‬ ‭total‬ ‭marks,‬ ‭display()‬ ‭method‬ ‭to‬ ‭display‬ ‭the‬ ‭student‬
‭information.‬‭Grade‬‭class‬‭should‬‭have‬‭member‬‭called‬‭total,‬‭getGrade()‬‭method.‬‭Value‬‭for‬‭the‬
‭total‬ ‭field‬ ‭is‬ ‭set‬ ‭by‬ ‭the‬ ‭Constructor‬ ‭of‬ ‭the‬ ‭grade‬ ‭class,‬ ‭getGrade()‬ ‭method‬ ‭will‬ ‭return‬ ‭the‬
‭grade for the students based on total.‬
‭Based‬ ‭on‬ ‭the‬ ‭above‬ ‭information‬ ‭,‬ ‭Write‬ ‭StudentResults‬ ‭class‬ ‭by‬ ‭importing‬ ‭above‬ ‭two‬
‭packages.‬

‭4.‬ ‭Program to implement‬‭Multiple inheritance using Interfaces.‬


‭Write‬‭a‬‭Person‬‭interface‬‭having‬‭getNAG()‬‭to‬‭read‬‭name,‬‭age,‬‭gender‬‭and‬‭display()‬‭method‬‭.‬
‭Write‬‭Student‬‭interface‬‭having‬‭getSC()‬‭to‬‭read‬‭sid,‬‭courseName‬‭and‬‭it‬‭has‬‭to‬‭extend‬‭Person‬
‭Page‬‭2‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
i‭nterface.‬‭Write‬‭Staff‬‭interface‬‭having‬‭getEid_Sal()‬‭to‬‭read‬‭eid‬‭and‬‭basicSalary.‬‭Declare‬‭HRA‬
‭as‬ ‭10%‬ ‭and‬ ‭DA‬ ‭as‬ ‭50%.‬ ‭Staff‬ ‭interface‬ ‭has‬ ‭to‬ ‭extend‬ ‭Person‬ ‭interface.‬ ‭Write‬
‭TeachingAssistant‬ ‭class,‬ ‭it‬ ‭has‬ ‭to‬ ‭implement‬ ‭both‬ ‭Student‬ ‭and‬ ‭Staff‬ ‭interfaces.‬
‭TeachingAssistant class must have gross() method to compute grosssalary.‬

‭5.‬ ‭Program on String Handling methods.‬


‭Design‬‭and‬‭implement‬‭a‬‭java‬‭program‬‭that‬‭will‬‭do‬‭the‬‭following‬‭operations‬‭to‬‭the‬‭string‬‭read‬
b‭ y the user.‬
‭“Welcome! This is “cs212” Java Course.”‬
‭A.‬ ‭Convert all alphabets to capital, lower case letters and print out the result.‬
‭B.‬ ‭Find the length of the string.‬
‭C.‬ ‭Find the index of the word ‘Course’.‬
‭D.‬ ‭Replace cs212 to cse212.‬
‭E.‬ ‭Find the sum of ASCII codes of all characters at even positions.‬
‭F.‬ ‭Add‬ ‭code‬ ‭to‬ ‭the‬ ‭program‬ ‭so‬ ‭that‬ ‭it‬ ‭reads‬‭the‬‭users‬‭firstname‬‭and‬‭lastname‬ ‭as‬‭a‬
‭single string, then print the last name followed by a comma and first initial.‬

‭Example:‬
E‭ nter your name: Ramana Maharshi‬
‭Your name is : Maharshi, R.‬

‭6.‬ ‭Program to handle various types of Built in and customized exceptions.‬


6‭ a.‬ ‭Write‬ ‭a‬ ‭Java‬ ‭Program‬ ‭using‬ ‭Command‬ ‭line‬ ‭arguments‬ ‭for‬ ‭performing‬ ‭addition,‬
‭subtraction, multiplication and division operations.‬
‭6b.‬ ‭Design‬ ‭a‬ ‭StudentRegistration‬ ‭class,‬ ‭provide‬ ‭your‬ ‭own‬ ‭Exception‬ ‭namely‬
‭SeatsFilledException.‬ ‭The‬ ‭exception‬ ‭is‬ ‭thrown‬ ‭when‬ ‭student‬ ‭registration‬ ‭number‬‭is‬‭greater‬
‭than‬ ‭XX20‬ ‭(where‬ ‭XX‬ ‭is‬ ‭last‬ ‭two‬ ‭digits‬ ‭of‬ ‭year‬ ‭of‬ ‭joining).‬ ‭The‬ ‭Class‬ ‭should‬ ‭contain‬
‭regNum, name and Course of the Student.‬

‭7.‬ ‭ rograms‬ ‭to‬ ‭implement‬ ‭Synchronization‬


P ‭and‬ ‭Inter‬ ‭Thread‬
‭Communication in Multi-threading.‬
7‭ a.‬ ‭Write‬ ‭a‬ ‭MultiThread‬ ‭program‬ ‭to‬ ‭print‬ ‭Second‬ ‭and‬ ‭Fifth‬ ‭table‬ ‭using‬ ‭Synchronization‬
‭concept.‬
‭7b.‬ ‭Write‬ ‭a‬ ‭Java‬ ‭Program‬ ‭for‬ ‭Producer-Consumer‬ ‭problem‬ ‭using‬ ‭InterThread‬
‭Communication concept.‬

‭8.‬ ‭Programs to implement ArrayList, LinkedList and HashSet collections.‬


8‭ a‬‭.‬ ‭Write‬ ‭a‬ ‭program‬ ‭that‬ ‭takes‬ ‭an‬ ‭arithmetic‬ ‭expression‬ ‭in‬ ‭Reverse‬ ‭Polish‬ ‭Notation‬ ‭and‬
‭returns‬‭the‬‭number‬‭that‬‭the‬‭expression‬‭evaluates‬‭to.‬‭Process‬‭sub‬‭expressions,‬‭keeping‬‭values‬
‭Page‬‭3‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
‭in‬‭a‬‭stack.‬‭How‬‭should‬‭operators‬‭be‬‭handled?‬ ‭For‬‭example‬‭"3,4,‬‭+,‬‭2,‬‭*,1,‬‭+"‬ ‭The‬‭ordinary‬
f‭ orm for this is (3 + 4) * 2 + 1.*/‬

8‭ b.‬‭Program to add 1 to the last digit of an ArrayList.‬


‭8c.‬‭Input a number ’N’ and an array as given below.‬
‭Input:N = 2 Array =1,2,3,3,4,4 O/p : 2‬
‭Find‬ ‭the‬ ‭least‬ ‭number‬ ‭of‬ ‭unique‬ ‭elements‬ ‭after‬ ‭deleting‬ ‭N‬ ‭numbers‬ ‭of‬ ‭elements‬ ‭from‬ ‭the‬
‭array.‬
‭In the above example, after deleting N=2 elements from the array.‬
‭In above 1,2 will be deleted. So 3,3,4,4 will be remaining so,‬
‭2 unique elements are in the array i.e 3 and 4.‬

‭Page‬‭4‬‭of‬‭30‬
‭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‬ ‭like‬‭setDetails(),‬‭getDetails(),‬‭getBalance(),‬‭withdraw()‬‭and‬‭Deposit()‬‭by‬‭Constructor‬
‭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)‬
‭Page‬‭5‬‭of‬‭30‬
‭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

‭ etails of the first account are‬


D
‭Account number is : 1000.0‬
‭Account balance is : 10000.0‬
‭Account type is : savings‬
‭Balance of the first account is : -10000.0‬

‭ etails of second account are‬


D
‭Account number is : 1000.0‬
‭Account balance is : 2000.0‬
‭Account type is : current‬
‭Balance of second account is : 12000.0‬

‭ etails of third account are‬


D
‭Account number is : 1002.0‬
‭Account balance is : 30000.0‬
‭Account type is : current‬

‭Page‬‭6‬‭of‬‭30‬
‭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:‬

v‭ olume of cube is : 1000.0‬


‭volume of cylinder is : 2198.0‬
‭volume of cuboid is : 60.0‬

‭Page‬‭7‬‭of‬‭30‬
‭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.‬ ‭Tstaff‬‭class‬‭having‬‭Qualification,‬‭yoe‬‭(years‬‭of‬‭experience),‬‭salary,‬
‭department‬ ‭as‬ ‭members‬ ‭and‬ ‭Display(),‬ ‭updateSalary()‬ ‭as‬ ‭methods‬ ‭with‬ ‭paramerized‬
‭Constructor.‬ ‭Nstaff‬ ‭class‬ ‭having‬ ‭yoe‬ ‭(years‬ ‭of‬ ‭experience),‬ ‭display()‬ ‭as‬ ‭method‬ ‭with‬
‭parameterized‬‭constructor.‬‭RNstaff‬‭class‬‭having‬ ‭salary,‬‭yoe‬‭(years‬‭of‬‭experience)‬‭as‬‭members‬
‭and‬‭display(),‬‭updateSalary()‬‭as‬‭methods‬‭with‬‭parameterized‬‭constructor.‬‭ANstaff‬‭class‬‭having‬
‭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‬
‭{‬

‭Page‬‭8‬‭of‬‭30‬
‭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;‬

‭Page‬‭9‬‭of‬‭30‬
‭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);‬

‭Page‬‭10‬‭of‬‭30‬
‭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‬

‭Page‬‭11‬‭of‬‭30‬
‭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‬ ‭class‬‭in‬‭STD‬‭package‬‭and‬‭Grade‬‭class‬‭in‬‭GRD‬‭package‬‭which‬‭is‬‭available‬‭in‬
‭STD‬ ‭package.‬ ‭Student‬ ‭class‬ ‭should‬ ‭have‬ ‭properties‬ ‭like‬ ‭name,‬ ‭rollno,‬ ‭four‬ ‭subject‬ ‭marks‬ ‭i.e‬
‭M1,‬‭M2,‬‭M3,‬‭M4,‬‭total‬‭and‬‭grade‬‭attributes.‬‭Parameterized‬‭constructor‬‭has‬‭to‬‭initialize‬‭name‬‭,‬
‭rollno‬ ‭and‬ ‭four‬ ‭subject‬ ‭marks.‬‭setGrade()‬‭method‬‭to‬‭set‬‭the‬‭grade‬‭,‬‭getTotal()‬‭to‬‭set‬‭the‬‭value‬
‭for‬‭total‬‭field‬‭and‬‭it‬‭will‬‭return‬‭total‬‭marks,‬‭display()‬‭method‬‭to‬‭display‬‭the‬‭student‬‭information.‬
‭Grade‬‭class‬‭should‬‭have‬‭member‬‭called‬‭total,‬‭getGrade()‬‭method.‬‭Value‬‭for‬‭the‬‭total‬‭field‬‭is‬‭set‬
‭by‬‭the‬‭Constructor‬‭of‬‭the‬‭grade‬‭class,‬‭getGrade()‬‭method‬‭will‬‭return‬‭the‬‭grade‬‭for‬‭the‬‭students‬
‭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);‬
‭Page‬‭12‬‭of‬‭30‬
‭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;‬
‭}‬
‭}‬

i‭mport 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 : ");‬

‭Page‬‭13‬‭of‬‭30‬
‭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‬

‭Page‬‭14‬‭of‬‭30‬
‭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‭ .‬‭Write‬‭a‬‭Person‬‭interface‬‭having‬‭getNAG()‬‭to‬‭read‬‭name,‬‭age,‬‭gender‬‭and‬‭display()‬‭method‬‭.‬
‭Write‬ ‭Student‬ ‭interface‬ ‭having‬ ‭getSC()‬ ‭to‬ ‭read‬ ‭sid,‬ ‭courseName‬ ‭and‬ ‭it‬ ‭has‬ ‭to‬ ‭extend‬ ‭Person‬
‭interface.‬‭Write‬‭Staff‬‭interface‬‭having‬‭getEid_Sal()‬‭to‬‭read‬‭eid‬‭and‬‭basicSalary.‬‭Declare‬‭hra‬‭as‬
‭10%‬ ‭and‬ ‭da‬ ‭as‬ ‭50%‬‭.‬ ‭Staff‬‭interface‬‭has‬‭to‬‭extend‬‭Person‬‭interface.‬‭Write‬‭TeachingAssistant‬
‭class,‬‭it‬‭has‬‭to‬‭implement‬‭both‬‭Student‬‭and‬‭Staff‬‭interfaces.‬‭TeachingAssistant‬‭class‬‭must‬‭have‬
‭gross() method to compute grosssalary.‬

i‭mport 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()‬
‭{‬
‭Page‬‭15‬‭of‬‭30‬
‭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‬
‭Page‬‭16‬‭of‬‭30‬
‭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‬

‭G.PULLA REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL‬


‭DEPARTMENT OF EMERGING TECHNOLOGIES IN COMPUTER SCIENCE (ECS)‬
‭JAVA PROGRAMMING LAB (JP(P))‬
‭Problem Statement:‬

‭5.‬ D
‭ esign‬ ‭and‬ ‭implement‬ ‭a‬ ‭java‬‭program‬‭that‬‭will‬‭do‬‭the‬‭following‬‭operations‬‭to‬‭the‬‭string‬‭read‬
‭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.‬ ‭Add‬‭code‬‭to‬‭the‬‭program‬‭so‬‭that‬‭it‬‭reads‬‭the‬‭users‬‭firstname‬‭and‬‭lastname‬ ‭as‬‭a‬‭single‬‭string,‬
‭then print the last name followed by a comma and first initial.‬
‭Example:‬
E‭ nter your name: Ramana Maharshi‬
‭Your name is : Maharshi, R.‬

i‭mport 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);‬

‭Page‬‭17‬‭of‬‭30‬
‭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.‬

‭Page‬‭18‬‭of‬‭30‬
‭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.‬‭The‬‭exception‬‭is‬‭thrown‬‭when‬‭student‬‭registration‬‭number‬‭is‬‭greater‬‭than‬
‭XX20(where‬‭XX‬ ‭is‬‭last‬‭two‬‭digits‬‭of‬‭year‬‭of‬‭joining).‬‭The‬‭Class‬‭should‬‭contain‬‭regNum,‬‭name‬
‭and Course of the Student.‬

i‭mport 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);‬
‭Page‬‭19‬‭of‬‭30‬
‭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‬

‭Page‬‭20‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
‭6‬ b‭ .‬‭Write‬‭a‬‭Java‬‭Program‬‭using‬‭Command‬‭line‬‭arguments‬‭for‬‭performing‬‭addition,‬‭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‬

‭Page‬‭21‬‭of‬‭30‬
‭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‬

j‭ava Arithmatic - 2 3‬
‭Difference of 2 and 3 is :-1‬
‭Finally block was executed at any cost‬

j‭ava Arithmatic x 2 3‬
‭multiplication of 2 and 3 is :6‬
‭Finally block was executed at any cost‬

j‭ava Arithmatic / 2 3‬
‭divison of 2 and 3 is :0.6666667‬
‭Finally block was executed at any cost‬

j‭ava Arithmatic $ 2 3‬
‭invalid operation‬
‭Finally block was executed at any cost‬

‭G.PULLA REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL‬

‭Page‬‭22‬‭of‬‭30‬
‭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 and‬‭Fifth 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);‬
‭}‬
‭Page‬‭23‬‭of‬‭30‬
‭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‬

‭7‬ b‭ .‬ ‭Write‬ ‭a‬ ‭Java‬ ‭Program‬ ‭for‬ ‭Producer-Consumer‬‭problem‬‭using‬‭Inter‬‭Thread‬‭Communication‬


‭concept.‬
‭Page‬‭24‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
c‭ lass Q‬
‭{‬
‭int n;‬
‭boolean flag=false;‬
‭synchronized int get()‬
‭{‬
‭if(!flag)‬
‭try‬
‭{‬
‭wait();‬
‭}‬
‭catch(InterruptedException e)‬
‭{‬
‭System.out.println(e);‬
‭}‬
‭System.out.println("got"+n);‬
‭flag=false;‬
‭notify();‬
‭return n;‬
‭}‬
‭synchronized void put(int i)‬
‭{‬
‭if(flag)‬
‭try‬
‭{‬
‭wait();‬
‭}‬
‭catch(InterruptedException e)‬
‭{‬
‭System.out.println(e);‬
‭}‬
‭n=i;‬
‭flag=true;‬
‭System.out.println("put"+n);‬
‭}‬
‭}‬
‭class producer implements Runnable‬
‭{‬
‭Q q;‬
‭producer(Q ob)‬
‭{‬
‭q=ob;‬
‭new Thread(this,"producer").start();‬

‭Page‬‭25‬‭of‬‭30‬
‭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‬

‭Page‬‭26‬‭of‬‭30‬
‭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‬

‭G.PULLA REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL‬


‭Page‬‭27‬‭of‬‭30‬
‭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:‬
‭8 a.‬‭Input a number ’N’ and an array as given below.‬
‭Input:N = 2 Array =1,2,3,3,4,4‬
‭O/p : 2‬
‭Find‬ ‭the‬ ‭least‬ ‭number‬ ‭of‬ ‭unique‬ ‭elements‬ ‭after‬ ‭deleting‬ ‭N‬ ‭numbers‬‭of‬‭elements‬‭from‬‭the‬
‭array.‬
‭In the above example, after deleting N=2 elements from the array.‬
‭In above 1,2 will be deleted.‬
‭So 3,3,4,4 will be remaining so,‬
‭2 unique elements are in the array i.e 3 and 4. */‬

/‭/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);‬
‭}‬
‭}‬

‭8‬ ‭b.‬‭Program to add 1 to the last digit of an ArrayList.‬


‭Page‬‭28‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
i‭mport java.util.*;‬
‭class OneAdd‬
‭{‬
‭public static List<Integer> addOne(List<Integer> A)‬
‭{‬
‭int n= A.size()-1;‬
‭A.set(n,A.get(n)+1);‬
‭for(int i=n; i>0&& A.get(i)==10; --i)‬
‭{‬
‭A.set(i,0);‬
‭A.set(i-1,A.get(i-1)+1);‬
‭}‬
‭if(A.get(0)==10)‬
‭{‬
‭A.set(0,0);‬
‭A.add(0,1);‬
‭}‬
‭return A;‬
‭}‬
‭public static void main(String args[ ])‬
‭{‬
‭Scanner sc=new Scanner(System.in);‬
‭System.out.println("Enter a number:");‬
‭String num=sc.nextLine();‬
‭List <Integer> al= new ArrayList<Integer>();‬
‭for(int i=0;i<num.length();i++)‬
‭{‬
‭//al.add((int)num.charAt(i)-48);‬
‭al.add(Integer.parseInt(String.valueOf(num.charAt(i))));‬
‭}‬
‭System.out.println(addOne(al));‬
‭System.out.print("Result after adding one is: ");‬
‭for(int st:al)‬
‭System.out.print(st);‬
‭System.out.println();‬
‭}‬
‭}‬

‭8 c.‬‭Write a program that takes an arithmetic expression‬‭in Reverse Polish‬


‭Page‬‭29‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
‭Notation and returns the number that the expression evaluates to.‬
‭Process subexpressions, keeping values in a stack. How should operators be handled?‬
‭For example "3,4, +, 2, *,1, +" The ordinary form for this is (3 + 4) * 2 + 1.‬

i‭mport 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));‬
‭}‬
‭}‬

‭Page‬‭30‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. A.Vishnuvardhan Reddy
S Dr.R.Praveen Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬

You might also like