JMU JMU - Department of Computer Science
Help Tools
Lab: Experience with Objects


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:

  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:
    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:
    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.
    Expand
  2. Read the documentation for the String java.lang.String class.
  3. How would you declare a variable of class String named course?


          String     course;
          
    Expand
  4. How would you instantiate the object named course above (so that it contains the letters "CS100")?


          course = new String("CS100");
          
    Expand
  5. Does the String class contain any methods that begin with set?


    No.
    Expand
  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.
    Expand
  7. Are these methods void? If not, what do they return?


    They aren't void, they return String objects.
    Expand
  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.
    Expand
  9. Read the documentation for the TextEditor class. (Note: It is in the package editor).
  10. How would you declare a variable of class TextEditor named editor?


          TextEditor        editor;
          
    Expand
  11. How would you instantiate the object named editor above (using the default constructor)?


          editor = new TextEditor();
          
    Expand
  12. How would you call the method with the signature:
            public void selectAll()
    	
    that belongs to the object named editor?


          editor.selectAll();
          
    Expand
  13. Are TextEditor objects mutable or immutable?


    They are mutable. Most obviously, the setText() method changes the text in the owning object.
    Expand
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.
  1. Create (and start editing) a file named JMUview.java that contains the following "shell":
    import editor.*;
    
    /**
     * 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.

  2. Add code to the main() method that:
    1. Declares a variable of class TextEditor named editor.
    2. Declares a variable of class TextWindow named window.
    3. Instantiates editor using the default constructor.
    4. Calls the setShouldHaveScrollBar() method of editor passing the boolean literal true.
    5. Calls the setEditable() method of editor passing the boolean literal false.
    6. In an appropriate place, instantiates window passing the String literal "JMUview" to the explicit value constructor.
    7. In an appropriate place, calls the setTextEditor() method of window passing the object editor.
    8. 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.
    9. 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);
        }
    }
            
    Expand
  3. Compile JMUview.java.
  4. Execute JMUview passing it no command-line arguments.
  5. 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.
  1. 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:

    1. Declare and construct a TextEditor object.
    2. Tell the TextEditor object it should be "editable".
    3. Tell the TextEditor object it should have a scrollbar.
    4. Declare and construct a TextWindow object with the title "JMUedit".
    5. Tell the TextWindow object it should use the TextEditor object you already constructed.
    6. Tell the TextWindow object that it should ask the user to confirm exit requests.
    7. Tell the TextWindow object to add a "File" menu.
    8. 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);
        }
        
    }
            
    Expand
  2. Compile JMUedit.java.
  3. Execute JMUedit, navigate to a directory/folder that contains a text file, and open it.
  4. Exit JMUedit and make sure that it "confirms".
  5. 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.
  1. 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:

    1. 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.
    2. 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.
    3. Each TextEditor must be editable and have a scrollbar.
    4. Each TextWindow must confirm on exit, have a "File" menu, and be visible.
    5. 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.)
    6. 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);
            }
        }
        
    }
            
    Expand
  2. 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.)
    Expand
  3. 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.
    Expand
  4. Compile JMUlti.java.
  5. Execute JMUlti with no command-line arguments.
  6. Edit JMUlti.
  7. 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).
  8. Use one or more of the windows.
  9. 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.
  1. 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);
        }
    }
            
    Expand
  2. Test your new implementation.

Copyright 2024