0% found this document useful (0 votes)
5 views16 pages

Labmanualoop1 RJ

The document outlines a list of experiments for a semester course on Object Oriented Programming at L D College of Engineering, detailing various practical tasks to be completed in Java. Each experiment focuses on different programming concepts such as basic constructs, arrays, object-oriented principles, inheritance, exception handling, and multithreading. The course is structured with planned dates for each experiment throughout the academic year 2023-24.

Uploaded by

121-ronak Singh
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)
5 views16 pages

Labmanualoop1 RJ

The document outlines a list of experiments for a semester course on Object Oriented Programming at L D College of Engineering, detailing various practical tasks to be completed in Java. Each experiment focuses on different programming concepts such as basic constructs, arrays, object-oriented principles, inheritance, exception handling, and multithreading. The course is structured with planned dates for each experiment throughout the academic year 2023-24.

Uploaded by

121-ronak Singh
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
You are on page 1/ 16

L D COLLEGE OF ENGINEERING

COMPUTER ENGINEERING DEPARTMENT


B E Semester- IV
Object Oriented Programming -I (3140705)
List of Experiments (Academic Year 2023-24)

Sr.
Title CO Planned Date
No.
Second Week,
1 To learn basic java programming constructs. CO1
March
Third Week,
2 To learn Arrays and Strings in Java. CO1
March
Fourth Week,
3 To implement basic OO concept. CO2
March
First Week,
4 To implement inheritance. CO2
April
To demonstrate the use of abstract classes and Second Week,
5 CO2
interfaces. April
Third Week,
6 To implement exception handling. CO3
April
Fourth Week,
7 To demonstrate the use of multithreading. CO3
April
First Week,
8 To demonstrate I/O from files. CO4
May
Second Week,
9 To learn recursion and generics. CO4
May
Third Week,
10 To demonstrate the use of Collection framework. CO4
May
Fourth Week,
11 To learn JAVA FX UI Controls CO5
May
First Week,
12 To implement event handling and animation. CO5
Jun

Course Coordinator

R. Jaiswal
Practical 1: To learn basic java programming constructs.

1. Write a program that displays the area and perimeter of a circle that has a radius of 5.5
using the following formula: perimeter = 2 * radius * pi, area = radius * radius * pi

2. Average acceleration is defined as the change of velocity divided by the time taken to
make the change, as shown in the following formula: a = v1 - v0/t Write a program that
prompts the user to enter the starting velocity v0 in meters/ second, the ending velocity
v1 in meters/second, and the time span t in seconds, and displays the average
acceleration.
Here is a sample run:
Enter v0, v1, and t: 5.5, 50.9, 4.5
The average acceleration is 10.0889

3. Write a program that prompts the user to enter a three-digit integer and determines
whether it is a palindrome number. A number is palindrome if it reads the same from
right to left and from left to right.
Here is a sample run of this program:
Enter a three-digit integer: 121
121 is a palindrome
Enter a three-digit integer: 123
123 is not a palindrome

4. Suppose a right triangle is placed in a plane. The right-angle point is placed at (0, 0), and
the other two points are placed at (200, 0), and (0, 100). Write a program that prompts the
user to enter a point with x- and y-coordinates and determines whether the point is inside
the triangle.
Here are the sample runs:
Enter a point's x- and y-coordinates: 100.5, 25.5
The point is in the triangle
Enter a point's x- and y-coordinates: 100.5, 50.5
The point is not in the triangle

5. The great circle distance is the distance between two points on the surface of a sphere.
Let (x1, y1) and (x2, y2) be the geographical latitude and longitude of two points. The
great circle distance between the two points can be computed using the following
formula: d = radius * arccos(sin(x1) * sin(x2) + cos(x1) * cos(x2) * cos(y1 - y2)) Write a
program that prompts the user to enter the latitude and longitude of two points on the
earth in degrees and displays its great circle distance. The average earth radius is
6,371.01 km. Note that you need to convert the degrees into radians using the
Math.toRadians method since the Java trigonometric methods use radians. The latitude
and longitude degrees in the formula are for north and west. Use negative to indicate
south and east degrees.
Here is a sample run:
Enter point 1 (latitude and longitude) in degrees: 39.55, -116.25
Enter point 2 (latitude and longitude) in degrees: 41.5, 87.37
The distance between the two points is 10691.79183231593 km
Practical 2: To learn Arrays and Strings in Java.

