0% found this document useful (0 votes)
60 views14 pages

Lab Report 4

The document contains a laboratory report for an Object Oriented Programming experiment. It includes: 1. An introduction to methods in Java, with examples of void and returning value methods. 2. Two tasks - the first calls a void method to calculate BMI, the second calls a method to calculate power consumption that returns a value. 3. A problem statement to calculate kinetic energy, with examples of calling the method to output energy values for different masses and velocities. The report demonstrates understanding of different method types in Java and calling methods to perform calculations and output results.

Uploaded by

Hassan Siddiqui
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)
60 views14 pages

Lab Report 4

The document contains a laboratory report for an Object Oriented Programming experiment. It includes: 1. An introduction to methods in Java, with examples of void and returning value methods. 2. Two tasks - the first calls a void method to calculate BMI, the second calls a method to calculate power consumption that returns a value. 3. A problem statement to calculate kinetic energy, with examples of calling the method to output energy values for different masses and velocities. The report demonstrates understanding of different method types in Java and calling methods to perform calculations and output results.

Uploaded by

Hassan Siddiqui
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/ 14

FACULTY OF ELECTRICAL AND ELECTRONIC

ENGINEERING

UNIVERSITI TUN HUSSEIN ONN MALAYSIA

LABORATORY REPORT

BEC 20702 – OBJECT ORIENTED PROGRAMMING


Experiment Code-
E4 - Method
Title

Date Date of Experiment : Date of Submissio


05/04/2012 09/04/2012

Program’s Code 2 BEC

Student’s Name Khairul Nizam Bin Anuar (CE110121)

Instructor’s Name Mrs. Nor’aisah Binti Sudin

Instructor Assessment (B)


Verification (A)

Signature : PART A: B: C:
/10 /20 /30

Discussion

Note : Conclusion

Total A : Total B :
TABLE OF CONTENT

CONTENT PAGE
Table of content 1
Part A : Review Question 2-3
Part B : Introducing Methods in Java 4–5
Task 1 : Calling a “void” Method
Task 2 : Call a method with a return value/variable. 6-9
Part C : Problem statement 10 - 11
Discussion & Conclucion 12
Reference 13

PART A : REVIEW QUESTION

1
1. a) Briefly explain the concept of overloading method.
- Overloading is the ability to define more than one method with the same name in a class. The
compiler is able to distinguish between the methods because of their method signatures.
- In Java it is possible to define two or more methods within the same class that share the same name,
as long as their parameter declarations are different. When this is the case, the methods are said to
be overloaded, and the process is referred to as method overloading. Method overloading is one of
the ways that Java implements polymorphism.

b) The following example shows a method to calculate volume of a box.

static int volume ( int width, int length, int height)

return width*length*height;

Based on above method, create one more method to apply your understanding on the concept in
Q1(a).

static double volume ( double width, double length, double height)

return width*length*height;

2. a) Predict the return value, return by xMethod and assume v and w receive 3 and 6 respectively.

static double xMethod (int v, int w)


{
double n = (++v/2) * w;
return n;
}

Output : 12

b) Modify the xMethod in Q2(a), so that you can printout the n value without returning it back to the
caller.

2
static double xMethod (int v, int w)

double n = (++v/2) * w;

System.out.println("N is equal to " + n);

3. Determine the return value returned by recursive function below, assume that n receive 5 from the
caller.

static int xRecursive ( int n)


{
if (n == 1)
return 1;
else
return 3 * xRecursive(n-1);
}

Output : 5*4*3*2*1

PART B : Introducing Methods in Java

Task 1 : Calling a “void” Method

3
Calculate BMI

Classification BMI (kg/m²)


Underweight <18.5
Normal range 18.5 – 22.9
Overweight at risk 23 - 24.9
Obese I 25 – 29.9
Obese II ≥30
BMI Classification Table

Source code :

1. //Program Description : 16. System.out.print("Enter 32. status = "Underweight";


