1 / 33

The System.exit() Method

The System.exit() Method. The System.exit method requires an integer argument. System.exit( 0 ); This argument is an exit code that is passed back to the operating system. This code is usually ignored, however, it can be used outside the program:

cruz
Télécharger la présentation

The System.exit() Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The System.exit() Method • The System.exit method requires an integer argument. System.exit(0); • This argument is an exit code that is passed back to the operating system. • This code is usually ignored, however, it can be used outside the program: • to indicate whether the program ended successfully or as the result of a failure. • The value 0 traditionally indicates that the program ended successfully. • We will learn about handling Exceptions so this will become less important except perhaps in debugging or when you want your program to end as part of the exception handling. • From Gaddis – Chapter 2 – slide 9

  2. The Parse Methods • Each of the numeric wrapper classes, Chapter 8, has a static method that converts a string to a number. • The Integer class has a method that converts a string to an int, • The Double class has a method that converts a string to a double, and • etc. • These methods are known as parse methods because their names begin with the word “parse.” • From Gaddis – Chapter 2 – slide 11

  3. The Parse Methods byte bVar = Byte.parseByte("1"); // Store 1 in bVar. int iVar = Integer.parseInt("2599"); // Store 2599 in iVar. short sVar = Short.parseShort("10"); // Store 10 in sVar. long lVar = Long.parseLong("15908"); // Store 15908 in lVar. float fVar = Float.parseFloat("12.3"); // Store 12.3 in fVar. double dVar = Double.parseDouble("7945.6"); // Store 7945.6 in dVar. From Gaddis – Chapter 2 – slide 12

  4. Review of the following Topics • The if Statement • The if-else Statement • The if-else-if Statement • Nested if Statements • Logical Operators • Comparing String Objects • More about Variable Declaration and Scope • The switch Statement • From Gaddis – Chapter 3 – slides 4 and 5

  5. The if Statement • The if statement is one of a group of statements known as selection statements. • The if statement allows the programmer to make decisions whether a section of code executes or not. (i.e. does the program select these statements or not?) • The if statement uses a boolean expression OR a boolean variable as an argument to decide if the next statement or block of statements executes. if (boolean expression is true) { execute what’s in this block } if (boolean_variable) { execute what’s in this block } From Gaddis – Chapter 3 – slides 6 modified

  6. Relational Operators • Shown above are the 6 relational operators with their meanings • They are frequently used as boolean expressions in if statements.

  7. *** Important *** • The result of the evaluation of a boolean expression is a boolean value. • if (7 > 5) { System.out.println ( “7 is bigger than 5);} • A boolean variable also has a boolean value. • boolean done; • done = true; • if (done) { System.out.println (“ done”); } • In an if statement a boolean variable is either true or false so we never compare it to true or false.

  8. If Statement Programming Style • What is to be done when an if statement is true or false follows the condition. Although it is legal, we will NEVER put it on the same line. if(average > 95) System.out.println(“That’s a great score!”); • Multiple statements are grouped into a block by using curly braces {} to enclose them. if(expression) { statement1; statement2; statement3; } • Remember that if the curly braces are not used, then only the next statement after the if condition will be executed conditionally.

  9. Use of Flags • A flag is a boolean variable that monitors some condition in a program. • When a condition is true, the flag is set to a true value. • The flag can be set to indicate a situation if(average > 95) highScore = true; • The state of the flag can be tested: if(highScore) System.out.println(“That’s a high score!);

  10. Comparing Characters • Characters can be tested using the relational operators. • Characters are stored in the computer using the unicode character format. • Each unicode character is stored using sixteen (16) bits. • Characters are ordinal, meaning they have an order in the unicode character set. • Since characters are ordinal, they can be compared to each other.

  11. Digression • Discussion of meaning of ordinal • Not only ordered but we know what the next element is. • for integers, given x, the next value is x+1 (integers are ordinal numbers) • for floating point numbers, given x.y, the next value is not known, could be x.y1 or x.y01, or x.y001 or … (floating point numbers are not ordinals)

  12. if-else Statements • The if-else statement adds the ability to conditionally execute code based if the expression of the if statement is false. • if-else-if statements can become very complex. if it is very cold, wear a heavy coat, else, if it is chilly, wear a light jacket, else, if it is windy wear a windbreaker, else, if it is hot, wear no jacket. • Care must be used since else statements match up with the immediately preceding unmatched if statement.

  13. Digression • Discussion of • Sign magnitude representation • Ones complement representation • Twos complement representation

  14. Nassi-Shneiderman Diagrams • http://en.wikipedia.org/wiki/Nassi-Shneiderman_diagram • http://www.smartdraw.com/tutorials/software/nassi/tutorial_01.htm • http://www.smartdraw.com/examples/view/index.aspx?catID=.Examples.SmartDraw.Software_Design.Nassi-Shneiderman_Diagrams

  15. if-else-if Flowchart

  16. Nested if Statements • If an if statement appears inside of another if statement (single or block) it is called a nested if statement. • The nested if is only executed if the if statement it is in results in a true condition. • Nested if statements can get very complex, very quickly.

  17. Logical Operators • Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. • Java also provides one unary (!) logical operator to reverse the truth of a boolean expression.

  18. Logical Operators

  19. The && Operator • The logical AND operator (&&) takes two operands that must both be boolean expressions. • The resulting combined expression is true iff (if and only if) both operands are true.

  20. The || Operator • The logical OR operator (||) takes two operands that must both be boolean expressions. • The resulting combined expression is false iff (if and only if) both operands are false.

  21. The ! Operator • The ! operator performs a logical NOT operation. • If an expression is true, !expression will be false. if (!(temperature > 100)) System.out.println(“Below the maximum temperature.");

  22. DeMorgan’s Law • The negation of • (A and B) is (not A or not B) • (A or B) is (not A and not B)

  23. Short Circuiting • Logical AND and logical OR operations perform short-circuit evaluation of expressions. • Logical AND will evaluate to false as soon as it sees that one of its operands is a false expression. • Logical OR will evaluate to true as soon as it sees that one of its operands is a true expression.

  24. Order of Precedence • The ! operator has a higher order of precedence than the && and || operators. • The && and || operators have a lower precedence than relational operators like < and >. • Parenthesis can be used to force the precedence to be changed.

  25. Order of Precedence

  26. Comparing String Objects • In most cases, you cannot use the relational operators to compare two String objects. • Reference variables contain the address of the object they represent. • Unless the references point to the same object, the relational operators will not return true.

  27. Ignoring Case in String Comparisons • In the String class the equals and compareTo methods are case sensitive. • In order to compare two String objects that might have different case, use: …

  28. Variable Scope • In Java, a local variable does not have to be declared at the beginning of the method. • The scope of a local variable begins at the point it is declared and terminates at the end of the method. • When a program enters a section of code where a variable has scope, that variable has come into scope, which means the variable is visible to the program.

  29. The switch Statement • The if-else statements allow the programmer to make true / false branches. • The switch statement allows the programmer to use an ordinal value to determine how a program will branch. • The switch statement can evaluate an integer type or character type variable and make decisions based on the value.

  30. The switch Statement • The switch statement takes the form: switch (SwitchExpression) { case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break; // case statements may be repeated //as many times as necessary default: // place one or more statements here }

  31. The switch Statement • The switch statement takes an ordinal value (byte, short, int, long, char) as the SwitchExpression. switch (SwitchExpression) { … } • The switch statement will evaluate the expression. • If there is an associated case statement that matches that value, program execution will be transferred to that case statement.

  32. The switch Statement • Each case statement will have a corresponding CaseExpression that must be unique. case CaseExpression: // place one or more statements here break; • If the SwitchExpression matches the CaseExpression, the Java statements between the colon and the break statement will be executed.

  33. The switch Case • The break statement ends the case statement. • The break statement is optional. • If a case does not contain a break, then program execution continues into the next case. • The default case is optional and will be executed if no CaseExpression matches the SwitchExpression.

More Related