CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG 1
Notice Assignments No new homework this week. Please make up the homework 1 5 & class exercises this week. 2
Notice: Mid-Term Mid-term Exam Date: the class on October 27 th (11:00 AM 12:15 PM) It covers the knowledge from Class 1 to Class 12 (October 13) Open-book but no electronic devices allowed You will be expected to read and write short segments of code Prepare Mid-term Exam Review class notes & class quizzes: 1 12 Weekly quizzes: 1 5 Homework: 2-5 Mid-term Review is posted here Mid-term Practice is posted here TA will go through the Mid-term Practice on October 20 th 3
New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 4
Object Equality You can't use the equality relational operator, ==, with objects Primitive types have only one value... but objects can have many values inside them... so deciding when one object equals another... is not an easy thing to do If you try to use the equality operator, ==... on objects... you will get a logic error 5
Object Equality 6
Object Equality We need a different way of testing for equality when dealing with objects Every Java object must have an equals method It is this method we should use... when testing for equality between objects The String method provides an equals method We have to use this method when comparing strings 7
Object Equality 8
New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 9
Multiple Conditions Sometimes when writing an if statement... you need to test for more than one condition Suppose you wanted to check that a number input by a user... is between 1 and 5 You could so something like this if (number >= 1){ if (number <= 5) { processnumber(number); } } 10
Multiple Conditions But Java gives you an alternative You can combine the two tests by using the logical AND operator The logical AND operator evaluates to true... if both of the expressions it operates upon are true Otherwise, it evaluates to false In Java, the symbol for the logical AND operator is two ampersands, && Using the logical AND operator we can rewrite this code as follows if (number >= 1 && number <= 5){ processnumber(number); } 11
Multiple Conditions There is also a logical OR operator The logical OR operator evaluates to true... if either of of its two operands is true In Java, the symbol for the logical OR operator... is two vertical bars, Again, there can't be any spaces between them So if we wanted to test whether a number is 1 or 2, we could write if (number == 1 number == 2){ processnumber(number); } 12
New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 13
Cumulative Sum We often need to compute the sum of a list of numbers The best way to do this is to keep a running total You declare variable to hold the sum and initialize it to 0 Then work your way through the list of numbers... adding each one, in turn to this running total Here is the pseudocode for this approach sum = 0 for (all numbers) { obtain next number sum += next } 14
Cumulative Sum To turn this algorithm into code... you need to know how many numbers to expect Let's look at a program which prompts the user... for the number of values to be entered... and then asks for the the values themselves 15
Cumulative Sum Notice that we declared the variable sum outside the loop We need to initialize sum to 0.0 With each pass through the loop... we add the next number to sum 16
New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 17
Min/Max Loops Another common programming problem is to find the maximum or minimum of a list of numbers Here's what we need to do to find a maximum Define a variable to hold the current maximum value Set this variable to an initial value For each value in the list, compare the new value to the current maximum... if the current value is larger then the current maximum... make this new value the current maximum 18
Min/Max Loops Here's what it looks like in pseudocode initialize max for (all numbers) { obtain next if (next > max) { max = next } } But what should we use for the initial value... of the variable that will hold the current maximum value? 19
Min/Max Loops We can't use zero If we did, we would get the wrong result for the following series of numbers: -87, -101, -35, -42, -200, -50 All of these numbers are less than 0... so the result would be 0... which is the wrong answer 20
Min/Max Loops There are two ways to deal with this problem If we knew the range of values to expect... We could set the initial value... to the lowest possible number in this range We need to use the lowest possible number... so that this initial value will be replaced... by one of the numbers in the series The second approach is to use the first values entered... to set the initial value of the maximum This is a little inconvenient... because it means reading the first value outside the loop 21
Min/Max Loops The pseudocode that covers these two possibilities is initialize max either to lowest possible value or to first value for (all numbers) { obtain next if (next > max) { max = next; } } 22
New Materials Outline Object Equality Multiple Conditions Cumulative Sum Min/Max Loops Cumulative Sum with if 23
Cumulative Sum with if Whenever we take the average of a series of numbers, we Add all the number together Divide the total by the number of values in the series So the code would look something like this The variable numberofnumbers... contains the length of the list of numbers we are averaging There is a problem with this code What happens if numberofnumbers is 0? 24
Cumulative Sum with if 25
Cumulative Sum with if NaN stands for Not A Number... because division by zero is not defined So we need to test for zero... before trying to determine the average 26
Cumulative Sum with if You can also use an if statement... to count how many negative numbers are entered We can do this by using a counter A counter is a variable that keeps track... of the number of times something has happened We need to initialize the counter to 0... and use an if statement to increment the counter... each time we encounter a negative number 27
Cumulative Sum with if 28