Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging and Random Numbers

Similar presentations


Presentation on theme: "Debugging and Random Numbers"— Presentation transcript:

1 Debugging and Random Numbers
Beginning Java Lesson 5 Debugging and Random Numbers

2 Debugging Bugs When errors occur in a program they are called bugs.
The process of removing the errors is know as debugging.

3 Types of Errors Syntax – These errors occur when typing the code. They occur because of spelling, punctuation, and capitalization errors as you type. Run-time – These errors occur when trying to run your program. When you run it you get a Java exception. Exceptions occur when you make a processing error like trying to divide by zero or sometimes you will see the exception NaN. Not a Number. This means that you tried to assign a value to a numeric variable, but the resulting value is not a number.

4 Types of Errors Logic - Logic errors are the most difficult to find and eliminate. These are errors that don’t keep your project from running, but cause incorrect or unexpected results. Unfortunately, there are no general rules for finding logic errors. Each programmer has his or her own particular way of searching for logic errors.

5 Logical Expressions To use Java for decision making, we write all possible decisions in the form of true or false? statements, called logical expressions. Note the result of a logical expression is a boolean type value. To make decisions, we need to know how to build and use logical expressions. The first step in building such expressions is to learn about comparison operators.

6 Comparison Operators Comparison operators are sometimes called a relational operators. Comparison operators do exactly what they say - they compare two values, with the output of the comparison being a boolean value. There are six comparison operators.

7 The “Equal to” Operator
The “equal to” operator represented by two equal (==) signs. Examples: Comparison Result 6 == 7 false 4 == 4 true Note: you cannot use this operator to compare two String variables – we’ll see why after looking at all the comparison operators.

8 The “Not Equal to” Operator
There is also a “not equal to“ operator represented by a symbol consisting of an exclamation point followed by the equal sign (!=). Examples of using this operator: Comparison Result 6 != 7 true 4 != 4 false

9 The “Greater Than” Operator
The “greater than“ operator (>) tells us if one number (left side of operator) is greater than another (right side of operator). Examples of its usage: Comparison Result 8 > 3 true 6 > 7 false 4 > 4

10 The “Less Than” Operator
The “less than“ operator (<) tells us if one number (left side of operator) is less than another (right side of operator). Some examples are: Comparison Result 8 < 3 false 6 < 7 true 4 < 4

11 The “Greater Than or Equal to” Operator
The “greater than or equal to” operator (>=) compares two numbers. The result is true if the number on the left of the operator is greater than or equal to the number on the right. Otherwise, the result is false. Examples: Comparison Result 8 >= 3 true 6 >= 7 false 4 >= 4

12 Precedence Comparison operators have equal precedence among themselves, but are lower than the precedence of arithmetic operators. This means comparisons are done after any arithmetic.

13 aString.equals(bString)
Comparing Strings Remember we said you can’t use the “equals to” operator (==) to compare String type variables. To properly compare two strings for equality, we use the String class equals method. aString.equals(bString) This method returns the boolean result of true if the aString and bString are the same length, that is have the same number of characters, and each character in one string is identical to the corresponding character in the other.

14 aString.equalsIgnoreCase(bString)
Comparing Strings String comparisons done in this way are case-sensitive. To ignore case in the comparison, use: aString.equalsIgnoreCase(bString)

15 Logical Operators Logical operators are used to combine logical expressions built using comparison operators. The first logical operator is the and operator represented by two ampersands (&&). An example of this is: x&& y This expression is asking the question “are x and y both true?”

16 Logical Operators x y x&& y true false In the example
X&&Y, here are the return values:

17 Logical Operators The other logical operator we will use is the or operator represented by two pipes (||). The pipe symbol is the shift of the backslash key (\) on a standard keyboard. The format for using this operator is:  x || y This expression is asking the question “is x or y true?” The or (||) operator will return a true value if either x or y is true. If both expressions are false, the or operator will return a false.

18 The “Or” Operator x y x || y true false
The x||y operator can return 4 possible combinations

19 Decisions - The if Statement
The if statement is not a single statement, but rather a group of statements that implements some decision logic. This is an example of a control structure. It is conceptually simple. Its format looks like this: if (expression) { [Java code block to be executed if expression is true] } else [Java code block to be executed if expression is false]

20 Decisions - The if Statement
So we have some logical expression which is formed from comparison operators and logical operators. if expression is true, then the first block of Java statements (marked by a pair of left and right curly braces) is executed. else (meaning expression is not true, or it is false), the second block of Java statements is executed. Whether expression is true or false, program execution continues with the first line of Java code after the last right curly brace (}).

21 The “If” Statement – An Example
if (temperature > 90) { cost = 50; } else cost = 25; If temperature > 90, a logical expression, is true, the cost will be 50, else (meaning temperature is not greater than 90) the cost will be 25. Notice that we have indented the lines of Java code in the two blocks, which is standard JAVA practice. The neighborhood kids opened a lemonade stand and want you to have your computer decide how much to charge for each cup they sell. Two int variables, cost and temperature have been defined and cost is set based on the temperature. The code is shown at right

22 The “If” Statement You can have as many else if statements as you want. However, that only one block of Java code in an if structure will be executed. This means that once Java has found a logical expression that is true, it will execute that block of code then leave the structure and execute the first line of code following the last right curly brace (}). Lets look at our Lemonade example again.

23 The “If” Statement Expanded
if (temperature > 90) { cost = 50; } else if (temperature > 80) cost = 40; else if (temperature > 70) cost = 30; Else cost = 25; The “If” Statement Expanded What would the cost be if temperature is 85? Temperature is not greater than 90, but is greater than 80, so cost is 40. What about a temperature of 75? Temperature is not greater than 80 but is greater than 70 so the cost is 30. Otherwise the cost is 25 for any temperature less than 70. Here we have taken our previous example and expanded it to include multiple “else” Statements.

24 Random Number Generator
How can you make a computer program unpredictable or introduce the idea of “randomness?” The key is the random number generator. This generator simply produces a different number every time it is referenced. The generator uses the Java Random object. This object is not built into the basic Java language. It is stored in a library called a Java API Package.

25 Using A Function from a Library The Random Number Generator
To tell the computer you are using something from the Library, you must use an import statement just like we did to use the Scanner. Import java.util.Random;  This statement goes after our package statement but before our program’s class definition header. And just like the Scanner, to use the Random object, it is first created using the object constructor:  Random myRandom = new Random(); This statement is placed with the variable declaration statements.

26 Creating a Random Number Continued
Now, whenever you need a random integer value, use the nextInt method of this Random object we created:  myRandom.nextInt(Limit) This statement generates a random integer value that is greater than or equal to 0 and less than Limit. Note it is less than Limit, not equal to. So the statement: myRandom.nextInt(5)  will generate random numbers from 0 to 4. The possible values will be 0, 1, 2, 3 and 4.

27 Assignments Associated with Lesson 5
The rest of Lesson 5 is dedicated to practicing the skills you have seen here in the powerpoint. There are 2 Programs for you to write and submit: RandomTest.java & Number.java As always, when you have completed these programs you are export them to .zip format and submit them in Google Classroom.


Download ppt "Debugging and Random Numbers"

Similar presentations


Ads by Google