CSE1115_Final_221
CSE1115_Final_221
Answer all the five questions from [Question 1 to 5]. There are two questions named Question 5. You need to
answer one of them.
Question 1 [4 + 4]
A. Suggest changes to the provided three classes for the following tasks:
i. Make the variable “m” both accessible from “method2” of class A and read-only once initialized
ii. If any class inherits class “B”, prevent the “method1” from being overridden. But it should be allowed to
override if any class inherits from class “A” or “C”
iii. Make sure class “C” cannot be inherited
N.B. Just write only the modified lines
B. Create and assign an object of Vehicle using anonymous inner class for both of Line 6 and 7 to produce
output “Uses LPG” from line number 9 and "Uses Jet Fuel" from Line number 10:
Question 2 [8]
Suppose you have a file “id.txt” that contains the ids of multiple UIU students. Write a java code to write the odd ids
in the id.txt file to another file called “odd.txt” and the even ids in the id.txt file to another file called “even.txt”.
Check the following example for clarification:
Page 1 of 4
Question 3 [8]
Consider the following class “CreditCard”. The constructor and the withdraw method should throw a user-
defined exception named “InvalidTxnException” with proper messages for the following cases:
• If the amount value passed to the constructor/method is negative, they throw the
InvalidTxnException with the following message:
“-5000 is not a valid amount for the requested transaction”
Here, -5000 is the value passed as the amount from main at Line No: 18
• If the withdrawal of the amount passed to the withdraw method crosses the max credit limit, throw
the InvalidTxnException with the following message:
“4000 cannot be withdrawn with current credit of 7000 for your limit of 10000”
Here, 4000 is the value passed as the amount from main at Line No: 25
Now write the user-defined exception: InvalidTxnException class with a proper super call to set messages. And
rewrite only the constructor and the withdraw method of CreditCard class to throw exceptions based on proper
conditions. Also, handle the InvalidTxnException from main for Line No: 18 & 25 using the try-catch keywords.
Page 2 of 4
Question 4 [4 + 4]
For the following GUI question, write a single code to answer both a & b.
a) Complete the following code to create a GUI as shown in image 1. Assume all necessary classes are imported
b) Provide button click handling code so that when button 1 (top-left) is pressed, the buttons show texts like
image 1. When button 4 (bottom-right) is pressed, the buttons show texts like image 2
image 1
image 2
Question 5 [8]
Consider the following Player class:
Now complete only the missing codes for the following comparator_main class:
/* Task 2: Using the add() method add objects with the followings to the
Arraylist
55, "Karim", "Bangladesh"
14, "Ponting", "Australia" */
/* Task 3: Sort the Arraylist in, ascending order of jersey number using a
comparator for comparing objects of Player class [You can also
define the Comparator as a separate class if you want]
*/
}
}
Page 3 of 4
Or,
Question 5 [6 + 2]
A. Consider the MyStack class that can keep at most five characters in the stack. The Producer thread pushes
an element to it while the Consumer thread pops from it. However, the Producer thread cannot push an
element while there are already five elements in it, and it must wait then until an element is popped.
Similarly, the Consumer must wait to pop any element till the stack is empty. For implementing the above
scenario, write codes for the following tasks to the mentioned sections of the following code snippets:
i. Add wait() method in pop() in MyStack class
ii. Start threads for object p and c in StackTest class
iii. Add sleep() in run() methods of Producer and Consumer classes
N.B. Just write only the added lines of codes for each class
Page 4 of 4