[an error occurred while processing this directive] CSE 142 Lab 5-5: midterm practice

University of Washington, CSE 142

Lab 5-5: midterm practice

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Basic lab instructions

Today's lab

Goals for today:

The boolean type

The boolean type represents logical values of true or false. Combine boolean expressions with logical operators && (and), || (or), and ! (not).

Example:

boolean test1 = 7 < 10;            // true
boolean test2 = (1 == 2);          // false
if ((test1 || test2) && 2 + 2 != 5) {
    System.out.print("hello");     // output: hello
}

String methods with boolean results

Method name Description
string.equals(string) whether the two strings are identical
string.equalsIgnoreCase(string) whether the two strings are identical, ignoring capitalization
string.startsWith(string) whether this string begins with the characters of the given string
string.endsWith(string) whether this string ends with the characters of the given string
string.contains(string) whether the characters of the given string occur within this string
String name = "Professor Smith";
if (name.startsWith("Prof")) {
    System.out.println("When are your office hours?");
}
[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!