0% found this document useful (0 votes)
23 views4 pages

ETE 4141 Java Lab Question Bank

This document contains 8 questions for a III semester Object Oriented Programming Laboratory exam. The questions cover a range of Java programming concepts including classes and objects, inheritance, packages, threads, and applets. Students are asked to write Java programs that demonstrate their mastery of these core OOP concepts.

Uploaded by

rounak282002
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)
23 views4 pages

ETE 4141 Java Lab Question Bank

This document contains 8 questions for a III semester Object Oriented Programming Laboratory exam. The questions cover a range of Java programming concepts including classes and objects, inheritance, packages, threads, and applets. Students are asked to write Java programs that demonstrate their mastery of these core OOP concepts.

Uploaded by

rounak282002
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

Department of Artificial Intelligence and Data Science/

Department of Artificial Intelligence and Machine


Learning
QUESTION BANK FOR III SEMESTER
(Term: Nov 2023-Mar 2024)
Object Oriented Programming Laboratory (ADL37)

Marks: 50 Exam Hours: 03


Credits: 0:0:1

Part A
1 Write a java program to display the employee details (ID, Name, Age, Salary) for
three employees. Read details using Scanner class. Display the names of
employees in the order of their age.
2 Write a java program to create an array of objects and perform matrix
multiplication on two given matrices.
3 Write a java program to implement the following
a. Create a superclass called student with data members as USN, Name and marks
b. Create a subclasses called UG_student having the fields USN, Name, and total no.
of semesters (ToS) and marks for three subjects
c. Create a subless PG_Student that extends student class which has the data
members USN, Name and Total No. of Semesters ( ToS) and marks for three
subjects
d. Define the main function to display the details for three students using arrays
of objects.
4 Write a java program to implement stack operation (Push, Pop). Your class should
have an empty constructor. Create two objects to demonstrate the stack operation
for 10 items.
5 Write a java program to maintain the student details like USN, Dept names, 3
subject grades and SGPA in student package and keep the staff details such as
Staffid, StaffName, designation and subjects handled in a staff package. In main
class use these two packages details for Staff and Student classes and display the
student and staff information as requested by the user.
Department of Artificial Intelligence and Data Science/
Department of Artificial Intelligence and Machine
Learning
6 Java program to find area of rectangle & triangle using Interface.

7 Write a Java program to demonstrate exception handling using try, multiple catch
block and finally block. Throw an error in try block to handle array out of bound
index.
8 Write java program to demonstrate swing, Display the button on UI with “click
“text on the button.
Department of Artificial Intelligence and Data Science/
Department of Artificial Intelligence and Machine
Learning
PART – B
1. Write a Java Program that does the following
a. Create a super class called Car. The Car class has the following fields and
methods.
int speed; double regularPrice; String color; double getSalePrice();
b. Create a sub class of Car class and name it as Truck. The Truck class has the
following fields and methods.
int weight; double getSalePrice();
/If weight>2000,10% discount. Otherwise,20%discount.
c. Create a subclass of Car class and name it as Ford. The Ford class has the
following fields and methods.
int year; int manufacturerDiscount; double getSalePrice();
//From the sale price computed from Carclass, subtract the manufacturer
Discount.
d. Create a subclass of Car class and name it as Sedan. The Sedan class has the
following fields and methods.
int length; double getSalePrice();
//If length>20 feet, 5% discount, Otherwise, 10% discount.
e. Create MyOwnAutoShop class which contains the main() method. Perform the
following within the main() method.
f. Create an instance of Sedan class and initialize all the fields with appropriate
values.
g. Use super(...) method in the constructor for initializing the fields of the
superclass.
e. Create an instance of the Ford class and initialize all the fields with
appropriate values
f. Use super(...) method in the constructor for initializing the fields of the super
class.
g. Create an instance of Car class and initialize all the fields with appropriatevalues.
Display the sale prices of all instances.
2 Write a Java Program that does the following related to Inheritance:

