A string is a sequence of characters. Strings are object of the String class.
Given the following string:
String message = "Hello, World!"
int n = message.length(); // n = 13
String name = "Dave"; String message = "Hello, " + name; // message = Hello, Dave
String a = "Agent";
int n = 7;
String bond = a + n; // bond = "Agent7"
double total = 3.70;
System.out.println("The total is " + total); // outputs "The total is 3.70"
String greeting = "Hello, World!"; String sub = greeting.substring(0,5); // sub = "Hello" sub = greeting.substring(7,12); // sub = "World" sub = greeting.substring(7); // sub = "World!"
|
The == operator tests whether
two object references are identical. To compare the contents of objects, you need to use the equals method. |
What does the following code do?
if ( string1.equals(string2) ) ...
if ( string1 == string2 ) ... // not useful
| The compareTo method compares strings in dictionary order. |
If two strings are not identical to each other, you still may want to know the relationship between them.
if string1.compareTo(string2) < 0, then the string string1 comes before the string string2.A && B is true only if both A and B are true.
A || B is true if either A or B are true.
Truth table
| A |
B |
A
&& B |
A
|| B |
!A |
| false |
false |
false |
false |
true |
| false |
true |
false |
true |
true |
| true |
false |
false |
true |
false |
| true |
true |
true |
true |
false |
Example: Print out a message when y >= x >= 0.
Answer:
if ( y >= x && x >= 0 )
System.out.println("y >= x >= 0");
Example:
Rewrite the following using a single if and boolean operators.
if ( single )
if ( gender == 'M' )
if ( age >= 18 && age <= 26 )
System.out.println("This person qualifies for our special offer");
Answer:
if ( single && gender == 'M' && age >= 18 && age <= 26 )
System.out.println("This person qualifies for our special offer");
Example: If grade is outside the interval [0,100], indicate an error. Otherwise output the grade.
Answer:
Version 1: Code using ||:
if ( grade < 0 || grade > 100 )
System.out.println("illegal grade");
else
System.out.println("Grade is " + grade);
Version 2: Code using &&:
if ( grade >= 0 && grade <= 100 )
System.out.println("Grade is " + grade);
else
System.out.println("illegal grade");
A bank charges $10 per month plus the following check fees for a commercial checking account.
$.10 each for less than 20 checks $.08 each for 20-39 checks $.06 each for 40-59 checks $.04 each for 60 or more checks
The bank also charges an extra $15 if the balance of the account falls below $400 (before any check fees are applied). Design a class that stores the beginning balance of an account and the number of checks written. It should also have a method that returns the bank's service fees for the month.
Public Interface for the BankCharges
/* Initializes the instance's fields. */ public BankCharges(double balance, int numChecks) /* Returns the current balance for this account */ public double getBalance() /* Returns the number of checks written on the account */ public int getNumChecks() /* Determines the service fees for this account by calling the private method calculateServiceFees. Subtract these fees from the balance. */ public void updateBalance() /* Returns the service fees and balance as a String with labels */ public String toString() /* This method is a private method to help in determining the new balance after the service fees are deducted. It is not part of the public interface but is useful in other methods. */ /* This method determines the service fees for this account and returns this fee. Be sure to include the $10 monthly charge and the $15 if the balance is below $400. */ private double calculateServiceFees()
Testing your BankCharges class
De Morgan's law shows how to simplify expressions in which a ! operator is applied to a term joined by the && or || operator. !(A && B) is the same as !A || !B |
The following table gives the operator precedence from highest (performed first) to lowest (performed last)
|
|
Descriptions |
|---|---|
|
|
logical not, unary plus, unary minus |
|
|
multiplication, division, and remainder |
|
|
addition and subtraction |
|
|
relational operators |
|
|
logical AND |
|
|
logical OR |
|
|
assignment operators |
Besides comparing numbers, you can compare characters using the relational operators. The order of char comparisons is based on the Unicode position of each character (see Appendix B).
| Expression | Value |
|---|---|
| 'a' < 'b' | true |
| 'X' <= 'A' | false |
| '3' > '4' | false |
| '3' <= '4' | true |
| 'a' > 'A' | true |
| ('A' <= ch) && (ch <= 'Z') | true if ch contains an uppercase letter; otherwise false |
Short-circuit evaluation means that in a multiple clause boolean expression, evaluation stops as soon as the answer is determined.
Examples:
true || X is always true so there is no point finding X.
Similarly false && Y is always false regardless of Y's value.
Example 1: possible division by zero
if ( scoreTotal/numScores > 90.0 ) System.out.println( "Excellent! Very good work ");
Possible solutions:
nested ifs:
if ( numScores > 0 ) {
if ( scoreTotal / numScores > 90.0 ) {
System.out.println("Excellent! Very good work ");
}
}
short-circuit evaluation using &&:
if ( numScores > 0 && scoreTotal / numScores > 90.0 )
System.out.println("Excellent! Very good work ");
In both solutions, the division only occurs if numScores is greater than 0.
A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, 1980). However, since the introduction of the Gregorian calendar, a year is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). Write a program that asks the user for a year and computes whether that year is a leap year. Implement a class LeapYear with a method boolean isLeapYear(). Include a method setYear(int year) that replaces the current year. Test your class with the dates mentioned in the problem.