r22 - Oop Lab Manual CSD
r22 - Oop Lab Manual CSD
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Laboratory Manual
(VCE-R22)
Course Code-A8602
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
PROGRAM OUTCOMES (POS)
PO1: Engineering Knowledge: Apply knowledge of mathematics, science, engineering fundamentals
and an engineering specialization to the solution complex engineering problems.
PO2: Problem Analysis: Identify, formulate, research literature and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences and
engineering sciences.
PO3: Design/ Development of Solutions: Design solutions for complex engineering problems and
design system components or processes that meet specified needs with appropriate consideration for
public health and safety, cultural, societal and environmental considerations.
PO4: Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data and synthesis of
information to provide valid conclusions.
PO5: Modern Tool Usage: Create, select and apply appropriate techniques, resources and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
PO6: The Engineer and Society: Apply reasoning informed by contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to professional
engineering practice.
PO7: Environment and Sustainability: Understand the impact of professional engineering solutions
in societal and environmental contexts and demonstrate knowledge of and need for sustainable
development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
of engineering practice.
PO9: Individual and Team Work: Function effectively as an individual, and as a member or leader in
diverse teams and in multi-disciplinary settings.
PO10: Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as being able to comprehend and write effective
reports and design documentation, make effective presentations and give and receive clear instructions.
PO11: Project Management and Finance: Demonstrate knowledge and understanding of engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
PO12: Life-long Learning: Recognize the need for and have the preparation and ability to Engage in
independent and life- long learning in the broadest context of technological Change.
COURSE OVERVIEW:
This integrated course provides a comprehensive coverage of theory and practice of OOP concepts
using Java. The course focuses on different aspect of core Java Environment suitable to write efficient,
maintainable, and portable code for real world application. It provides strong foundation on OOP
Principles, Packages, and Interfaces and also illustrates Exception Handling and Multithreaded
mechanisms. The course provides In depth knowledge to implement Collection framework. Emphasis
on AWT and Swing concepts used for GUI applications is given with event handling. The course plays a
vital role in developing front-end interface for Mini and Major Projects.
COURSE OBJECTIVE
The course enables the learner to apply Object-oriented Programming concepts to develop Console and
GUI components as per needs and specifications.
COURSE OUTCOMES (COs)
After the completion of the course, the student will be able to:
CO# Course Outcomes
A8602.1 Make use of various constructs to write a console application.
A8602.2 Use principles of OOP to develop real time applications.
A8602.3 Examine the applications for Exception Handling and Multithreading.
A8602.4 Implement Collection Framework to organize data efficiently .
A8602.5 Build GUI applications using AWT and Swings.
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Bloom’s Level
CO# Remember Understand Apply Analyze Evaluate Create
(L1) (L2) (L3) (L4) (L5) (L6)
A8602.1 ✔
A8602.2 ✔
A8602.3 ✔
A8602.4 ✔
A8602.5 ✔
PO10
PO11
PO12
PSO1
PSO2
CO#/
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
POs
A8602.1 2 2 2 2
A8602.2 3 2 2 2
A8602.3 2 2 2 2
A8602.4 3 2 2 2
A8602.5 3 3 3 3
Note: 1-Low, 2-Medium, 3-High
List of Programs For Practice:
Tools and Expected
S.No Title of the Experiment
Techniques Skills/Ability
Implement Control statements
a. Read the marks of a student in 4 subjects and find
1 grade.
b. Program to check a number is Amstrong or not. Open source
Program to display prime numbers from m to n. Java Tool kit:
Implement OOP First principle - Encapsulation JDK 8 and Apply OOP
a. Define a class Rectangle with data member’s length above versions, principles to solve
and width. Write methods to find perimeter and problems
area of a rectangle. (class and object) Open Source
2
b. Create a class Account with data members name, IDE: Eclipse /
acno and balance. Use appropriate methods to NetBeans
perform various operations like deposit, withdraw,
balnce Check. OS: Windows /
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Tools and Expected
S.No Title of the Experiment
Techniques Skills/Ability
c. Create a class Student with appropriate data and Linux
methods using constructor.
Implement OOP Second principle – Polymorphism Web browser:
and Arrays Internet
a. Create overloaded methods to find volume of Explorer/
3 Sphere, Cylinder & Cone. Google/ Firefox
b. To sort given list of elements in ascending order.
c. Read two matrices of size m*n , p*q , perform the
multiplication of matrices.
Implement Java String Class.
a. Check a string is palindrome or not.
b. Given a string and an int n, return a string made of
n repetitions of the last ncharacters of the string.
You may assume that n is between 0 and the length
of the string, inclusive. Write a Java program.
repeatEnd("Hello",3)→"llollollo
repeatEnd("Hello",2)→"lolo"
repeatEnd("Hello", 1) → "o"
4
c. We'll say that a "triple" in a string is a char
appearing three times in a row. Return the number
of triples in the given string. The triples may
overlap. Write a Java program.
countTriple("abcXXXabc")→1,
countTriple("xxxabyyyycd")→3
countTriple("a") → 0
d. Read array of City names and Sort in dictionary
order.(Ascending order). Develop user
defined packages
Implement OOP Third principle – Inheritance.
and Interfaces
a. Declare a class called Employee having employee
_id and employee_name as members. Extend class
Employee to have a subclass called Salary having
designation and monthly_salary as members.
Define following:
Required constructor
A method to find and display all details of
employees drawing salary more than
5
Rs.20000/-
main () method to create an array.
b. Write a Java program that create an abstract base
class Shape with two members base and height, a
member function for initialization and a function to
compute shapeArea(). Derive two specific classes
Triangle and Rectangle which override the function
shapeArea(). Write a driver classes (main) to
display the area of the triangle and the rectangle.
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Tools and Expected
S.No Title of the Experiment
Techniques Skills/Ability
(Use super keyword).
Max. Marks
S.NO# EVALUATION METHOD ASSESSMENT TOOL
Marks Total
Pre-Lab Questions 1
Viva-Voce 1
2 Test 10 10
Preparatory Lab Test CAT-I
Viva-Voce 10 10
Test 40
Method Description
public String next() It returns the next token (string) from the
scanner.
public String nextLine() It moves the scanner position to the next line and
returns the value as a string.
public byte nextByte() It scans the next token as a byte.
public short nextShort() It scans the next token as a short value.
public int nextInt() It scans the next token as an int value.
public long nextLong() It scans the next token as a long value.
public float nextFloat() It scans the next token as a float value.
public double nextDouble() It scans the next token as a double value.
import java.util.*;
import java.io.*;
public class DataRead
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Roll No : “ +rollno);
System.out.println(“Name is “ +name);
System.out.println(“Feee is “ +fee);
sc.close();
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
PART-2
WEEK-1
Objective: After the completion of the practice session, the student will be able to implement
Control statements
import java.util.*;
public class GradeDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Marks in 4 Subjects");
int m1= sc.nextInt();
int m2= sc.nextInt();
double m3= sc.nextDouble();
double m4= sc.nextDouble();
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
if (s==m)
System.out.println("The number " +m + " -> is Amstrong");
else
System.out.println("The number " +m + " -> is Not Amstrong");
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
Output:
b) Create a class Account with data members name, acno and balance. Use appropriate
methods to perform various operations like deposit,withdraw, balnceCheck.
class Account
{
int acno;
String name;
double balance;
public void setData(int x , String y , double z)
{
acno=x;
name=y;
balance=z;
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
public double getBal( )
{
return (balance);
}
public void deposit( double amt)
{
balance = balance + amt;
}
public void withdraw( double amt)
{
balance = balance -amt;
}
public void display()
{
System.out.println("The account details are");
System.out.println("Account No -> " +acno);
System.out.println("Account Name -> " +name);
System.out.println("Account Balance -> " + balance);
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
c) Create a class Student with appropriate data and methods using constructor.
class Student
{
int rollno; String name, dept;
double avg;
Student()
{ }
Student(int x)
{
rollno = x;
}
Student(int x , String y)
{
this(x);
name=y;
}
Student(int x , String y , double z )
{
this(x,y);
avg=z;
}
Student(int x , String y , double z , String p )
{
this(x,y,z);
dept=p;
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
WEEK-3
Objective: After the completion of the practice session, the student will be able toimplement OOP
Second principle – Polymorphism.
a) Create overloaded methods to find volume of Sphere, Cylinder & Cone.
/* Use Method overloading concept to solve the problem.
• Volume of Sphere: 4/3 p r3
• Volume of Cylinder: p r2h
• Volume of Cone: 1/3 p r2h */
class Volume
{
final double PI= 3.142; //final to define constants
double getVolume(double r)
{
return(4.0/3 * PI * r*r*r);
}
void getVolume(double r , double h)
{
System.out.print("The volume of Cylinder is ");
double res = PI*r*r*h;
System.out.println(res);
}
double getVolume(int r , int h)
{
return(1/3.0 * PI *r*r*h);
}
}
public class VolumeDemo
{
public static void main(String args[])
{
Volume v1 = new Volume();
System.out.println("The volume of Sphere is " + v1.getVolume(3.67));
v1.getVolume(5.6,8.9);
System.out.println("The volume of Cone is " +v1.getVolume(8,10));
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
c) Read two matrices of size m*n , p*q , perform the multiplication of matrices.
import java.util.*;
public class MatrixDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of Matrix A - m * n");
int m = sc.nextInt();
int n = sc.nextInt();
System.out.println("Enter the size of Matrix B - p * q");
int p = sc.nextInt();
int q = sc.nextInt();
if (n != p)
{
System.out.println("Multiplication cannot be performed");
System.exit(0);
}
int a[][] = new int[m][n];
int b[][] = new int[p][q];
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
int c[][] = new int[m][q];
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
WEEK-4
Objective: After the completion of the practice session, the student will be able toImplement Java
String Class.
a) Check a string is palindrome or not.
import java.util.*;
public class Palindrome
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String s = sc.next();
int len = s.length();
int i , j=len-1;
boolean flag=true;
for(i=0 ; i < len/2 ; i++)
{
if(s.charAt(i) != s.charAt(j))
{
flag=false;
}
j--;
}
if(flag==true)
System.out.println("The string " +s + " is Palindrome");
else
System.out.println("The string " +s + " is Not a Palindrome");
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
b) Given a string and an int n, return a string made of n repetitions of the last n characters of
the string. You may assume that n is between 0 and the length of the string, inclusive.
Write a Java program.
repeatEnd("Hello",3)→"llollollo
repeatEnd("Hello",2)→"lolo"
repeatEnd("Hello", 1) → "o"
import java.util.*;
public class String1
{
public static String repeatEnd(String str, int n)
{
int len = str.length();
String res = " ";
for (int i = 0; i < n; i++)
{
res = res+str.substring(len - n, len);
}
return res;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String str = sc.next();
System.out.println("Enter the number of Chars");
int n = sc.nextInt();
String res = repeatEnd( str , n);
System.out.println("The result is " +res);
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
c) We'll say that a "triple" in a string is a char appearing three times in a row. Return the
number of triples in the given string. The triples may overlap. Write a Java program.
countTriple("abcXXXabc")→1,
countTriple("xxxabyyyycd")→3
countTriple("a") → 0
import java.util.*;
public class String2
{
public static int countTriple(String str)
{
int len = str.length();
int count = 0;
for (int i = 0; i < len-2; i++)
{
char tmp = str.charAt(i);
if (tmp == str.charAt(i+1) && tmp == str.charAt(i+2))
count++;
}
return count;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String");
String str = sc.next();
int res = countTriple( str );
System.out.println("The result is " +res);
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
import java.util.*;
public class SortDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of City Strings");
int n = sc.nextInt();
String s[] = new String[n];
System.out.println("\n\n Enter the City names");
for(int i=0; i< n;i++)
s[i] = sc.next();
System.out.println("\n\n The cities before sorting are");
for(int i=0; i< s.length;i++)
System.out.println(s[i]);
for(int i= 0; i < s.length; i++)
{
for(int j = i + 1; j < s.length; j++)
{
if( s[i].compareTo(s[j]) > 0 )
{
String tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
System.out.println("\n\n The citiesAfter Sorting are");
for(int i=0; i< s.length;i++)
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
System.out.println(s[i]);
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
WEEK-5
Objective: After the completion of the practice session, the student will be able to implement
OOP Third principle – Inheritance.
a) Declare a class called Employee having employee _id and employee_name as members.
Extend class Employee to have a subclass called Salary having designation and
monthly_salary as members. Define following:
• Required constructor
• A method to find and display all details of employees drawing salary more than
Rs.20000/-
• main () method to create an array.
import java.util.*;
class Emp
{
int empid;
String empname;
Emp(int no)
{
empid = no;
}
Emp(int no , String name)
{
this(no);
empname = name;
}
}
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
b) Write a Java program that create an abstract base class Shape with two members base
and height, a member function for initialization and a function to compute shapeArea().
Derive two specific classes Triangle and Rectangle which override the function
shapeArea(). Write a driver classes (main) to display the area of the triangle and the
rectangle.(Use super keyword).
double shapeArea()
{
return(base*hgt);
}
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
WEEK-6
Objective:After the completion of the practice session, the student will be able toimplement Packages and
Interfaces.
a) Create a Package Measure; in which store a class named Convertor that contains
methods to convert mm to cm, cm to m and m to km. Define a class Need_Convertor
that imports the Convertor class, now store Need_Convertor outside the package
Measure. Perform path settings accordingly.
package Measure;
public class Converter
{
public float mmtom(float mm)
{
float m=(mm/1000);
return m;
}
public float cmtom(float cm)
{
float m=(cm/100);
return m;
}
public float mtokm(float m)
{
float km=(m/1000);
return km;
}
}
import Measure.*;
public class NeedConverter
{
public static void main(String args[])
{
Converter c=new Converter();
System.out.println(" mm to m is "+c.mmtom(100));
System.out.println(" cm to m is "+c.cmtom(1000));
System.out.println(" m to km is "+c.mtokm(3000));
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
b) Write a Java program that implements an interface Student which has two methods
displayGrade() and attendance(). Implement two classes PG_Student and UG_Student
with necessary inputs of data.
interface Student
{
void displayGrade();
void attendence();
}
}
class UGStudent implements Student
{
String name;
int rollno;
String grade;
double att;
}
public class IntDemo
{
public static void main(String args[])
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
{
PGStudent s = new PGStudent("Harsha", 1101, "A",78.5);
s.display();
s.attendence();
s.displayGrade();
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
WEEK-7
Objective: After the completion of the practice session, the student will be able toimplement Exception
Handling .
a) Creates a user interface to perform integer divisions. The user enters two numbers in the text
fields, Num1 and Num2. The division of Num1 andNum2 is displayed in the Result field when
the Div- id button is clicked. If Num1 or Num2 were not an integer, the program would throw
a NumberFormatException. If Num2 is Zero, the program would throw an
ArithmeticException. Display the exception in a message dialog box.
import java.util.*;
public class ExcepDemo1
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number num1");
String s1 = sc.next();
System.out.println("Enter the number2");
String s2=sc.next();
try
{
int num1 = Integer.parseInt(s1);
int num2 = Integer.parseInt(s2);
System.out.println("num1 is " +num1);
System.out.println("num2 is " +num2);
if(num2 ==0)
throw new ArithmeticException ("Division Error");
catch(ArithmeticException e)
{
System.out.println("num2 must not be zero");
System.out.println("Exception" +e);
}
finally
{
System.out.println("Finally block is executed");
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
System.out.println("Remaining statements");
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Objective: After the completion of the practice session, the student will be able todevelop applications on
Multithreaded Programming and thread synchronization.
a) Create a multithreaded java program by creating a subclass of Thread and then creating,
initializing, and staring two Thread objects from your class. The threads will execute
concurrently and display “Java is object oriented” in console window.
class NewThread extends Thread
{
NewThread(String name)
{
super(name);
//start();
}
t1.start();
t2.start();
System.out.println("Main Program");
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
class Buffer
{
int item;
boolean produced = false;
synchronized void produce(int x)
{
if(produced)
{
try{
wait();
}
catch(InterruptedException ie)
{
System.out.println("Exception Caught");
}
}
item =x;
System.out.println("Producer - Produced-->" +item);
produced =true;
notify();
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Buffer b;
Producer( Buffer b)
{
this.b = b;
start();
}
}
}
b.consume();
b.consume();
b.consume();
b.consume();
}
}
import java.util.*;
class Employee
{
int eid;
String ename;
double sal;
import java.util.*;
class HashDemo
{
public static void main(String args[])
{
//Creating HashSet
HashSet<String> set=new HashSet<String>();
Iterator<String> i=set.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
WEEK-10
Objective: After the completion of the practice session, the student will be able to implement Collection
Frameworks to retrieve data.
a) Implement MouseListener and MouseMotionListener.
import java.awt.*;
import java.awt.event.*;
MouseDemo(String title)
{
super(title);
addMouseListener(this);
addMouseMotionListener(this);
setSize(500,500);
setVisible(true);
//window close
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
} );
msg= "MouseClicked";
x = e.getX();
y = e.getY();
repaint();
}
Output:
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
});
}
WEEK-11
Objective: After the completion of the practice session, the student will be able to Develop GUI
applications using AWT.
a) Create a Simple login window to validate a user with name and password.
import java.awt.*;
import java.awt.event.*;
public class LoginDemo extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1,t2;
Button b1;
LoginDemo(String name)
{
super(name);
l1=new Label("User Name");
l1.setBounds(20,50,100,20);
//x,y -> top left point , width,height are dimensions
l2=new Label("Password");
l2.setBounds(20,100,100,20);
l3 = new Label("->");
l3.setBounds(80,200,200,20);
add(l1);add(t1);
add(l2);add(t2);
add(b1);add(l3);
b1.addActionListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}});
}
public void actionPerformed(ActionEvent e)
{
String uname = t1.getText();
String pwd = t2.getText();
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
b) Using Grid Layout design a Simple calculator with appropriate event handling.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
add(b7);
add(b8);
add(b9);
add(t1);
add(b4);
add(b5);
add(b6);
add(mul);
add(b1);
add(b2);
add(b3);
add(sub);
add(b0);
add(eql);
add(add);
add(div);
//t1.setBounds(30,30,200,40);
add(clr);
add(rem);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
div.addActionListener(this);
mul.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
eql.addActionListener(this);
clr.addActionListener(this);
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
rem.addActionListener(this);
}
}
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
Output:
WEEK-12
Objective: After the completion of the practice session, the student will be able to Develop GUI
applications using Swing Controls.
a) Create a user interface to insert employee details, Display the data in Text area.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
EmpDemo( )
{
jf = new JFrame( ); //Top or High Level Window
jf.setSize(300,300);
jf.setTitle("Demo");
jp = new JPanel(); // lower or second level window
l1 = new JLabel("EMP-ID");
l2 = new JLabel("EMP-NAME");
l3 = new JLabel("Designation");
l4 = new JLabel("Gender");
l5 = new JLabel("CITY");
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
jc = new JComboBox(cities);
b1 = new JButton("Submit");
jf.setLayout(new GridLayout(5,4));
jp.add(l1); jp.add(t1);
jp.add(l2); jp.add(t2);
jp.add(l3); jp.add(t3);
jp.add(l4); jp.add(r1);jp.add(r2);
jp.add(l5); jp.add(jc);
jf.add(jp);
jf.setSize(1000,600);
jf.setVisible(true);
b1.addActionListener (this);
if(r1.isSelected())
data= data+ "Male" +";";
else
data= data+ "FeMale" +";";
ta1.setText(data);
}
public static void main(String args[])
{
new EmpDemo();
}
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
b) Create a JTable to display various fields of Student data like RollNo, Name, Branch
,Year, Percentage etc.
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/* import javax.swing.*/
JFrame f;
JTable j;
JStudent()
{
f = new JFrame();
f.setTitle("JTable Example");
String[][] data = {
{ "101", "Rajesh", "CSE","II","78.5"},
{ "102", "Harsha", "CSE","II","87.5"},
{ "103", "Varsha", "CSE","II","65.5"},
{ "104", "Kiran", "IT","II","75.5"},
{ "105", "Karan", "IT","II","87.5"},
};
// Column Names
String[] head = { "RollNo", "Name", "Department","Branch" , "Percentage"};
// adding it to JScrollPane
JScrollPane sp = new JScrollPane(j);
f.add(sp);
f.setSize(500, 200);
f.setVisible(true);
}
Output:
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
PART-3
ONLINE RESOURCES
OBJECTIVE
To help students on acquiring more practice on the course using various online resources
DESCRIPTION
These open tools form an excellent practice platform for the students, which he can explore anytime
from anywhere. The links of the websites providing OOP tutorials are given. Students are needed to
explore the websites.
LINKS
1. https://www.geeksforgeeks.org/java/
2. https://www.tutorialspoint.com/java/java_tutorial.pdf
3. https://www.pdfdrive.com/java-tutorial-e19074086.html
4. https://www.tutorialspoint.com/java/index.htm
5. https://www.tutorialspoint.com/java/index.htm
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
PART-4
Description
Possible viva questions include answers are provided. The student can have a practice of them.
What is the range of the random number r generated by the code below?
1
int r = (int)(Math.floor(Math.random() * 8)) + 2;
No, try, catch and finally forms a single unit and no other statements should exist in
Ans.
between try, catch and finally blocks.
Ans. throws keyword is used to declare an exception. You can find a detailed explanation
Why it is always recommended that clean up activities like closing the DB connections
and I/O resources to keep inside a finally block?
finally block will always be executed by ensuring the cleanup operations in finally
Ans. block, you will assure that those operations will be always executed irrespective of
whether an exception has occurred or not.
17 What is OutOfMemoryError in Exception Handling?
RunTimeException which occurs when JVM not able to cast an object of one type to
Ans.
another type is called ClassCastException.
Which class is the root class for all types of errors and exceptions in Exception
20
Hierarchy?
Ans. java.lang.Throwable is the superclass for all types of errors and exceptions in java.
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
21 When do we use printStackTrace() method in java?
Ans. NumberFormatException is thrown when you try to convert a String into a number.
When an exception is thrown by the main method then JVM terminates the program.
Ans.
As a result, you will find the exception message and stack trace in the system console.
28 What is multithreading?
Multithreading is a process of executing multiple threads simultaneously.
Multithreading is used to obtain the multitasking. It consumes less memory and gives
Ans.
the fast and efficient performance. Its main advantages are:
Threads share the same address space.
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
The thread is lightweight.
The cost of communication between the processes is low.
PART-5
KNOWLEDGE BASE
Description
Knowledge basncludes possible viva questions whose answers are not provided. The student has to
explore for the answers.
1. What is the order of the three top level elements of the java source file?
2. What are the states associated with the thread?
13. How can you share data between two thread in Java?
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
14. How can multiple threads be controlled simultaneously?
16. What is the main difference between wait () and sleep () method?
17. What is an immutable object? How can it help in writing a concurrent application?
18. What is ThreadPool?
20. What are the main differences between notify and notifyAll in Java?
31. Does finally runs always? On which case finally doesn't run?
42. Write a method that will remove given character from the String?
51. What is the first argument of the String array in main method?
65. Can you have an inner class inside a method and what variables can you access?-
78. Is it necessary that each try block must be followed by a catch block?
79. If I write return at the end of the try block, will the finally block still execute?
80. What is the type of argument that the sleep() method of Thread class takes.
81. What type of Exception is thrown by Thread.sleep()?
83. What is daemon thread and which method is used to create the daemon thread?-
84. What is use of assigning priority to a thread?
88. What are the advantages of the model over the event-inheritance model?
89. What is source and listener?
90. What is meant by controls and what are different types of controls in AWT?
91. What is meant by controls and what are different types of controls in AWT?
92. What is the difference between choice and list?
What is a layout manger and what are different types of layout managers available
93.
in java AWT?
94. How are the elements of different layouts organized?
(AUTONOMOUS)
Affiliated to JNTUH, Approved by AICTE, Accredited by NAAC with A++ Grade, ISO
9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(DATA SCIENCE)
95. Which containers use a Border layout as their default layout?