1. Assume letters A, E, I, O, and U as the vowels. Write a program that prompts the user to
enter a string and displays the number of vowels and consonants in the string.

2. Write a program that prompts the user to enter two strings and displays the largest
common prefix of the two strings.

3. Some websites impose certain rules for passwords. Write a method that checks whether a
string is a valid password. Suppose the password rules are as follows: A password must
have at least eight characters. A password consists of only letters and digits. A password
must contain at least two digits. Write a program that prompts the user to enter a
password and displays Valid Password if the rules are followed or Invalid Password
otherwise.

4. Write a method that returns a new array by eliminating the duplicate values in the array
using the following method header: public static int[] eliminateDuplicates(int[] list) Write
a test program that reads in ten integers, invokes the method, and displays the result.

5. Write a method that returns the index of the smallest element in an array of integers. If
the number of such elements is greater than 1, return the smallest index. Use the
following header: public static int indexOfSmallestElement(double[] array)
Practical 3: To implement basic OO concept.

1 Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields
named width and height that specify the width and height of the rectangle. The default values are 1
for both width and height.
A no-arg constructor that creates a default rectangle.
A constructor that creates a rectangle with the specified width and height.
A method named getArea() that returns the area of this rectangle.
A method named getPerimeter() that returns the perimeter.
Write a test program that creates two Rectangle objects—one with width 4 and height 40 and the
other with width 3.5 and height 35.9.
Display the width, height, area, and perimeter of each rectangle in this order.

2 Design a class named Account that contains:


A private int data field named id for the account (default 0). A private double data field named
balance for the account (default 0). A private double data field named annualInterestRate that stores
the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date
data field named dateCreated that stores the date when the account was created.
A no-arg constructor that creates a default account.
A constructor that creates an account with the specified id and initial balance.
The accessor and mutator methods for id, balance, and annualInterestRate.
The accessor method for dateCreated.
A method named getMonthlyInterestRate() that returns the monthly interest rate.
A method named getMonthlyInterest() that returns the monthly interest.
A method named withdraw that withdraws a specified amount from the account.
A method named deposit that deposits a specified amount to the account.
Write a test program that creates an Account object with an account ID of 1122 a balance of Re.
20,000 and an annual interest rate of 4.5%. Use the withdraw method to withdraw Re. 2,500 use the
deposit method to deposit Re. 3,000 and print the balance, the monthly interest, and the date when
this account was created.

3 Design a class named Stock that contains:


A string data field named symbol for the stock’s symbol.
A string data field named name for the stock’s name.
A double data field named previousClosingPrice that stores the stock price for the previous day.
A double data field named currentPrice that stores the stock price for the current time.
A constructor that creates a stock with the specified symbol and name.
A method named getChangePercent() that returns the percentage changed from
previousClosingPrice to currentPrice.
Write a test program that creates a Stock object with the stock symbol ORCL, the name
Oracle Corporation, and the previous closing price of 34.5. Set a new current price to 34.35 and
display the price-change percentage.
Practical 4: To learn inheritance in Java.

