Lab: Experience with Objects

Instructions:

Answer the following questions one at a time. After answering each question, check your answer (by clicking on the arrow triangle icon if it is available) before proceeding to the next question.

Getting Ready:

Before going any further, you should:

1) Depending on your development environment, create either a directory or a project for this lab. 2) Setup your development environment. 3) Download the following files: TextEditor.class TextWindow.class to an appropriate directory/folder (e.g., the course downloads directory/folder). In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as… or Save link as....

4) Briefly review the following documents: TextEditor TextWindow

Note that a class may be referred to using its fully qualified name. For example, the String class may be referred to as the java.lang.String class and the Scanner class may be referred to as the java.util.Scanner class. 1. The Basics: This part of the lab will help refresh your memory about the declaration, construction, and use of objects.

1) What does it mean when we say that an object is immutable? (Note: Be precise and use appropriate terminology.) It means that the attributes of the object can't be changed.

2) Read the documentation for the String class

3)How would you declare a variable of class String named course? String course;
4)How would you instantiate the object named course above (so that it contains the letters "CS100")? course = new String("CS100");
5)Does the String class contain any methods that begin with set? No
6)The String class contains some methods that make it look like String objects are mutable. Name some of them. It contains several methods that start with replace, several methods that start with to and a trim() method, that, from their names, seem like they would change the owning object.
7)Are these methods void? If not, what do they return? They aren't void, they return String objects.
8)Are String objects mutable? No. The methods that make it look like they might be don't change the owning object, they return different objects.

9) Read the documentation for the TextEditor class.

10)How would you declare a variable of class TextEditor named editor? TextEditor editor;
11)How would you instantiate the object named editor above (using the default constructor)? editor = new TextEditor();
12)How would you call the method with the signature: public void selectAll() that belongs to the object named editor? editor.selectAll();
13)Are TextEditor objects mutable or immutable? They are mutable. Most obviously, the setText() method changes the text in the owning object.