LAB 1: INTRODUCTION TO PROGRAMMING

advertisement
LAB 5: METHODS.
Methods are often called modules. They are like miniature programs; you can put them together to
form a larger program.
LAB SHEET 5.1: Defining a method, Calling a Method
ESTIMATED TIME
:
1.5 hours
OBJECTIVE
:
To define methods.
To invoke methods with a return value.
REQUIREMENTS
:
JCreator 5.00 Pro
PROCEDURE
:
Read the question and write the program. Then run the program and
get the correct output.
CONCLUSION
:
Students are able to define methods and invoke methods with a return
value.
DISCUSSION / RESULT / EXERCISE
Consider the following program segment:
import java.util.*;
public class LabSheet5_1
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int
num;
double dec;
}
public static int one(int x, int y)
{
1
}
public static double two(int x, double a)
{
int first;
double z;
}
}
a.
Write the definition of method one so that it returns the sum of x and y if x is greater than y;
otherwise, it should return x minus 2 times y.
b.
Write the definition of method two as follows:
i.
Read a number and store it in z.
ii.
Update the value of z by adding the value of a to its previous value.
iii.
Assign the variable first the value returned by method one with the parameters 6
and 8.
iv.
Update the value of first by adding the value of x to its previous value.
v.
If the value of z is more than twice the value of first, return z; otherwise, return 2
times first minus z.
c.
Write a Java program that tests parts a and b. (Declare additional variables in the method
main, if necessary.)
2
LAB SHEET 5.2: Calling a Method, Passing Parameters by Values
ESTIMATED TIME
:
1.5 hours
OBJECTIVE
:
To pass arguments by value.
To invoke methods with a return value.
REQUIREMENTS
:
JCreator 5.00 Pro
PROCEDURE
:
Read the question and write the program. Then run the program and
get the correct output.
CONCLUSION
:
Students are able to pass arguments by value and invoke methods
with a return value.
DISCUSSION / RESULT / EXERCISE
The following formula gives the distance between two points (𝑥1 , 𝑦1 ) and (𝑥2 , 𝑦2 ) in the Cartesian
plane:
2
√(𝑥2 − 𝑥1 )2 + (𝑦2 − 𝑦1 ) .
Given the center and a point on a circle, you can use this formula to find the radius of the circle.
Write a program that prompts the user to enter the center and a point on the circle. The program
should then output the circle’s radius, diameter, circumference, and area. Your program must have
at least the following methods:
a.
b.
c.
d.
e.
distance: This method takes as its parameters four numbers that represent two points in
the plane and returns the distance between them.
radius: This method takes as its parameters four numbers that represent the center and a
point on the circle, calls the method distance to find the radius of the circle, and returns
the circle’s radius.
circumference: This method takes as its parameter a number that represents the radius
of the circle and returns the circle’s circumference. (If 𝑟 is the radius, the circumference is
2𝜋𝑟)
area: This method takes as its parameter a number that represents the radius of the circle
and returns the circle’s area. (If 𝑟 is the radius, the area is 𝜋𝑟 2 .)
Assume that 𝜋 = 3.1416.
3
Download