Methods Exercises

advertisement
COSC236 methods: void vs value-returning
 void method: name begins with a verb
 vale-returning method name is noun or adjective describing the returned result
Is each of the following void or value-returning?
1.
2.
3.
4.
5.
6.
7.
8.
displayMenu
square
dispSum
sum
printDimensions
perimeter
max
isEven
void
value-returning
Write the declaration for each:
1.
2.
3.
4.
5.
6.
7.
8.
displayMenu
square
dispSum
sum
printDimensions
perimeter
max
isEven
public static void displayMenu ()
public static int square ()
void method
Name begins with Verb
May return 0, 1, or many values as arguments
General purpose
Standalone call
value-returning method
Name usually a noun or adjective
Returns 1 value only
Usually mathematical
Call is part of an expression
Some example value-returning methods:
min, max, cos, sin, sum, avg, diff, isPrime
Some example void methods:
showDiff, calcAvg, getIput, validateDate, displayHeader
1
Arguments:
 Formal arguments are declared with the method
 A type (int, double, char, etc) must be included with the formal argument
 There may be 0, 1, or many arguments
 pass by value
o passed into the method
o default
o values are known before calling the method
o values are not changed inside the method
 pass by reference
o passed into and/or out of the method
o values are known after calling the method
o values are changed inside the method
o must be object or reference variable
Finish the declarations from above, fully specifying each argument
1.
2.
3.
4.
5.
6.
7.
8.
displayMenu
square
dispSum
sum
printDimensions
perimeter
max
isEven
public static void displayMenu ()
public static int square (int num)
2
Method Definition
 Include declaration and body
 body should contain code to implement what their name describes (no more – no less)
 value-returning methods must have return statement
 Any variables used must be declared as arguments or local variables
Write the code for the methods/procedures
1. displayMenu (display 1. Spanish 2. French 3. German 4. English 5. Exit)
2. square
3. dispSum
4. sum
5. printDimensions
6. perimeter
7. max
8. isEven
Show a sample call to each of the procedures/methods you have implemented above:
1. displayMenu
2. dispSquare
3. square
4. sum
5. printDimensions
6. perimeter
7. max
8. isEven
Calling a Method
 To call a method, you send actual arguments
 Actual arguments do not need to have the same name as the formal arguments
 Formal arguments and actual arguments must match in number and in type
 void method calls are standalone:
 Method calls are part of an expression, the value returned must be stored, used, or printed
3
Methods Exercise:
1. In the program fragment shown below, identify the following items: method heading, method
body, method definitions, formal arguments, actual arguments, method call, and local variables:
public class Exercise
//line1
{
//line2
public static void main (String[] args)
//line3
{
//line4
int c;
//line5
double y;
//line6
char z;
//line7
//..
//line8
hello(x, y, z);
//line9
//..
//line10
hello(x + 2, y – 3.5, 'S');
//line11
//..
//line12
}
//line13
public static void hello(int first, double second,
//line14
char ch)
//line15
{
//line16
int num;
//line17
double y;
//line18
//..
//line19
}
//line20
}
2. For the program above, fill in the blanks below with variable names to show the matching that
occurs between the actual and formal argument list in each of the two calls.
First call to hello
Second call to hello
Formal
Actual
Formal
Actual
a. _____
_____
_____
_____
b. _____
_____
_____
_____
c. _____
_____
_____
_____
3. For the program above, list the line numbers in the order they will be executed.
4.
(T or F)In Java, the names of the corresponding formal and actual arguments must be the same.
4
Sample Problem:
processSalaries
salary
taxRate
incomeTax
if (salary >= 40000)
taxRate = .3;
else if (salary >= 30000)
taxRate =.25;
else if (salary >= 20000)
taxRate = .2;
else if (salary >= 10000)
taxRate = .15;
else if (salary >= 5000)
taxRate = .06;
else
taxRate = 0;
}
5
Consider the following method headings:
public static int test(int x, char ch, double d, int y)
public static double two(double d1, double d2)
public static char three (int x, int y, char ch, double d);
Answer the following questions:
a. How many arguments does the method test have? What is the type of the method test?
b. How many arguments does method two have? What is the type of method two?
c. How many arguments does method three have? What is the type of method three?
d. How many actual arguments are needed to call the method test? What is the type of each
argument, and in what order should you use these arguments in a call to the method test?
e. Write a Java statement that prints the value returned by the method test with the actual
arguments 5, 5, 7.3, and 'z'.
f. Write a Java statement that prints the value returned by the method two with the actual
arguments 17.5 and 18.3 respectively.
g. Write a Java statement that prints the next character returned by the method three. Use your
own actual arguments.
Consider the following statements:
double num1, num2, num3;
int int1, int2, int3;
double value;
num1 = 5.0; num2 = 6.0; num3 = 3.0;
Int1 = 4; int2 = 7; int3 = 8;
And the method heading:
public static double cube(double a, double b, double c)
Which of the following statements are valid? If they are invalid, explain why:
a. value = cube(num1, 15.0, num3);
b. System.out.println(cube(num1, num3, num2));
c. System.out.println(cube(6.0, 8.0, 10.5));
d. System.out.println(cube(num1, num3));
e. System.out.println(cube(num1 + " " + num3);
f. value = cube(num1, int2, num3);
g. value = cube(7, 8, 9);
6
5. Write a method isEven() that uses the remainder operator (%) to determine whether an integer
is even. The method should take an integer argument and return true if an integer is even and
false otherwise. Incorporate this method into an application that inputs a sequence of integers
(one at a time) and determines counts the number of even numbers and odd numbers.
6. Write an application that prompts the user for the radius of a circle and uses a method called
circle Area to calculate the area of the circle.
7. What is the output
public class Test
{
public static void main(String[] args)
{
int a = 1;
System.out.println(a);
testMethod(a);
System.out.println(a);
}
public static void testMethod(int f)
{
System.out.println(f);
f= 2;
System.out.println(f);
}
}
8. What is the output
import java.awt.*;
public class Point
{
public static void main(String[] args)
{
Point p = new Point (1,2);
System.out.println(p);
testMethod(p);
System.out.println(p);
}
public static void testMethod(Point p)
{
System.out.println(p);
p.setLocation(3,4);
System.out.println(p);
}
}
7
9. Write a method printTriangleType that accepts three integer arguments representing the
lengths of the sides of a triangle and prints what type of triangle it is. The three types are
equilateral (three sides the same length), isosceles (two sides the same length), and scalene
(three sides of different lengths). There are certain integer values (or combination of these
values) that would be illegal and could not represent a valid triangle. What are these values?
Include precondition(s) for the printTriangleType method. Write another method called
isValidTriangle that accepts the three sides as arguments. Include the following code in
printTriangleType:
If (!isValidTriangle)
Throw new IllegalArgumentException("invalid side");
8
Objects Exercises
1.
2.
3.
4.
A variable declared using a class is also called an object.
How does a variable of a primitive type differ from a reference variable?
What does the operator new do?
Suppose that str is a string variable. Write a Java statement that uses the new operator to
instantiate an object str and assign the string "Java Programming" to str.
5. What method is used to create an input dialog box?
6. What method is used to create an output dialog box?
7. What is the name of the class that contains the methods to create the input and output dialog
box? What is the name of the package that contains this class?
9
Download