150 likes | 306 Views
The break And continue Statements. a break statement can be used to abnormally terminate a loop. use of the break statement in loops bypasses the normal mechanisms and makes the code hard to read and maintain. considered bad form to use the break statement in this manner.
E N D
The break And continue Statements • a break statement can be used to abnormally terminate a loop. • use of the break statement in loops bypasses the normal mechanisms and makes the code hard to read and maintain. • considered bad form to use the break statement in this manner. ACS-1903
The continue Statement • a continue statement will cause the currently executing iteration of a loop to terminate and the next iteration will begin. • a continue statement will cause the evaluation of the condition in while and for loops. • as with the break statement, the continue statement should be avoided because it makes the code hard to read and debug. ACS-1903
Deciding Which Loops to Use • The while loop: • Pretest loop • Use it where you do not want the statements to execute if the condition is false in the beginning. • The do-while loop: • Post-test loop • Use it where you want the statements to execute at least one time. • The for loop: • Pretest loop • Use it where there is some type of counting variable that can be evaluated. ACS-1903
Files • Input … reading (we’ll focus on reading files first) • Output … writing • Errors can occur • Attempting to read a file that does not exist • Writing to a file that is protected • These types of errors need to be provided for ACS-1903
Exceptions • When something unexpected happens in a Java program, an exception is thrown. • The method currently executing when the exception is thrown must either handle the exception, or, pass it up the line. • Handling the exception is discussed later. • To pass it up the line, the method needs a throws clause in the method header. • For now just accept that this must be done ACS-1903
Exceptions • To insert a throws clause in a method header, simply add the word throws and the name of the expected exception. • The class Exception can be used to catch all exceptions. public static void main(String[] args) throws IOException{…} • File I/O is a checked exception (meaning the exception must be handled or passed up). • A program with file I/O will generate a compile-time error if the exception is not handled or passed up. • Example: FileWriteDemo.java ACS-1903
Reading Data From a File • Java provides several classes to read data from a file. • FileReader • Open an existing file for reading and establish a connection with it. • BufferedReader • Uses a buffer to allow the reading of full lines of text at a time rather than one byte at a time. ACS-1903
Detecting The End of a File • The readLine() method of the BufferedReader class will return null if the end of the file has been reached. FileReader freader = new FileReader(filename); BufferedReader inputFile = new BufferedReader(freader); // Read the first item. String str = inputFile.readLine(); // If an item was read, display it // and read the remaining items. while (str != null) { System.out.println(str); str = inputFile.readLine(); } inputFile.close();// close the file when done. We say end-of-file occurs when we read a line, but there are no more lines left to read. End-of-file is detected by checking for a null line. At this time you need to know that we can read files one line at a time, until end-of-file, and each line is available as a string ACS-1903
String Value Conversion • The readLine() method of the BufferedReader class only reads in text as a String object. • Strings that represent numbers can be converted and stored into primitive variables. • Java provides wrapper classes that make conversion easy. ACS-1903
String Value Conversion Example: FileSum.java ACS-1903
The Random Class • Some applications, such as games and simulations, require the use of randomly generated numbers. • The Java API has a class, Random, for this purpose. To use the Random class, use the import statement and create an instance of the class. import java.util.Random; Random randomNumbers = new Random(); ACS-1903
Some Methods of the Random Class Example: MathTutor.java ACS-1903
Example Suppose we want to program a game that involves two dice. Die will be a class from which we can instantiate two objects (one for each die). We can toss the pair of dice 1,000,000 times and see what the average throw is. A UML class diagram showing the classes the program has, each of their attributes and methods, and the relationships between classes: 2 1 Die Random DiceGame die1 die2 numberSides generator nextInt() main() toss() A dicegame is played with two dice A die uses a random number generator Java code: DiceGame.java Die.java ACS-1903
A UML sequence diagram showing how the objects interact Die is a class from which we instantiate two objects. We toss the pair of dice 1,000,000 times. System.out DiceGame main() printLn() new() die1:Die new() : Random new() die2:Die new() :Random Loop [1,000,000 times] toss() nextInt() toss() nextInt() printLn() ACS-1903