Download Exercise 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ECCE/Exercises 6
Software Devel opment
Methods
The following two examples use a programmer-declared method called maximum to
determine and return the largest of three double values that are input by the user.
Part A
1. Analyze the structure of the following Java program
“MaximumFinder.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
// MaximumFinder.java
// Programmer-declared method maximum.
import java.util.Scanner;
public class MaximumFinder
{
// obtain three floating-point values and determine maximum
value
public void determineMaximum()
{
// create Scanner for input from command window
Scanner input = new Scanner( System.in );
// prompt for and input three floating-point values
System.out.print(
"Enter three floating-point values separated by
spaces: " );
double number1 = input.nextDouble(); // read first double
double number2 = input.nextDouble(); // read second
double
double number3 = input.nextDouble(); // read third double
// determine the maximum value
double result = maximum( number1, number2, number3 );
// display maximum value
System.out.println( "Maximum is: " + result );
} // end method determineMaximum
// returns the maximum of its three double parameters
public double maximum( double x, double y, double z )
{
double maximumValue = x; // assume x is the largest to
start
// determine whether y is greater than maximumValue
if ( y > maximumValue )
maximumValue = y;
// determine whether z is greater than maximumValue
if ( z > maximumValue )
maximumValue = z;
return maximumValue;
} // end method maximum
} // end class MaximumFinder
__________________________________________________________
Updated 3/11/05
1
ECCE/Exercises 6
Software Devel opment
3. Create and compile the program. Explain and analyse the program above.
Part B
1. Analyze the structure of the following Java program
“MaximumFinderTest.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
// MaximumFinderTest.java
// Application to test class MaximumFinder.
public class MaximumFinderTest
{
// application starting point
public static void main( String args[] )
{
MaximumFinder maximumFinder = new MaximumFinder();
maximumFinder.determineMaximum();
} // end main
} // end class MaximumFinderTest
__________________________________________________________
3. Create and compile the program. Explain and analyse the program above.
The scope of a declaration is the portion of the program that can refer to the declared
entity by its name. Such an entity is said to be “in scope” for the portion of the
program. The following two exercises demonstrate scoping issues with fields and
local variables.
Part C
1. Analyze the structure of following Java program “Scope.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
// Scope.java
// Scope class demonstrates field and local variable scopes.
public class Scope
{
// field that is accessible to all methods of this class
private int x = 1;
// method begin creates and initializes local variable x
// and calls methods useLocalVariable and useField
public void begin()
{
int x = 5; // method's local variable x shadows field x
System.out.printf( "local x in method begin is %d\n", x
);
useLocalVariable(); // useLocalVariable has local x
useField(); // useField uses class Scope's field x
useLocalVariable(); // useLocalVariable reinitializes
local x
useField(); // class Scope's field x retains its value
Updated 3/11/05
2
ECCE/Exercises 6
Software Devel opment
System.out.printf( "\nlocal x in method begin is %d\n", x
);
} // end method begin
// create and initialize local variable x during each call
public void useLocalVariable()
{
int x = 25; // initialized each time useLocalVariable is
called
System.out.printf(
"\nlocal x on entering method useLocalVariable is
%d\n", x );
++x; // modifies this method's local variable x
System.out.printf(
"local x before exiting method useLocalVariable is
%d\n", x );
} // end method useLocalVariable
// modify class Scope's field x during each call
public void useField()
{
System.out.printf(
"\nfield x on entering method useField is %d\n", x );
x *= 10; // modifies class Scope's field x
System.out.printf(
"field x before exiting method useField is %d\n", x );
} // end method useField
} // end class Scope
__________________________________________________________
3. Create and compile the program. Explain and analyse the program above.
The next program uses the continue statement in a for to skip the statement at line
12 when the nested if (line 9) determines that the value o count is 5.
Part D
1. Analyze the structure of following Java program “ScopeTest.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
// ScopeTest.java
// Application to test class Scope.
public class ScopeTest
{
// application starting point
public static void main( String args[] )
{
Scope testScope = new Scope();
testScope.begin();
} // end main
} // end class ScopeTest
__________________________________________________________
3. Create and compile the program. Explain and analyse the program above.
Methods of the same name can be declared in the same class, as long as they have
different sets of parameters (method overloading). When an overloaded method is
called, the Java compiler selects the appropriate method by examining the number,
types and order of the arguments in the call. Method overloading is commonly used to
Updated 3/11/05
3
ECCE/Exercises 6
Software Devel opment
create several methods with the same name that perform the same or similar tasks, but
on different types or different numbers of arguments.
Part E
1. Analyze the structure of following Java program “MethodOverload.java”.
2. Run the program and predict the outcome in your logbook.
__________________________________________________________________
// MethodOverload.java
// Overloaded method declarations.
public class MethodOverload
{
// test overloaded square methods
public void testOverloadedMethods()
{
System.out.printf( "Square of integer 7 is %d\n", square( 7
) );
System.out.printf( "Square of double 7.5 is %f\n", square(
7.5 ) );
} // end method testOverloadedMethods
// square method with int argument
public int square( int intValue )
{
System.out.printf( "\nCalled square
%d\n",
intValue );
return intValue * intValue;
} // end method square with int argument
with
int
argument:
// square method with double argument
public double square( double doubleValue )
{
System.out.printf( "\nCalled square with double argument:
%f\n",
doubleValue );
return doubleValue * doubleValue;
} // end method square with double argument
} // end class MethodOverload
3. Explain and analyse the program above.
Part F
1. Analyze the structure of following Java program
“MethodOverloadTest.java”.
2. Run the program and predict the outcome in your logbook.
___________________________________________________________
// MethodOverloadTest.java
// Application to test class MethodOverload.
public class MethodOverloadTest
{
public static void main( String args[] )
{
MethodOverload methodOverload = new MethodOverload();
Updated 3/11/05
4
ECCE/Exercises 6
Software Devel opment
methodOverload.testOverloadedMethods();
} // end main
} // end class MethodOverloadTest_
_________________________________________________________
3. Create and compile the program. Explain and analyse the program above.
Questions:
1. Why is Method Main Declared static?
2. How can you declare Methods with multiple parameters?
3. Write a program that simulates 20 rolls of a six-sided die and displays the
value of each roll.
4. What is enumeration and what is the benefit of using enumeration constants?
5. Write an application that tests whether the examples of the Math class method
calls shown in Fig. 6.2 (page 209 of the core book) actually produce the
indicated results.
Updated 3/11/05
5
Related documents