also a good way to be a lazy programmer Remember: we want the computer (not us!) to do the work • Main advantage of methods Instead of rewriting the same code over and over, we can use just one method over and over
method starts the program Also calls the next two methods 2. public static String [] createStooges() Creates and returns an array of Strings 3. public static void displayArray (String [] anArray) Displays an array of Strings
returns an array public static ReturnType [] method(){ //method's code return array; } • Be sure to add square brackets after the class name of the return type [ ]
array stooges to the array stoogesNames String [] stoogesNames = ArraysAndMethods.createStooges(); Method call is in the main() method • The method createStooges() is a static method, so method call uses syntax: ClassName.methodName()
and returns an array of Strings public static String [] createStooges(){ String [] stooges = new String[3]; stooges[0] = new String("Larry"); stooges[1] = new String("Curley"); stooges[2] = new String("Moe"); return stooges; }
stooges = new String[3]; stooges[0] = new String("Larry"); stooges[1] = new String("Curley"); stooges[2] = new String("Moe"); return stooges; } This method definition is below the main() method
call tells the corresponding method definition to execute the code contained within that method definition A method definition contains a block of code that has a specific task
for a method call is placed inside a method definition (inside the curly brackets) The code for a method definition is placed inside the class definition (inside the curly brackets) {…}
method ArraysAndMethods.displayArray (stoogesNames); • Method declaration is below the main() method public static void displayArray(String [] anArray){ //code to display an array }
to run your program step by step, one line of code at a time Also displays the value for each variable • Useful not only for finding bugs, but also understanding how methods work
plus button 2. Set a breakpoint by moving the mouse to the left side of the screen and clicking the left mouse button A red dot will remain on the screen
plus button 2. Set a breakpoint by moving the mouse to the left side of the screen and clicking the left mouse button The breakpoint is where the debugger will start The breakpoint must be on a line that has executable code
using the command line interface The command line is the line at the prompt on the UNIX shell • Commands Text editor: emacs Program.java Compiler: javac Program.java Interpreter: java Program
UNIX shell and Emacs, see the links on the ICS 211 class web site • You are welcome to use any programming environment to edit, compile, and run your programs • For example, you could use Pico, Vi, Eclipse, etc.
type in the main() method? public static void main(String [] args) • The args (arguments) variable is a parameter and an array of Strings • args is used to access the command line arguments
in your program on jGRASP 1.On the top menu, click Build 2.On the drop-down menu, click Run Arguments 3.A Run Arguments text field will appear at the top of the screen 4.Type your command line arguments into this text field
Throws (raises, creates) an exception to indicate that an exception has occurred 2. Catches (lowers, ends) an exception, so that code can be executed that handles (deals with) the error in some way
code containing statements which might throw an exception A block is the code contained between curly brackets try{ statement1; statement2; statementN; }
a try block, program control skips over remaining statements in the try block, & continues in the catch block which corresponds to the exception’s type • If an exception is not thrown, then program control continues after the last catch block
method at the top of each method • This way, you do not have to worry about scope issues Scope is where a variable is visible (where it can be used in a program) • See program TryCatchBlocks.java for an example (at top of main method)
class hierarchy See the link to the Java API on the class webpage and find the Exception class • Exceptions can be broken down into two main types 1. Runtime (unchecked) exceptions 2. Checked exceptions
constructor File file = new File("fileName"); Where "fileName" is a String object with the name of the file • The File constructor does not create a new file The File constructor only connects our program to an existing file
string that is either: Simply the name of the file if the file is in the current directory "file2.txt" Or full name of path to the file "C:\ics211\array\file2.txt"
which has the same name as the 1st command-line argument (commandlineArguments[0]) If file does not exists, with throw FileNotFoundException and end Scanner method hasNextLine() will return “true” if more lines are in the file, otherwise “false”
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Get out and enjoy a nearby beach!