1 (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and
its two subclasses named Student and Employee. Make Faculty and Staff subclasses of
Employee. A person has a name, address, phone number, and email address. A student has a
class status (freshman, sophomore, junior, or senior). Define the status as a constant. An
employee has an office, salary, and date hired. A faculty member has office hours and a rank.
A staff member has a title. Override the toString method in each class to display the class
name and the person’s name.
Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and
invokes their toString() methods.

2 (The Account class) Design a class named Account that contains:


■ A private int data field named id for the account (default 0).
■ A private double data field named balance for the account (default 0).
■ A private double data field named annualInterestRate that stores the current interest rate
(default 0). Assume all accounts have the same interest rate.
■ A private Date data field named dateCreated that stores the date when the account was
created.
■ A no-arg constructor that creates a default account.
■ A constructor that creates an account with the specified id and initial balance.
■ The accessor and mutator methods for id, balance, and annualInterestRate.
■ The accessor method for dateCreated.
■ A method named getMonthlyInterestRate() that returns the monthly interest rate.
■ A method named getMonthlyInterest() that returns the monthly interest.
■ A method named withdraw that withdraws a specified amount from the account.
■ A method named deposit that deposits a specified amount to the account.
(Subclasses of Account)Create two subclasses for checking and saving accounts. A checking
account has an overdraft limit, but a savings account cannot be overdrawn.
Write a test program that creates objects of Account, SavingsAccount, and CheckingAccount
and invokes their toString() methods.

3 Suppose that we are required to model students and teachers in our application. We can
define a super class called Person to store common properties such as name and address, and
subclasses Student and Teacher for their specific properties. For students, we need to
maintain the courses taken and their respective grades; add a course with grade, print all
courses taken and the average grade. Assume that a student takes no more than 30 courses for
the entire program. For teachers, we need to maintain the courses taught currently, and able to
add or remove a course taught. Assume that a teacher teaches not more than 5 courses
concurrently.
Design the classes as follows.

Write a program to implement all these classes and test program to test all the methods.
Practical 5: To learn abstract classes and interfaces Java.

1 Write a superclass called Shape (as shown in the class diagram), which contains:
 Two instance variables color (String) and filled (boolean).
 Two constructors: a no-arg (no-argument) constructor that initializes the color to
"green" and filled to t rue, and a constructor that initializes
the color and filled to the given values.
 Getter and setter for all the instance variables. By convention, the getter for
a boolean variable xxx is called isXXX() (instead of getXxx() for all the other
types).
 A toString() method that returns "A Shape with color of xxx and
filled/Not filled".
Write a test program to test all the methods defined in Shape.
Write two subclasses of Shape called Circle and Rectangle, as shown in the class
diagram.

The Circle class contains:


 An instance variable radius (double).
 Three constructors as shown. The no-arg constructor initializes the radius to 1.0.
 Getter and setter for the instance variable radius.
 Methods getArea() and getPerimeter().
 Override the toString() method inherited, to return "A Circle with
radius=xxx, which is a subclass of yyy", where yyy is the output of
the toString() method from the superclass.
The Rectangle class contains:
 Two instance variables width (double) and length (double).
 Three constructors as shown. The no-arg constructor initializes
the width and length to 1.0.
 Getter and setter for all the instance variables.
 Methods getArea() and getPerimeter().
 Override the toString() method inherited, to return "A Rectangle with
width=xxx and length=zzz, which is a subclass of yyy",
where yyy is the output of the toString() method from the superclass.

Write a class called Square, as a subclass of Rectangle. Convince yourself that


Square can be modeled as a subclass of Rectangle. Square has no instance
variable, but inherits the instance variables width and length from its superclass Rectangle.
 Provide the appropriate constructors (as shown in the class diagram).
Hint:

public Square(double side) {


super(side, side); // Call superclass Rectangle(double, double)
}
 Override the toString() method to return "A Square with side=xxx,
which is a subclass of yyy", where yyy is the output of
the toString() method from the superclass.
 Do you need to override the getArea() and getPerimeter()? Try them out.
 Override the setLength() and setWidth() to change both
the width and length, so as to maintain the square geometry.

2 Define an abstract class Shape. Define two classes Rectangle and Triangle from extending the
Shape class as shown blow. Test the call of getArea() methods by the instances of all the
classes
Re-write the abstract super class Shape into an interface, containing
only abstract methods, as follows

3 (The ComparableCircle class) Define a class named ComparableCircle that extends Circle
and implements Comparable. Implement the compareTo method to compare the circles on
the basis of area. Write a test class to find the larger of two instances of ComparableCircle
objects

.
Practical 6: To implement exception handling in java application.
1 Write a program that meets the following requirements:
■ Creates an array with 100 randomly chosen integers.
■ Prompts the user to enter the index of the array, then displays the corresponding element value. If
the specified index is out of bounds, display the message Out of Bounds.
(ArrayIndexOutOfBoundsException)

2 Write a program that prompts the user to read two integers and displays their sum. Your program
should prompt the user to read the number again if the input is incorrect. (InputMismatchException)

3 Define the Triangle class with three sides. In a triangle, the sum of any two sides is greater than the
other side. The Triangle class must adhere to this rule.
Create the IllegalTriangleException class, and modify the constructor of the Triangle class to throw
an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:
/** Construct a triangle with the specified sides */
public Triangle(double side1, double side2, double side3)
throws IllegalTriangleException {
// Implement it
}
Practical 7: To demonstrate the use of multithreading.

1 (Extend Thread) Write a program to create a thread extending Thread class and demonstrate the use
of slip() method.
2 (Implement Runnable) Write a program to create a thread implementing Runnable interface and
demonstrate the use of join() method.
3 (Synchronize threads) Write a program that launches 10 threads. Each thread adds 1 to a variable
sum that initially is 0. Define an Integer wrapper object to hold sum. Run the program with and
without synchronization to see its effect.
Practical 8: To demonstrate I/O from files.

1 (Remove text) Write a program that removes all the occurrences of a specified string from a text
file. For example, invoking java Practical7_1 John filename removes the string John from the
specified file. Your program should get the arguments from the command line.

2 (Count characters, words, and lines in a file) Write a program that will count the number of
characters, words, and lines in a file. Words are separated by whitespace characters. The file name
should be passed as a command-line argument.

3 (Write/read data) Write a program to create a file named Practical7.txt if it does not exist. Write 100
integers created randomly into the file using text I/O. Integers are separated by spaces in the file.
Read the data back from the file and display the data in increasing order.
Practical 9: To learn recursion and generics.

1 (Decimal to binary) Write a recursive method that converts a decimal number into a binary number
as a string. The method header is: public static String dec2Bin(int value)
Write a test program that prompts the user to enter a decimal number and displays its binary
equivalent.
2 (Distinct elements in ArrayList) Write the following method that returns a new ArrayList. The new
list contains the non-duplicate elements from the original list.
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list)
3 (Generic binary search) Implement the following method using binary search.
public static <E extends Comparable<E>>
int binarySearch(E[] list, E key)
Practical 10: To demonstrate the use of Collection framework.

