330 likes | 475 Views
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:
E N D
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
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
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
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
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
Relational Operators • Shown above are the 6 relational operators with their meanings • They are frequently used as boolean expressions in if statements.
*** 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.
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.
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!);
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.
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)
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.
Digression • Discussion of • Sign magnitude representation • Ones complement representation • Twos complement representation
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
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.
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.
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.
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.
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.");
DeMorgan’s Law • The negation of • (A and B) is (not A or not B) • (A or B) is (not A and not B)
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.
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.
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.
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: …
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.
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.
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 }
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.
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.
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.