



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Quiz next Wednesday. Reading Assignment Chapter 5, sections 5.5 through 5.8. Programming Assignment The next programming assignment is on-line. Although it is not due until next Monday the 14th, I strongly recommend working on it before the quiz, as it will increase your understanding of conditional logic. Chapter 5: Decisions Review boolean variables relational operators boolean operaters conditional statements
System.out.println("second is smaller"); What does the above print when the user enters 7 and 3? How about when they enter 4 and 6? Make sure you include the {}! Remember, the compiler doesn't care about indentation. Don't let the indentation fool you. And another example (Example3.java): if ( x > 1 ) if ( y < 2 ) S.O.PL("Here"); else S.O.PL("There"); What does the above print when x=2 and y = 1? What does it print when x = 0 and y = 1? What does it print when x = 2 and y = 3? This is called a 'dangling-else'. It's not clear which 'if' the 'else' goes with. In Java, the else matches the previous unmatched 'if' in the same block. Once again, note that the indentation has nothing to do with the actual program logic - indent carefully! Use {} to make the matching explicit. Switch statement Let's say we wanted to write a little calculator program, and we use different keystrokes to represent different operations: '', '/', '+', '-', '^'. We could write the code to handle these commands using the if else-if construct: int firstValue = stdin.nextInt(); int secondValue = stdin.nextInt(); int result; boolean error = false; char command = stdin.nextChar(); if (command == '+' ) { result = firstValue + secondValue; else if (command == '') result = firstValue * secondValue; else if (command == '-') result = firstValue - secondValue; else if ( command == '/' ) result = firstValue / secondValue;
Let's look at another example: Assume dayOfWeek is a variable where 1 = Sunday, 7 = Saturday. Then we want to go to CSIS110 on MWF, study on Tuesday and Thursday, and enjoy the weekend on Friday and Saturday. How do we do this with a switch statement? Let's look at another example of if else-if (Month1.java): if ( month==4 || month==6 || month==9 || month==11 ) { daysInMonth = 30; else if ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) daysInMonth = 31; else if ( month == 2 ) daysInMonth = 28; // Assume not a leap year else daysInMonth = 0; How could we write this using switch (Month2.java)? switch ( month ) { case 4: case 6: case 9: case 11: daysInMonth = 30; break; case 1: case 3: case 5: case 7: case 8: case 10 case 12: daysInMonth = 31; break; case 2: daysInMonth = 2; break; default: daysInMonth = 0; } Note that I have to specify each case separately. I can't say something like 'case 4,6,9,11:'. Note that the keyword for the situation where the value does not match is 'default', not 'else'
break statement is important - if you leave it out, your program won't work! Case choices must be integral values - you can't put Strings, or floats, or boolean expressions here. Equality Testing of Objects If we want to see if two ints have the same value, we can use the == operator: int value1 = stdin.nextInt(); int value2 = stdin.nextInt(); if ( value1 == value2 ) S.O.PL( "value1 is equal to value2"); else S.O.PL( "value1 is not equal to value2" ); Tempting to do the same thing with Strings - Let's look at StringCompare.java String firstString = stdin.nextLine(); String secondString = stdin.nextLine(); if ( firstString == secondString ) S.O.PL( "The Strings are the same"); else S.O.PL( "The Strings are different" ); Let's look at another example (modify StringCompare to add thirdString): String firstString = stdin.nextLine(); String secondString = stdin.nextLine(); String thirdString = firstString; if ( firstString == secondString ) S.O.PL( "The Strings are the same"); else S.O.PL( "The Strings are different" ); if ( firstString == thirdString ) S.O.PL( "The Strings are the same"); else S.O.PL( "The Strings are different" ); So, what is == testing? It is checking to see if the references are the same? Do the Strings refer to the same memory location? How do we check to see if the String contents are the same?