Question Paper
Exam Date & Time: 20-Nov-2024 (09:30 AM - 12:30 PM)
MANIPAL ACADEMY OF HIGHER EDUCATION
THIRD SEMESTER B.TECH END SEMESTER EXAMINATIONS, NOV 2024
OBJECT ORIENTED PROGRAMMING [DSE 2123]
Marks: 50 Duration: 180 mins.
DESCRIPTIVE
Answer all the questions.
Instructions to Candidates: Answer ALL questions Missing data may be suitably assumed
1) Design a class MobilePhone with attributes as brand, model, and price. Write a parameterized constructor to initialize these attributes. Add a method (5)
displayPhoneDetails() to print the phone details. Write a main method to create an array of MobilePhone objects using the parameterized constructor
A) and display the details of each phone.
B) Given string objects, s1 be "Manipal" and s2 be "manipal". Write the statements for the following operations: (3)
i) Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual.
ii) Create a substring of s1 from index 1 to index 4.
iii) Split "Welcome to Manipal" into an array of tokens delimited by a space.
C) Consider a superclass Appliance with a method turnOn(), and two subclasses WashingMachine and Refrigerator that override the turnOn() method. (2)
Given the following code, explain which turnOn() method will be called and why.
Appliance myAppliance = new WashingMachine();
myAppliance.turnOn();
2) Consider an e-commerce system that includes a superclass called Product with the following fields: productId, name, and price. The Product class (5)
should have a method to calculate the total price after applying a discount. Create two subclasses, Electronics and Clothing, that extend Product. Each
A) subclass should add specific fields relevant to the product type (e.g., warrantyPeriod for Electronics and size for Clothing) and override the method to
calculate the total price, applying different discount rates or conditions as appropriate.
B) Write a Java program that prompts users to enter two dates in the YYYY-MM-DD format. Use the SimpleDateFormat class to parse the input strings into (3)
Date objects. Then, use the appropriate method to compare the two dates and display one of the following messages:
i) "The first date occurs after the second date."
ii) "The first date occurs before the second date."
iii) "Both dates are equal."
C) Suppose you are developing a Java application that requires comparing objects of a custom class to determine if they represent the same entity based (2)
on specific attributes. Explain how the equals() method in the Java Object class can be overridden to implement this functionality. Discuss why it is
important to override the equals() method properly and provide an example scenario where incorrect implementation could lead to unexpected behavior
in your application.
3) Demonstrate the handling of different exceptions in Java by writing a program that simulates weather conditions based on the four seasons: Summer, (5)
Winter, Autumn, and Spring. The program should take user input for the current season and throw specific exceptions for invalid inputs, such as:
A)
i) A custom exception for an invalid season.
ii) An IllegalArgumentException if the input is not a string.
iii) A NullPointerException if no input is provided.
iv) Display relevant messages for the user based on the above exception types.
B) Describe how interfaces differ from abstract classes, and list the key reasons why interfaces are used in Java programming. (3)
C) Explain the importance of Java packages with respect to access modifiers. (2)
4) Write a multithreaded program in Java to perform the following tasks: (5)
Main Thread: Read the total duty hours of 'n' employees from the keyboard and store them in an array.
A)
Thread-1: Find the minimum hours worked by an employee.
Thread-2: Calculate the average hours worked by all employees.
Finally, the main thread should display the results obtained from Thread-1 and Thread-2.
B) Demonstrate the behavior of the finally block from try and catch blocks. Apply this concept by writing a Java program that divides two numbers and uses (3)
the finally block to display a message, regardless of whether an exception occurs.
C) The following program is used to copy the content of source.txt into dest.txt. Identify the error if there is any, and suggest your solution. (2)
Page 1 of 2
5) Consider a class called Advertisement that stores information about each product, including the product ID (integer, starting from 1), brand name (5)
(string), and product price (float). Create this class with the necessary input and output methods, and store the objects of this class in a file named
A) Advertisement_info.txt. Calculate the total price of all the products by reading these objects from the file and display the result. Write a Java program to
implement the solution to this problem.
B) Write a Java program that creates a vector to store 'n' double values. Calculate the mean of all values, then use an iterator to subtract the mean from (3)
each element, modifying the original vector. Finally, use a list iterator to display the elements in reverse order.
C) Given a random-access file namely numbers.txt containing 10 double values, and rw_obj is an object of RandomAccessFile. Write Java statements for (2)
the following using random-access file operation:
i) Read the 5th data item in the file.
ii) Insert a value: 22.5 at the beginning of the file.
-----End-----
Page 2 of 2