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
-
Mouse over highlighted words if you're not sure what they mean!
-
Talk to your classmates for
help.
-
You may want to bring your textbook to future labs
to look up syntax and examples.
-
Stuck? Confused? Have a question? Ask a TA for
help, or look at the book or past .
-
Complete as much of the lab as you can within the allotted time. You don't need to keep working on these exercises after you leave.
-
Feel free to complete problems in any order.
-
Make sure you've signed in on the sign-in sheet before you leave!
Today's lab
Goals for today:
- use
boolean
expressions and variables to represent logical
true/false expressions
- examine logical assertions that can be made about a running
program
- practice problems similar to what will be on the midterm exam
- Where you see this icon, you can click it to check the problem in
Practice-It!
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;
boolean test2 = (1 == 2);
if ((test1 || test2) && 2 + 2 != 5) {
System.out.print("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!