a. Create an abstract class called ‘Vehicle’ which contains the 'hashelmet',‘year of


manufacture’ and two abstract methods ‘getData()’ and ‘putData()’. Demonstrate
the error when attempt is made to create objects of ‘Vehicle’.
b. Have two derived classes ‘TwoWheeler’ and ‘FourWheeler’, ‘FourWheeler’ is a
final class. Demonstrate the error when attempt is made to inherit from
‘FourWheeler’.
c. Your abstract class should have overloaded constructors that initializes
hashelmet and year of manufacture for TwoWheeler and FourWheeler
respectively.
d. ‘TwoWheeler” has data elements ‘Brand’, ‘Cost’, ‘EngineType’ (possible values “2
stroke”, “4 stroke”), and ‘Color’. Demonstrate the various ways in which the two
abstract methods can be dealt ‘getData()’ and ‘putData()’ can be dealt with by the
derived classes, ‘TwoWheeler’ and ‘FourWheeler’.
e. The sub-class of ‘TwoWheeler’ called ‘MyTwoWheeler’ has the element
‘OwnerName’.
f. Use the super keyword to initialize an object of ‘MyTwoWheeler’ with all the
values of its parent class data elements.
Department of Artificial Intelligence and Data Science/
Department of Artificial Intelligence and Machine
Learning
3 Write a Java Program that implements the following
a. Define a class SavingsAccount with following characteristics.
b. Use a static variable annualInterestRate to store the annual interest rate for
all account holders.
c. Private data member savingsBalance indicating the amount the saver
currently has on deposit.
D. Method calculateMonthlyInterest to calculate the monthly interest as
(savingsBalance * annualInterestRate / 12). After calculation, the interest shouldbe
added to savingsBalance.
h. Static method modifyInterestRate to set annualInterestRate.
i. Parameterized constructor with savingsBalance as an argument to set the
value of that instance.
Test the class SavingsAccount to instantiate two savingsAccount objects, saver1
and saver2, with balances of Rs.2000.00 and Rs3000.00, respectively. Set
annualInterestRate to 4%, then calculate the monthly interest and print the new
balances for both savers. Then set the annualInterestRate to 5%, calculate the next
month’s interest and print the new balances for both savers.
4 Write java program to create a package called AdvMath, which has two classes. In
main class use this package to display the result as requested by the user.

i. To calculate y = sin(x)+cos(x)+tan(x)

5 Write a java program to keep details of bank customer name and balance in
MyPack package, Initialize using contractors and define display function. In main
class use this package and pass the information (name and balance) and display
using display function.

6 Write a Java program to create 2 threads to perform the following operations.

Create Threads using Extend Thread class

Thread 1 will print all the prime numbers from 1 to 100. Thread will sleep for
0.5 second after printing every number.

Thread 2 will print all the numbers from 100 to 200. Thread will sleep for 0.5
second after printing every number.

7 Java Program to create 2 threads.

Create Threads using Runnable Interface

Print Prime Numbers from 1 to 50 using Thread1. Print Prime Numbers from 100
to 150 using Thread2. After Every number, put the thread to sleep.

8 Write java program to perform addition of two numbers using applet.

Common questions

Powered by AI

Java applets were traditionally used for embedding Java programs in web browsers, allowing for interactive user interfaces. However, applets have limitations, including security restrictions and browser compatibility issues. Meanwhile, Swing provides a more robust and flexible set of components for building standalone Java applications with rich GUIs. Unlike applets, Swing supports a more extensive range of components and improved organizational layout management. Swing applications are platform-independent and not confined to web environments, making them more versatile and widely used in contemporary Java development .

Threads in Java allow the simultaneous execution of operations, optimizing performance by utilizing multi-core processors efficiently. This is beneficial for tasks that can run concurrently without needing a sequence, such as computing prime numbers and managing user interface refreshes. For example, creating separate threads to find prime numbers in different ranges allows the application to perform extensive calculations simultaneously . However, threads can introduce complexity, such as synchronization issues, requiring careful management to avoid race conditions and deadlocks. Furthermore, improperly handled threads can waste resources or lead to unpredictable behavior .

