0% found this document useful (0 votes)
30 views11 pages

LR2 3

The document is a lab report that includes: 1) Three assignments on method overloading, inheritance, and polymorphism in Java. 2) For each assignment, the problem statement, class diagram, logic, test cases and results are provided. 3) The third assignment is only partially completed and asks to identify the missing part in the program to handle shape objects of different types (circle, rectangle, triangle) by displaying their areas.

Uploaded by

Shree Shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

LR2 3

The document is a lab report that includes: 1) Three assignments on method overloading, inheritance, and polymorphism in Java. 2) For each assignment, the problem statement, class diagram, logic, test cases and results are provided. 3) The third assignment is only partially completed and asks to identify the missing part in the program to handle shape objects of different types (circle, rectangle, triangle) by displaying their areas.

Uploaded by

Shree Shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab Report LR2.

Course: CS593 – Object Oriented Programming


Department: Computer Science and Engineering, Techno Main Salt Lake
Name: Shreyashi Muhury Roll 13000119057
:

1. Assignment Number: Lab Assignment LA2.3.1

2. Problem statement: Study the following program on method overloading, and write
only the missing part in the answer window:

???? // Your code

public class MethodOverloadTest {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Inputs
int n = sc.nextInt();
double d = sc.nextDouble();

// Instantiate MethodOverload class for using square methods


MethodOverload mol = new MethodOverload();

// Call square method


System.out.printf("Square of %d: %d\n", n, mol.square(n) );

// Call overloaded square method


// BTW, overloading also possible with different number or order of parameters
System.out.printf("Square of %f: %.2f\n", d, mol.square(d) ); // Overloaded method
}
}

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: 

1. Start : enter class MethodOverloadTest


2. Enter main()
3. Take input numbers n and d
4. Create object mol of MethodOverload
5. Call int square(n) of MethodOverload class
a. Matching integer parameters are encountered and n*n is returned in integer
data type
6. Print square
7. Call double square(n) of MethodOverload class
a. Matching double parameters are encountered and n*n is returned in double
data type
8. Print square
9. Stop

5. Inputs, Outputs and Error handling: 

Inputs for the Integer number n and double number d.


program
Expected Outputs Square of n and d.
from the program
Error handling (for Display ERROR if anything other than numbers are entered.
preventing wrong
data processing)

6. Test cases and results:


SL Test Case Expected Result Observed Result Status
1 Enter Square of -2: 4 Square of -2: 4 Ok
n = -2 Square of 1.500000: 2.25 Square of 1.500000:
d = 1.5 2.25
2 Enter Square of 5: 25 Square of 5: 25 Ok
n=5 Square of 2.500000: 6.25 Square of 2.500000:
d = 2.5 6.25
3 Enter ERROR ERROR Ok
n =7
month = “abc”

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");
}
}

?????

public class TestOverride {


public static void main(String[] args) {
Child c1 = new Child();
c1.home();
c1.car();
}
}

3. Class Diagram:
Parent
+home():void
+car():void

extends

TestOverride Uses object Child

+main(args[]:String):void {static} +car():void

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

5. Inputs, Outputs and Error handling: 


Inputs for the -
program
Expected Outputs Existing home
from the program New Car
Error handling (for - (No input)
preventing wrong
data processing)

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;

Scanner sc = new Scanner(System.in);

// Loop inputs taking as long as Ctrl-d is not pressed


while (sc.hasNextInt()) {
// Input sheet type (1: Circle, 2: Rectangle, 3: Traiangle)
sheetType = sc.nextInt();
s = createShape(sheetType, sc); // Create Shape object
if (s != null) Shapes[i++] = s; // Populate into Shapes array
}

n = i-1; // No. of sheets

for (i=0; i<=n; i++) {


System.out.print(i+": ");
Shapes[i].displayShape();
}

}
}

Page 7 UD/OOP/LR/v1.0
3. Class Diagram:
Uses object
Shape Sheets

area:double +main(args[]:String):void {static}


computeArea():void createShape(sheetType:int,
displayShape():void sc:Scanner):Shape {static}

extends
extends
extends

Circle Rectangle Triangle


r:double l:double h:double
b:double b:double
Circle(r:double) Rectangle(l:double,b:double) Triangle(h:double,b:double)
computeArea():void computeArea():void computeArea():void
displayShape():void displayShape():void displayShape():void

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

1 Enter Rectangle detected Rectangle detected Ok


2 Ignoring wrong type: 9 Ignoring wrong type: 9
56 Circle detected Circle detected
9 0: Rectangle(5.00,6.00) Area: 30.00 0: Rectangle(5.00,6.00) Area: 30.00
1 1: Circle(5.00) Area: 78.54 1: Circle(5.00) Area: 78.54
5
2 Enter Triangle detected Triangle detected Ok
3 Ignoring wrong type: 9 Ignoring wrong type: 9
56 Circle detected Circle detected
9 0: Triangle(5.00,6.00) Area: 30.00 0: Triangle(5.00,6.00) Area: 30.00
1 1: Circle(7.00) Area: 153.93 1: Circle(7.00) Area: 153.93
7
3 Enter Ignoring wrong type: 9 Ignoring wrong type: 9 Ok
9 Circle detected Circle detected
1 ERROR ERROR
-5

7. Conclusion 
Loops in Java.
Inheritance in Java.
Method overriding.
Objects in Java.
Object aray.

Page 11 UD/OOP/LR/v1.0

You might also like