In this lab you will be using methods to provide better
organization and clarity to your code. Methods can improve
program readability, reduce code redundancy, and modularize
program development.
- Set up a new eclipse project for each lab (detailed instructions)
- Copy MinOfThreeInts.java into your project.
In this lab, each exercise
will be making modifications to the starting program. You
may want to keep separate versions of the program for each
completed exercise. After you complete each exercise,
close the editor tab and then in the package explorer
select and then copy and paste the program (Windows:
Ctrl-C Ctrl-V Macs Cmd-C Cmd-V). Eclipse will let
you choose a new name for the copy.
Run the MinOfThreeInts code and try different types of
input (characters, doubles, etc) when asked for an integer.
Notice how the program is more robust than our normal
programs that will crash when non-integer data is typed in
at the keyboard when an integer is expected. Also notice how
long the main routine is. Almost 100 lines! With so much
code, it is hard to understand what is going on in the
program.
Your first task is to put the introductory message code
(the five println statements and the preceding comment into
a method called intro(). Remove the five println
statements from the main routine and replace them with a
call to your intro() method.
Your intro() method is an example of a method with
no parameters and no return values.
Paste your intro() method
into your solutions
document. No output is required.
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! 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. Your readInt(...) should have a single
parameter for the Scanner to be used.
Once the method is written, eliminate the code to read in integers from the main routine. All that code will be replaced with just the following 3 lines in main :
value2 = readInt(scnr);
value3 = readInt(scnr);
Copy your
readInt() method into
your solutions document.
The program can be confusing because the prompt "Please enter an integer value " is always the same. Revise your readInt(...) method by adding a second String argument that is the String used for the prompt.
Change the calls in main() to:
value2 = readInt(scnr, "Please enter the 2nd value ");
value3 = readInt(scnr, "Please enter the 3rd value ");
Copy your readInt() method into your solutions, document.
The main routine is now much less cluttered and easier to understand. We finish the job in this last exercise.
Create a method called minOfThreeInts() 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 :
Copy your minOfThreeInts(...) and main() methods into your solutions document.
Save your solutions document and submit it.