Instructions:
Answer the following questions one at a time. After answering each question,
check your answer (by clicking on the check-mark icon if it is available)
before proceeding to the next question.
Getting Ready:
Before going any further, you should:
Depending on your development environment, create either a
directory or a project for this lab.
to an appropriate directory/folder. (In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above.)
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.
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.
Read the documentation for the String class.
How would you declare a variable of class String
named course?
String course;
How would you instantiate the object named course
above (so that it contained the letters "CS100"?
course = new Course("CS100");
Does the String class contain any methods that begin
with set?
No.
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.
Are these methods void? If not, what do they return?
They aren't void, they return String objects.
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.
Read the documentation for the TextEditor class.
How would you declare a variable of class TextEditor
named editor?
TextEditor editor;
How would you instantiate the object named editor
above (using the default constructor)?
editor = new TextEditor();
How would you call the method with the signature:
public void selectAll()
that belongs to the object named editor?
editor.selectAll();
Are TextEditor objects mutable or immutable?
They are mutable. Most obviously, the setText()
method changes the text in the owning object.
2. Some Practice:
This part of the lab will give you some practice declaring, constructing,
and using objects. It will also help you familiarize yourself with
some of the terminology of object-oriented programming.
Create (and start editing) a file named JMUview.java
that contains the following "shell":
/**
* The driver for JMUview -- a simple command-line argument viewer
*
* @author Your Name
* @version 1.0
*/
public class JMUview
{
/**
* The entry point for JMUview
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
}
}
and replace "Your Name" with the appropriate text.
Add code to the main() method that:
Declares a variable of class
TextEditor named editor.
Declares a variable of class
TextWindow named window.
Instantiates editor using
the default constructor.
Calls the
setShouldHaveScrollBar() method of
editor passing the boolean literal
true.
Calls the
setEditable() method of
editor passing the boolean literal
false.
In an appropriate place, instantiates window passing
the String literal "JMUview" to the explicit value
constructor.
In an appropriate place, calls the
setTextEditor() method of
window passing the object
editor.
In an appropriate place, checks to see if there are any command-line
arguments. If not, calls the
setText() method of the
editor object, passing it
"No command-line arguments.". Otherwise, it iteratively
calls the append() method of the editor
object twice (per iteration), first passing the command-line
argument and then passing a newline character.
After all of the method calls, calls the
setVisible() method of
window passing the boolean literal
true. (Note: The call to setVisible()
must be the last method call on the window object.)
JMUview.java
import java.lang.reflect.Array;
/**
* The driver for JMUview -- a simple command-line parameter viewer
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class JMUview
{
/**
* The entry point for JMUview
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
int length;
TextEditor editor;
TextWindow window;
editor = new TextEditor();
editor.setShouldHaveScrollBar(true);
editor.setEditable(false);
window = new TextWindow("JMUview");
window.setTextEditor(editor);
length = Array.getLength(args);
if (length == 0)
{
editor.setText("No command-line arguments.");
}
else
{
for (int i=0; i<length; i++)
{
editor.append(args[i]);
editor.append("\n");
}
}
window.setVisible(true);
}
}
Compile JMUview.java.
Execute JMUview passing it no command-line arguments.
Execute JMUview passing it several command-line arguments.
3. More Practice:
This part of the lab will give you more practice declaring,
constructing, and using objects. It will also help you
familiarize yourself with standard Java documentation.
Create a class named JMUedit (with appropriate
documentation) that has, at a minimum,
a method with the signature:
public static void main(String[] args)
Your implementation must:
Declare and construct a TextEditor object.
Tell the TextEditor object it should be "editable".
Tell the TextEditor object it should have a scrollbar.
Declare and construct a TextWindow object
with the title "JMUedit".
Tell the TextWindow object it should use
the TextEditor object you already constructed.
Tell the TextWindow object that it should
ask the user to confirm exit requests.
Tell the TextWindow object to add a "File" menu.
Tell the TextWindow object it should be visible.
JMUedit.java
/**
* The driver for JMUedit -- a simple text file editor
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class JMUedit
{
/**
* The entry point for JMUedit
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
TextEditor editor;
TextWindow window;
editor = new TextEditor();
editor.setEditable(true);
editor.setShouldHaveScrollBar(true);
window = new TextWindow("JMUedit");
window.setTextEditor(editor);
window.setShouldConfirmExit(true);
window.addFileMenu();
window.setVisible(true);
}
}
Compile JMUedit.java.
Execute JMUedit, navigate to a directory/folder that
contains a text file, and open it.
Exit JMUedit and make sure that it "confirms".
Exexute JMUedit again type some text. Then, make
sure that you can save the text to a file.
4. Using Many Objects at Once:
This part of the lab will give you some practice using many objects
at once. It will also help you familiarize yourself with writing
main classes.
Create a class named JMUlti that has, at a minimum,
a method with the signature:
public static void main(String[] args)
Your implementation must satisfy the following requirements:
This method must check to see if there are any command-line
arguments. If not, it must initialize an int
variable named number to 1; If there are
command-line arguments, it must convert args[0]
to an int value using the
static parseInt(String) method in
the Integer class and assign this value to
an int variable named number.
This method must construct number different
TextWindow objects
(each containing a unique TextEditor object).
Note: The technique you use must be appropriate for constructing
any number of objects. In other words, use a loop.
Each TextEditor must be editable and have a scrollbar.
Each TextWindow must confirm on exit, have a "File"
menu, and be visible.
Each TextWindow must have its own title
that includes the text "JMUlti -- Window" and the appropriate number
between 0 and number - 1. (Hint: You can use
String concatenation or the static
method named format() in the String class
that works just like the printf() method but, instead
of printing an appropriately formatted String, returns
one.)
Each TextEditor must initially contain the text
"JMUlti -- Window" and a number (between 0
and number - 1). This text should be selected (so
that it will be replaced when the user enters new text).
JMUlti.java.loop
import java.lang.reflect.Array;
/**
* The driver for JMUlti -- a simple multi-file text editor
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class JMUlti
{
/**
* The entry point for JMUlti
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
int length;
int number;
String title;
TextEditor editor;
TextWindow window;
length = Array.getLength(args);
if (length == 0)
{
number = 1;
}
else
{
number = Integer.parseInt(args[0]);
}
for (int i=0; i<number; i++)
{
title = String.format("JMUlti -- Window %d", i);
editor = new TextEditor();
editor.setEditable(true);
editor.setShouldHaveScrollBar(true);
editor.setText(title);
editor.selectAll();
window = new TextWindow(title);
window.setTextEditor(editor);
window.setShouldConfirmExit(true);
window.addFileMenu();
window.setVisible(true);
}
}
}
Why is it unnecessary to have references to all of the
TextEditor and TextWindow objects?
No methods need to be invoked after setVisible() is
called. So, there is no reason to keep a reference to each object.
(This is a very important point! So, as we continue to talk about objects,
make sure you think about this issue.)
Suppose you did need store have references to all of the
TextEditor and TextWindow objects, what
would you do?
Use arrays. However, arrays of objects can be a little tricky,
so we'll discuss them at length later in the semester.
Compile JMUlti.java.
Execute JMUlti with no command-line arguments.
Edit JMUlti.
Execute JMUlti with a small numeric command-line
argument. (Note: The windows may open "on top of"
each other, so you may have to move one to see the others).
Use one or more of the windows.
Exit JMUlti. (Note: Closing one window will
cause the application to exit. Hence, all of the other windows
will also close.)
5. Some More Practice Writing Methods:
This part of the labe will give you some more practice writing methods.
Re-write JMUlti.java in such a way that
the main() method iteratively invokes a private
static "helper" method named createWindow() that
is passed an int and constructs a single
TextWindow containing a TextEditor with all
of the same properties as in the existing implementation.
JMUlti.java.method
import java.lang.reflect.Array;
/**
* The driver for JMUlti -- a simple multi-file text editor
*
* @author Prof. David Bernstein, James Madison University
* @version 1.0
*/
public class JMUlti
{
/**
* The entry point for JMUlti
*
* @param args The command-line arguments
*/
public static void main(String[] args)
{
int length;
int number;
length = Array.getLength(args);
if (length == 0) number = 1;
else number = Integer.parseInt(args[0]);
for (int i=1; i<=number; i++)
{
createWindow(i);
}
}
/**
* Create a TextWindow with a particular number
*
* @param index The window number
*/
private static void createWindow(int index)
{
String title;
TextEditor editor;
TextWindow window;
title = String.format("JMUlti -- Window %d", index);
editor = new TextEditor();
editor.setEditable(true);
editor.setShouldHaveScrollBar(true);
editor.setText(title);
editor.selectAll();
window = new TextWindow(title);
window.setTextEditor(editor);
window.setShouldConfirmExit(true);
window.addFileMenu();
window.setVisible(true);
}
}