Lab Report 4
Lab Report 4
ENGINEERING
LABORATORY REPORT
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
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.
return width*length*height;
Based on above method, create one more method to apply your understanding on the concept in
Q1(a).
return width*length*height;
2. a) Predict the return value, return by xMethod and assume v and w receive 3 and 6 respectively.
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;
3. Determine the return value returned by recursive function below, assume that n receive 5 from the
caller.
Output : 5*4*3*2*1
3
Calculate BMI
Source code :
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
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.
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 :
13