[Go to site: main page, start]

0% found this document useful (0 votes)
16 views30 pages

Java Programming Lab Curriculum Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views30 pages

Java Programming Lab Curriculum Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

‭ ava Programming Lab (JP(P))‬

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‬

‭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. [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))‬

‭[Link]‬ ‭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. [Link] Reddy
S [Link] 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. [Link] Reddy
S [Link] 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. [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‬ ‭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()‬
‭{‬
‭[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)‬
‭Page‬‭5‬‭of‬‭30‬
‭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

‭ 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. [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:‬

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. [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.‬ ‭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()‬
‭{‬
‭[Link]("ename= "+ename);‬
‭[Link]("eid= "+eid);‬
‭[Link]("econtact= "+econtact);‬
‭}‬
‭}‬
‭class TStaff extends Staff‬
‭{‬

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

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

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

‭Page‬‭11‬‭of‬‭30‬
‭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‬ ‭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()‬
‭{‬
‭[Link]("Name of the student is : "+name);‬
‭[Link]("Roll num of the student is : "+rno);‬
‭Page‬‭12‬‭of‬‭30‬
‭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;‬
‭}‬
‭}‬

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

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

‭Page‬‭14‬‭of‬‭30‬
‭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‭ .‬‭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 [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()‬
‭{‬
‭Page‬‭15‬‭of‬‭30‬
‭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‬
‭Page‬‭16‬‭of‬‭30‬
‭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‬

‭[Link] 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 [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);‬

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

‭Page‬‭18‬‭of‬‭30‬
‭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.‬‭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 [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);‬
‭Page‬‭19‬‭of‬‭30‬
‭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‬

‭Page‬‭20‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. [Link] Reddy
S [Link] 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=[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‬

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

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‬

‭[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL‬

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

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


‭concept.‬
‭Page‬‭24‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. [Link] Reddy
S [Link] 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)‬
‭{‬
‭[Link](e);‬
‭}‬
‭[Link]("got"+n);‬
‭flag=false;‬
‭notify();‬
‭return n;‬
‭}‬
‭synchronized void put(int i)‬
‭{‬
‭if(flag)‬
‭try‬
‭{‬
‭wait();‬
‭}‬
‭catch(InterruptedException e)‬
‭{‬
‭[Link](e);‬
‭}‬
‭n=i;‬
‭flag=true;‬
‭[Link]("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. [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‬

‭Page‬‭26‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. [Link] Reddy
S [Link] Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
g‭ ot0‬
‭put1‬
‭got1‬
‭put1‬

‭[Link] REDDY ENGINEERING COLLEGE (AUTONOMOUS): KURNOOL‬


‭Page‬‭27‬‭of‬‭30‬
‭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:‬
‭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 [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);‬
‭}‬
‭}‬

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


‭Page‬‭28‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. [Link] Reddy
S [Link] Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬
i‭mport [Link].*;‬
‭class OneAdd‬
‭{‬
‭public static List<Integer> addOne(List<Integer> A)‬
‭{‬
‭int n= [Link]()-1;‬
‭[Link](n,[Link](n)+1);‬
‭for(int i=n; i>0&& [Link](i)==10; --i)‬
‭{‬
‭[Link](i,0);‬
‭[Link](i-1,[Link](i-1)+1);‬
‭}‬
‭if([Link](0)==10)‬
‭{‬
‭[Link](0,0);‬
‭[Link](0,1);‬
‭}‬
‭return A;‬
‭}‬
‭public static void main(String args[ ])‬
‭{‬
‭Scanner sc=new Scanner([Link]);‬
‭[Link]("Enter a number:");‬
‭String num=[Link]();‬
‭List <Integer> al= new ArrayList<Integer>();‬
‭for(int i=0;i<[Link]();i++)‬
‭{‬
‭//[Link]((int)[Link](i)-48);‬
‭[Link]([Link]([Link]([Link](i))));‬
‭}‬
‭[Link](addOne(al));‬
‭[Link]("Result after adding one is: ");‬
‭for(int st:al)‬
‭[Link](st);‬
‭[Link]();‬
‭}‬
‭}‬

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


‭Page‬‭29‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. [Link] Reddy
S [Link] 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 [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));‬
‭}‬
‭}‬

‭Page‬‭30‬‭of‬‭30‬
‭Prepared by: Approved by:‬
‭ ri. [Link] Reddy
S [Link] Sam‬ ‭Revision 0‬
‭Sri.‬‭K. Srikanth‬ ‭H.O.D‬

Common questions

Powered by AI

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 .

You might also like