Вы находитесь на странице: 1из 14

FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING UNIVERSITI TUN HUSSEIN ONN MALAYSIA

LABORATORY REPORT BEC 20702 OBJECT ORIENTED PROGRAMMING


Experiment Code-Title Date Programs Code Students Name Instructors Name Instructor Verification (A) E4 - Method Date of Experiment : 05/04/2012 2 BEC Khairul Nizam Bin Anuar (CE110121) Mrs. Noraisah Binti Sudin Assessment (B) PART Discussion A: /10 B: /20 C: /30 /10 /5 / 75 Date of Submission : 09/04/2012

Signature :

Note :
Total A : /5

Conclusion Total B :

Total (A + B)
Instructors Comment Submission Stamp

/ 80

TABLE OF CONTENT

CONTENT Table of content Part A : Review Question Part B : Introducing Methods in Java Task 1 : Calling a void Method Task 2 : Call a method with a return value/variable. Part C : Problem statement Discussion & Conclucion Reference

PAGE 1 2-3 45

6-9 10 - 11 12 13

PART A : REVIEW QUESTION

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. 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


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

Output :

1. 2.

We have method in line 26, method name as calculateBMI 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.


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 :

Modify/add some more codes to ask the user if the heating process compensate inefficiency. If so, add ~10% to the electricity amount to overcome inefficiencies.

Source code :

Output :

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 cant 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. 2. 3. 4. 5.

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

13

Вам также может понравиться