CSE1115 Mid 231
CSE1115 Mid 231
CSE1115 Mid 231
2. Consider the following three classes and the output of the program. Then answer (a),(b) and (c).
package M1;
package M1.M2;
if (m1.eat(h1)==true) {
System.out.println("Monster has eaten human "+h1.id);
}else{
System.out.println("Human escaped");
}
System.out.println(m1.scare(h2.bravery));
}
}
Expected Outcome:
(a) Correct the given code (don’t modify the methods yet) by editing or adding any lines. You cannot
remove any line from the code. Also write necessary getter methods (Assume the variables are read-
only). 2]
(b) Observe the output given and write necessary constructors and blocks accordingly. [3]
(c) Implement the eat() and scare() methods. The eat() method checks the intelligence of a human- if it’s
“high”, returns true, otherwise it calls the increaseWeight() method and then returns false. The
scare() method checks a human’s bravery- if it's false, it prints a line: “Human scared." otherwise it will
print: "Human is too brave to scare." [3]
3 of 6
3. Given the following information write the necessary code to implement AdvancedCalculator
class. [6]
Expected output
Subtraction result: -1
Summation result: 15
5. Find out the errors in the following code and explain the reasons of Errors in those particular lines. [3]
class Point{
int x, y;
final int f = 10;
final Point p = new Point(1, 2);
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
class Check{
public static void main(String args[]){
Point point = new Point(5, 5);
point.f = 5;
point.p.x = 10;
point.p = new Point(1, 5);
}
}
5 of 6
class Main{
public static void main(String[] args) {
Cake cake[];
a. calcPrice() method in the Cake class is used to calculate the total price of a cake. You need to override
this method in each of the derived classes: OrderCake and ReadymadeCake. Price is calculated as per the
following rules: [4]
Also, override another method printDetails() in each class. This method will print the information about
a particular cake according to the following format:
Name: <name>
Rate: <rate>
Weight/Quantity: <value>
Total Price: <price>
Now, complete the OrderCake and ReadymadeCake class by overriding calcPrice() and
printDetails() methods.
b. Create an array of three cake objects. First two objects are of OrderCake type and later one object is of
ReadymadeCake type in the main() method. Assign some values to the class attributes while creating those
objects. If you successfully create the array, your output will look like as follows: [2]
Name: OrderCake
Rate: 150
Weight: 3
Total Price: 450
… …. … …. ….. …
Name: ReadymadeCake
Rate: 200
Quantity: 2
Total Price: 400
…. …… …… …… …… ..