Java's exception handling framework enhances software robustness by allowing developers to gracefully manage errors that occur during runtime, such as array index out-of-bounds errors. By using try-catch blocks, programmers can anticipate potential exceptions and implement error-handling code to prevent the program from crashing, thereby preserving user experience and data integrity . Additionally, the finally block ensures that necessary cleanup operations are performed, enhancing resource management .

Inheritance in Java allows for the creation of a base class, 'Vehicle,' which serves as a blueprint with common attributes like 'year of manufacture.' Subclasses such as 'TwoWheeler' and 'FourWheeler' inherit these properties and can add specific attributes or override methods to serve their unique purposes. This demonstrates polymorphism, where a general class type can refer to objects of different subclasses. The final class 'FourWheeler' cannot be inherited, ensuring some classes remain unchanged for stability . Inheritance thus promotes code reuse and organizational clarity .

Cohesion refers to the degree to which the elements of a module or package belong together. High cohesion is desirable as it increases the maintainability and reusability of the software. In Java, packages such as `AdvMath` for mathematical operations and `MyPack` for bank detail management exemplify this principle by grouping related classes and functions within their respective domains. This organization ensures that classes are highly related within a package, optimizing module comprehensibility and reducing the interdependencies between unrelated components, facilitating easier maintenance and scalability . Having dedicated packages for specific functionality aligns with best practice by keeping module responsibility clear and focused .

Using interfaces to define geometric calculations, such as finding the area of shapes like rectangles and triangles, promotes consistent method usage across different classes implementing the interface. It allows for polymorphism, where different shape classes can be used interchangeably if they implement the same interface, enhancing code flexibility and scalability . However, the challenge lies in ensuring each class correctly implements the required methods, as interfaces do not provide method implementations. It also requires thorough understanding of interface usage to effectively utilize its benefits .

Inheritance allows derived classes to inherit common methods and fields from a base class, reducing redundancy and enhancing maintainability as shared code needs only to be maintained in one place. For example, in a class hierarchy involving a superclass `Car` with subclasses `Truck` and `Sedan`, shared attributes like speed and color are defined once in the `Car` class, and subclasses inherit these automatically. Future updates to shared behavior only need changes in the superclass, simplifying maintenance . However, careful design is essential to prevent excessive coupling and ensure derived classes fit the intended use .

Polymorphism allows different types of savings accounts to be managed through a common interface, enhancing the flexibility and scalability of account management systems. By defining a base class `Account` with methods like `calculateMonthlyInterest`, various account types such as `SavingsAccount`, `BusinessAccount`, or `StudentAccount` can inherit from it and override or extend these methods as needed. This way, a banking application can handle a collection of accounts polymorphically, calling the same methods regardless of the specific account type, simplifying the code for interest calculation and other operations .

Abstract classes and interfaces provide distinct yet complementary mechanisms to define and implement real-world problems in OOP. Abstract classes allow sharing common base functionality while deferring specific implementations to subclasses. This is ideal when different objects share some behavior but also maintain unique aspects — e.g., a `Vehicle` class with `TwoWheeler` and `FourWheeler`. Interfaces ensure flexibility and standardization by defining method signatures without implementation, allowing multiple disparate classes to share certain functionalities, such as calculating geometric properties or executing tasks. This decouples the 'what' from the 'how,' promoting system extensibility and plugin-like architecture .

Encapsulation in a banking application is crucial for protecting sensitive customer data and ensuring that only authorized operations interact with account details. By encapsulating customer account properties like balance and account number within classes and controlling their access via public methods, encapsulation prevents unauthorized access and modification. This is achieved using access modifiers like private for data members and public for methods, aligning with best practices for data security and ensuring integrity in banking systems . Methods like `getBalance` or `deposit` safely interact with the data while maintaining strict control over how and when data changes, contributing to system reliability .

You might also like