Starting from:

$15

# Lab-5-Methods solution



Lab 5 – Introduction to Methods

We want to find the average of three numbers (doubles). Consider the following class (which is not complete):

```java
// Program finds the minimum of 3 numbers
import java.util.Scanner;

public class Lab5
{
// find the minimum of three numbers
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double one; // first number
double two; // second number
double three; // third number
System.out.printf("%s\n %s\n %s\n", "Type the end-of-file indicator to terminate", "On UNIX/Linux/Mac OS X type <ctrl d then press Enter", "On Windows type <ctrl z then press Enter");
System.out.print("Or enter first number: ");

while (input.hasNext())
{
one = input.nextDouble();
// Write code to get the remainder of the inputs and
// convert them to double values */
// Write code to display the minimum of the three
// floating-point numbers */
System.out.printf("\n%s\n %s\n %s\n", "Type the end-of-file indicator to terminate", "On UNIX/Linux/Mac OS X type <ctrl d then press Enter", "On Windows type <ctrl z then press Enter");
System.out.print("Or enter first number: ");
} // end while

} // end main
// determine the smallest of three numbers
// write the header for the minimum3 method

public static
{
// determine the minimum value
return // Write code to compute the minimum of three numbers
} // end method minimum3
}
```

1. Add the appropriate code in the while loop to input the remainder of the input data.
2. Complete the header of the minimum3 method.
3. Write the code that returns the smallest value.
4. The code shown has two printf statements that are large and repetitive. Create a new method, promptUser, that will accomplish the same task.
5. Create a new method, findMinimum, that returns the smallest of two doubles.
6. Rewrite the minimum3 method use the findMinimum.

More products