Display Information & BMI your height( in meter): ");
Status 33. else if (BMI >=18.5 &&
17. height = BMI <=22.9)
2. //Program/Class Name : MyInput.readFloat();
BMIStatus.java 34. status = "Normal Range";
18.
3. //Programmer : Nor'aisah 35. else if (BMI >= 23 &&
Sudin 19. System.out.print("What is BMI <=24.9)
your weight (in kg): ");
4. //Number Of Experiment : 36. status = "Overweight at
E4-PART B -Task1 20. weight = Risk";
MyInput.readFloat();
5. 37. else if (BMI >=25 && BMI
21. <=29.9)
6. class BMIStatus {
22. calculateBMI(name, 38. status = "Obese I";
7. height,weight);
39. else if (BMI >=30)
8. public static void 23.
main(String args[]) { 40. status = "Obese II";
24. }//End main method
9. int age; 41. else
25.
10. float height,weight, BMI; 42. status ="Not in the range &
26. public static void can’t be classified";
11. String name; calculateBMI (String n, float h,
float w) { 43.
12.
27. 44. System.out.println( n + "
13. System.out.println("What is you are in " + status + " with "
your name ?"); 28. String status; + BMI + " BMI value");

14. name = 29. double BMI = w 45.


MyInput.readString(); /Math.pow(h,2);
46. } //End CalculateBMI
15. 30. method

31. if (BMI < 18.5) 47. }//End BMIStatus class

Output :

4
1. We have method in line 26, method name as calculateBMI
2. Method in line 26 is math class method. We need this because it used to operate the calculations for
BMI calculation

Task 2 : Call a method with a return value/variable.

5
Case study 2: Create a power consumption calculator program which can calculate
amount of Power in(kWh) required to heat a quantity of water. The following table
shows the related formula used. It assumes 100% efficiency, with no losses.

Type, compile and debug the program. If there are errors or warnings
occurred, correct them first and re-debug. Then, execute and analyze the
output.

Source code :

Output :

6
Modify/add some more codes to ask the user if the heating process

7
compensate inefficiency. If so, add ~10% to the electricity amount to overcome
inefficiencies.

Source code :

8
Output :

9
PART C :

Problem Statement 2: Kinetic Energy (Ek) is defined as the work needed to accelerate a body of a given mass
from the rest to its current velocity. Simulate the Kinetic Energy Calculator using Java
program to help the user in determining the Ek value. The formula to calculate the Ek is as
follows:
Kinetic Energy : Ek = ½ mv2
where
Ek = Kinetic Energy (in Joules),
M = Mass of object,(in kg)
V = Velocity or Speed of object,(in m/s).

Source code :

10
Output :

Determine the Kinetic Energy of a 500kg roller coaster train which moves at a
speed 20m/s.

A 55 kg woman is running with a speed 3.87m/s. Calculate the kinetic energy


needed to do this exercise.

11
DISCUSSION

Part B :
 In sourde code part b task 2, it has contain 2 method structure, 1st method is main method and second
method is math class method name as calPower.

 This is for math calculation to calculate amount of Power in(kWh) required to heat a quantity of water.

 For the first time debuging this program contain 2 error at line no 22 and no 29, this happen because var
t2 declare as double but for T declare as int, int declaration can’t read for double declaration

 Similar situation at line 29. To correct the program, just change declaration for T from int T to double T
and also change at line 29 from static int calPower to static double calPower.
 If the heating process compensate inefficiency, to overcome inefficiencies with add 10% to the
electricity amount I use switch case control loop for this program.

Part C :
 In this task my source code also have 2 method, first main method and second method is math class
method name as KE.

 This class method for do calculation how to calculate kinetic energy using equation given by applying
inside program.

CONCLUSION
 For this time laboratory, it is how I can learn and understand how to create and invoke methods and
pass arguments to methods in Java Programs.
 After completing this lab I can be able identify suitable method in math class to execute the math
formula in java statement and create user defined methods to java programs.
 Also can propose correct user defined methods and divide the program into subtasks in solving the
problems.
 And then I also can be able implement the concept of parameter passing to methods to illustrate given
problem statements and interact with the user

12
REFERENCE :

1. Retrieved at website http://java.about.com/od/o/g/overloading.htm


2. Retrieved at website http://www.javasamples.com/showtutorial.php?tutorialid=284
3. Retrieved at website http://www.roseindia.net/help/java/m/method-overloading-in-java.shtml
4. Retrieved at website http://rationalpi.wordpress.com/2007/01/29/main-methods-in-java/
5. Object Oriented Design Heuristics Addison-Wesley, Riel, Arthur 1996

13

You might also like