Question: Methods - Java is the code being used. Exercise 1: (2 points) A method to display the Introductory message Run the MinOfThreeInts code and try

Methods - Java is the code being used.

Exercise 1: (2 points) A method to display the Introductory message

Run the MinOfThreeInts code and try different types of input (characters, doubles, etc) when asked for an int. Notice how the program is more robust than our normal programs where it will crash (technically : throw an exception) when non-integer data is typed in at the keyboard when an integer is expected. Notice how the long the main routine is (approximately 100 lines long !). With all this code, it is not clear what is really going on in the program. Your first task is to put the introductory message code (the five println statements following the comment // Let's provide a good Introduction) in a method called intro. Remove the five println statements from the main routine (if you have not done so already) and replace them with a call to intro method. The Intro method is an example of a method with no parameters and no return value. Paste your intro method into your solutions document. No output is required.

Exercise 2: (4 points) A method to read in integers

The main routine contains a large number of lines to read in integers and do basic error handling (users never do what you want them to do !). This clutters up main and makes it hard to understand what is going on. Even worse, much of the code is repetitious as the same thing is done for each of the three integers. Your second task is to create a method called readInt that asks the user for an integer and continually prompts them until a proper integer is entered. When a good integer is obtained from the user, the method should return that value. The readInt should have a single parameter for the Scanner. Once the method is written, eliminate the code to read in integers from the main routine. That code will be replaced with the following lines in main :

value1 = readInt(console); value2 = readInt(console); value3 = readInt(console); Copy your readInt method into your solutions document.

Exercise 3: (4 points) A method to calculate the minimum

The main routine is much less cluttered and easier to understand with the changes from Exercise 2. We finish the job in this last exercise. Your third task will be to create a method called calcMin that takes three integer parameters and returns the minimum value of those parameters. Once the method is written, eliminate the code to determine the minimum from main. That code will be replaced with the following line : min = calcMin(value1,value2,value3); Copy your calcMin and main method into your solutions document.

Here is the code needed.

import java.util.Scanner;

public class MinOfThreeInts { public static void main(String[] args) { Boolean goodInt; int value1, value2, value3; int parsedValue = 0; Scanner console = new Scanner(System.in); // Let's provide a good Introduction System.out.println("This program determines the minimum of three ints"); System.out.println("It gracefully reports errors when erroneous data is entered "); System.out.println("For example, if you type in 'abc' when this program asked for an int"); System.out.println("the program will report the error & ask for another int"); System.out.println("Try giving it bad input ! ");

// Get the first integer System.out.print(" Please enter an integer value "); String input = console.nextLine(); goodInt = false; while (!goodInt) { try { parsedValue = Integer.parseInt(input); goodInt = true; } catch(NumberFormatException ex) { System.out.print(" Invalid input, please enter Int "); input = console.nextLine(); } } value1 = parsedValue;

// Get the second integer System.out.print(" Please enter an integer value "); input = console.nextLine(); goodInt = false; while (!goodInt) { try { parsedValue = Integer.parseInt(input); goodInt = true; } catch(NumberFormatException ex) { System.out.print(" Invalid input, please enter Int "); input = console.nextLine(); } } value2 = parsedValue;

// Get the third integer System.out.print(" Please enter an integer value "); input = console.nextLine(); goodInt = false; while (!goodInt) { try { parsedValue = Integer.parseInt(input); goodInt = true; } catch(NumberFormatException ex) { System.out.print(" Invalid input, please enter Int "); input = console.nextLine(); } } value3 = parsedValue;

// Now calculate the minimum int min = value1; if (value2 < min) { min = value2; } if (value3 < min) { min = value3; } // Now report the results System.out.println(" The minimum value of the three ints is " + min); }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!