University of Washington, CSE 142
Lab 8: Classes and Objects
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:
- write our own new classes of objects
- declare fields, methods, and constructors
- understand the difference between class code and client code
-
Practice working with the critter classes and defining your own critter
class.
-
Through a series of exercises, you will make a LabTA
critter more and more complex in terms of its behaviors. The LabTA class
will serve as a model for how to solve the homework.
- Where you see this icon, you can click it to check the problem in Practice-It!
Declaring a class (syntax)
public class ClassName {
// fields
fieldType fieldName;
// methods
public returnType methodName() {
statements;
}
}
A couple things look different than programs for past homeworks:
- no
main
method. It won't be run like a client program.
- methods don't have the
static
keyword in the header.
- variables declared outside of any method (fields) are visible in every method.
[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]
[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!