Lab Report 4
Lab Report 4
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
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.
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.
Output : 5*4*3*2*1
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
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