LR2 3
LR2 3
2. Problem statement: Study the following program on method overloading, and write
only the missing part in the answer window:
// Inputs
int n = sc.nextInt();
double d = sc.nextDouble();
3. Class Diagram:
Uses object
MethodOverload MethodOverloadTest
square(n:int):int +main(args[]:String):void {static}
square(d:double):double
Page 1 UD/OOP/LR/v1.0
4. Logic:
7. Conclusion
Data types in Java.
Method overloading.
Page 2 UD/OOP/LR/v1.0
1. Assignment Number: Lab Assignment LA2.3.2
2. Problem statement: Study the following program with real-life example where the
child buys a new car and stays in the existing home. Write only the missing part in the answer
window:
class Parent {
public void home() {
System.out.println("Existing home");
}
public void car() {
System.out.println("Existing car");
}
}
?????
3. Class Diagram:
Parent
+home():void
+car():void
extends
Page 3 UD/OOP/LR/v1.0
4. Logic:
1. Start : enter class TestOverride
2. Enter main()
3. Create object c1 of Child class
4. Call c1.home()
a. Enter home() of Parent class (inherited method)
b. Print "Existing home"
5. Call c1.car()
a. Enter car() of Child class (overrides car() of Parent)
b. Print "New Car"
6. Stop
Page 4 UD/OOP/LR/v1.0
6. Test cases and results:
SL Test Case Expected Result Observed Result Status
1 - Existing home Existing home Ok
New Car New Car
7. Conclusion
Inheritance in Java.
Method Overriding.
Page 5 UD/OOP/LR/v1.0
1. Assignment Number: Lab Assignment LA2.3.3
2. Problem statement: Imagine a factory with scrap pieces of different shapes. Shapes
were categorised under following types:
1: Circle
2: Rectangle
3: Triangle
Study the partly-developed program carefully for understanding the logic of shape entries,
area computation and displaying values. Identify the missing part in the program and write in
the answer window. Ensure to display "Ignoring wrong type: x" where x was the value of
invalid type. Ensure to display new line after each output line.
import java.util.Scanner;
// Superclass Shape
class Shape {
double area;
void computeArea() { System.out.println("Compute area based on specific shape"); };
void displayShape() { System.out.println("Display Shape dimensions with area"); };
}
// Subclass Circle
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
@Override
void computeArea() { super.area = Math.PI * r * r; }
@Override
void displayShape() { System.out.printf("Circle(%.2f) Area: %.2f\n", r, super.area); }
}
// Subclass Rectangle
class Rectangle extends Shape {
double l, b;
Rectangle(double l, double b) { this.l = l; this.b = b; }
@Override
void computeArea() { super.area = l * b; }
@Override
void displayShape() { System.out.printf("Rectangle(%.2f,%.2f) Area: %.2f\n", l, b,
super.area); }
}
// Subclass Triangle
Page 6 UD/OOP/LR/v1.0
class Triangle extends Shape {
double h, b;
Triangle(double h, double b) { this.h = h; this.b = b; }
@Override
void computeArea() { super.area = 0.5 * h * b; }
@Override
void displayShape() { System.out.printf("Triangle(%.2f,%.2f) Area: %.2f\n", h, b,
super.area); }
}
// Main class
public class Sheets {
?????
// main method
public static void main( String args[] ) {
Shape Shapes[] = new Shape[50];
int n, i=0, sheetType;
Shape s;
}
}
Page 7 UD/OOP/LR/v1.0
3. Class Diagram:
Uses object
Shape Sheets
extends
extends
extends
Page 8 UD/OOP/LR/v1.0
4. Logic:
1. Start : enter class Sheets
2. Enter main()
3. Create an array Shapes[] of Shape objects
4. Take input until Ctrl + D is pressed
a. sheetType = input from user
b. call createShape(sheetType,sc)
Select case according to sheetType
i. sheetType = 1 for circle
Print “Circle detected”
Input radius r
Create object c of Circle
o Initialise radius of Circle to r
Call c.computeArea()
o Calculate area = pi*r*r
return c
ii. sheetType = 2 for Rectangle
Print “Rectangle detected”
Input length l and breadth br
Create object rec of Rectangle
o Initialise length to l and breadth to br of Rectangle
Call rec.computeArea()
o Calculate area = l*br
return rec
iii. sheetType = 3 for Triangle
Print “Triangle detected”
Input height h and base b
Create object t of Triangle
o Initialise height to h and base to b of Triangle
Call t.computeArea()
o Calculate area = h*b
return t
iv. sheetType something else
Print “Ignoring wrong type:”+sheetType
return null
c. If returned object is not null, populate it into Shapes[]
5. Print the item number, shape and its area
6. Stop
Page 9 UD/OOP/LR/v1.0
5. Inputs, Outputs and Error handling:
Inputs for the Number sheetType and measurements
program
Expected Outputs Shape detected and area of those shapes
from the program
Error handling (for Check for invalid shape number and negative measurements
preventing wrong
data processing)
Page 10 UD/OOP/LR/v1.0
6. Test cases and results:
SL Test Case Expected Result Observed Result Status
7. Conclusion
Loops in Java.
Inheritance in Java.
Method overriding.
Objects in Java.
Object aray.
Page 11 UD/OOP/LR/v1.0