1 (Store numbers in a linked list) Write a program that lets the user enter numbers from a graphical
user interface and displays them in a text area, as shown in Figure. Use a linked list to store the
numbers. Do not store duplicate numbers. Add the buttons Sort, Shuffle, and Reverse to sort,
shuffle, and reverse the list.

2 (Perform set operations on priority queues) Create two priority queues, {"George", "Jim",
"John", "Blake", "Kevin", "Michael"} and {"George", "Katie", "Kevin", "Michelle",
"Ryan"}, and find their
union, difference, and intersection.
3 (Guess the capitals using maps) Store pairs of 10 states and its capital in a map. Your program
should prompt the user to enter a state and should display the capital for the state
Practical 11 : To learn JAVA FX UI Controls.

1 (Color and font) Write a program that displays five texts vertically, as shown in Figure. Set a
random color and opacity for each text and set the font of each text to Times Roman, bold, italic,
and 22 pixels

2 (Display a bar chart) Write a program that uses a bar chart to display the percentages of the overall
grade represented by projects, quizzes, midterm exams, and the final exam, as shown in Figure
14.46b. Suppose that projects take 20 percent and are displayed in red, quizzes take 10 percent and
are displayed in blue, midterm exams take 30 percent and are displayed in green, and the final exam
takes 40 percent and is displayed in orange. Use the Rectangle class to display the bars.
Interested readers may explore the JavaFX BarChart class for further study.

3 (Display a rectanguloid) Write a program that displays a rectanguloid, as shown in Figure 14.47a.
The cube should grow and shrink as the window grows or shrinks.
Practical 12: To implement event handling and animation.
1 (Select a font) Write a program that can dynamically change the font of a text in a label displayed on a
stack pane. The text can be displayed in bold and italic at the same time. You can select the font name
or font size from combo boxes, as shown in Figure. The available font names can be obtained using
Font.getFamilies(). The combo box for the font size is initialized with numbers from 1 to 100.

2 (Control a moving text) Write a program that displays a moving text, as shown in Figure. The text
moves from left to right circularly. When it disappears in the right, it reappears from the left. The text
freezes when the mouse is pressed and moves again when the button is released.

3 (Create an image animator with audio) Create animation in Figure to meet the following
requirements:
■ Allow the user to specify the animation speed in a text field.
■ Get the number of iamges and image’s file-name prefix from the user. For example, if the user
enters n for the number of images and L for the image prefix, then the files are L1.gif, L2.gif, and so
on, to Ln.gif. Assume that the images are stored in the image directory, a subdirectory of the
program’s class directory. The animation displays the images one after the other.
■ Allow the user to specify an audio file URL. The audio is played while the animation runs.

You might also like