44-141 Exercise 6

advertisement
44-141 Exercise 6
1. Examine the Driver class listed below, the main method and the methods ( method1(), method2(),
method3(), method4(), method5() ) listed inside the Driver class. Using the variables declared in
the main method, write in the space provided how one should call each of the static methods from
inside the main method. Pay close attention to “arguments” and “return values”.
public class Driver
{
public static void main()
{
int intNumber;
String message;
double doubleValue = 11.5;
//Call method1
method1();
//Call method2
intNumber=method2();
//Call method3
method3(intNumber);
//Call method4
message=method4(doubleValue);
//Call method5
doubleValue=method5(intNumber);
}
public static void method1()
{
System.out.println( "Hello from method 1" );
}
public static int method2()
{
return 10;
}
public static void method3(int number)
{
System.out.println( "This is my number " + number );
}
public static String method4(double number)
{
String message = "This is my number returned as part of a string "
+ number;
return message;
}
public static double method5(int number)
{
if ( number == 10 )
return (double) 10 * 2;
else
return (double)10;
}
}
15 points- 4 points each (2 points for arguments, 1 point for return value).
2. Examine the Driver class listed below. Note that a portion of the main method has been written for
you. Your task to do the following:
 You should write a static method named findAverage that contains two integer parameters.
The method should calculate the average of the two integers and returns a double value. For
example, if 10 and 11 are passed to the findAverage() method, this method will return
10.5
 Note the main program contains two int variables, intValue1 and intValue2, which
will contain integer values. Complete the main method by showing how you would call the
findAverage() method.
import java.util.Scanner;
public class Driver
{
public static void main()
{
int intValue1;
int intValue2;
double average;
Scanner keyboardInput = new Scanner( System.in );
//Retrieve input of first integer
System.out.print( "Enter an integer: " );
intValue1 = keyboardInput.nextInt();
//Retrieve input of second integer
System.out.print( "Enter an integer: " );
intValue2 = keyboardInput.nextInt();
//Call the average method
average=findAverage(intValue1,intValue2);
System.out.println( "The average is " + average );
}
//===== end method main(String[]) =====
//findAverage method should be defined below
public static double findAverage(int a,int b){
return (a+b)/2.0;
}
}
//======= end class StaticProblem2 =======
12 points. 4 points for calling the method (2 points for arguments, 1 point for return value, 1 point for
general syntax) and 8 points for writing the method (2 points for arguments, 2 points for return value, 2
points for formula, 1 point for a return statement, 1 point for double return value and double formula)
3. Examine the Driver class listed below. Note that a portion of the main method has been written for
you. Your task to do the following:
 You should write a static method named calculateMonthlyInterest that contains two
parameters. The first parameter will be the an int variable which is used to store the principle.
The second parameter will be a double variable which is used to store the interest rate. The
method will calculate the monthly interest for the principle using the interest rate. For
example, if the principle is 1111 and the interest rate is 15%, then the monthly interest would
be 13.8875 (yearly interest is 1111*0.15. Monthly interest is yearly interest/12).
 The main program contains variables that will be used with the
calculateMonthlyInterest method. Complete the main method by showing how you
would call the calculateMonthlyInterst() method.
import java.util.Scanner;
public class StaticProblem3
{
public static void main()
{
int principle;
double interestRate;
double monthlyInterest;
Scanner keyboardInput = new Scanner( System.in );
System.out.print( "Enter the principle: " );
principle = keyboardInput.nextInt();
System.out.print( "Enter the interest rate: " );
interestRate = keyboardInput.nextDouble();
//Call the calculateMonthlyInterset method
monthlyInterest=calculateMonthlyIntreset(principle,intrestRate);
System.out.println( "The monthly interest is " + monthlyInterest );
}
//===== end method main(String[]) =====
public static double calculateInterest(int principle,double interestrate)
{
return(interestrate*principle)/12;
}
}
//end method calculateInterest()
//======= end class StaticProblem2 =======
10 points. 4 points for calling the method (2 points for arguments, 1 point for return value, 1 point for
general syntax) and 6 points for writing the method (1 point for return type, 2 points for arguments, 2
points for formula, 1 point for a return statement).
Download