Java Lab Manual
Java Lab Manual
1
2301CS201 - Object Oriented Programming using Java
B.Tech LAB PLANNING[BATCH 2024]
s
st
str
stre
strea
stream
B 6 Write an interactive program to print a diamond shape. For example, if user enters
the number 3, the diamond will be as follows:[B]
*
**
***
**
5 2
*
C 7 There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly rotated at an unknown pivot
index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ...,
nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7]
might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the
index of target if it is in nums, or -1 if it is not in nums.[C]
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
2
2301CS201 - Object Oriented Programming using Java
B.Tech LAB PLANNING[BATCH 2024]
B 5 Define class for Complex number with real and imaginary as data members. Create its
constructor, overload the constructors. Also define addition method to add two
complex objects. [B]
B 6 WAP that counts the number of objects created using static. [B]
C 7 Write a program in Java to demonstrate use of this keyword. Check whether this can
access the Static variables of the class or not.[C]
Practical -7: Programs using inheritance
A 1 Declare a class called Student having following data members:id_no,
no_of_subjects_registered, subject_code, subject_credits, grade_obtained and spi.
Define constructor and calculate_spi methods. Define main to instantiate an array for
objects of class student to process data of n students to be given as command line
arguments. [A]
A 2 Declare a class called book having author_name as private data member. Extend book
class to have two sub classes called book_publication & paper_publication. Each of
these classes have private member called title. Write a complete program to show
usage of dynamic method dispatch (dynamic polymorphism) to display book or paper
publications of given author.Use command line arguments for input data. [A]
A 4 Demonstrate the use of Super Keyword. [A]
A 5 Demonstrate the use of Final Keyword. [A]
B 3 Create a class named 'Member' having the following members:
1-Name
2-Age
3-Phone number
4-Address
5-Salary
It also has a method named 'printSalary' which prints the salary of the members.
Two classes 'Employee' and Manager' inherits the 'Member' class. The 'Employee' and
7 'Manager' classes have data members 'specialization' and 'department' respectively.
Now assign name, age, phone number address and salary to an employee and a 2
manager by making an object of both of these classes and print the same along with
specialization and department respectively. [B]
C 6 4. Design a class named MyPoint to represent a point with x- and y-coordinates. The
class contains:
The data fields x and y that represent the coordinates with getter methods.
1. a no-arg constructor that creates a point (0, 0).
2. a constructor that constructs a point with specified coordinates.
3. a method named distance that returns the distance from this point to a specified
point of the MyPoint type.
4. a method named distance that returns the distance from this point to another point
with specified x- and y-coordinates.Create a class named ThreeDPoint to model a
point in a three-dimensional space. Let ThreeDPoint be derived from MyPoint with
following additional features:
1. A data fields named z that represents the z-coordinate.
2. A no-arg constructor that creates a point (0, 0, 0).
3. A constructor that constructs a point with three specified coordinates.
4. A get method that returns the z value.
5. Override the distance method to return the distance between two points in the
three-dimensional space. Write a program that creates two points (0, 0, 0) and (10,
30, 25.5) and display the distance between the two points. [C]
3
2301CS201 - Object Oriented Programming using Java
B.Tech LAB PLANNING[BATCH 2024]
4
2301CS201 - Object Oriented Programming using Java
B.Tech LAB PLANNING[BATCH 2024]