CSCI A201/A597 and I210

Lab Notes One

Second semester 2000-2001


One line description of the lab.
Goals for the first lab:

Here are the steps for this lab:

  1. Check that you appear in QuizSite.

  2. Check that you appear in Postem.

    If something is wrong please e-mail dgerman@indiana.edu immediately.

  3. Start Notepad (it should be under Utilities) and create a file.

  4. The text of the file should be:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hello, world!"); 
      } 
    } 
  5. Save this file as One.java on the desktop.

  6. Open an MS-DOS command window.

  7. Find the file that you have just created (use dir One.java or something similar). Note that you need to make sure you're accessing the folder that corresponds to the desktop.

  8. Compile the file (use javac One.java)

  9. Find the resulting class (dir One.class)

  10. Run the program: (use java One)

  11. Remember that these are the steps of program development:

    1. Create (edit) a program in a file (extension .java)
    2. Compile that .java file with javac (a .class gets created)
    3. Run the program (the .class file) with java

  12. Use your textbook. Sections 1.7, 1.8 are going to help you.

  13. Now change the contents of One.java as follows:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hi, there!"); 
      } 
    } 
    Note that the part that changed is in blue.

  14. Save the file, compile and run One once again. What's the outcome?

  15. Now change the file as follows:
    public class One { public static void main(String[] 
    args) { System.out.println("Hi,       there!"); } } 
    Notice we only change the amount of existing blank space, nothing else.

  16. Save and compile the file, then run the program. What's the outcome?

  17. Now change the file to:
    public 
    class 
    One 
    {
    public 
    static 
    void 
    main
    (
    String
    [
    ] 
    args
    ) 
    {
    System.out.println("Hi, there!"); 
    } 
    } 
  18. Save and compile the file, run the program. What's the outcome?

  19. Does the book have anything to say about it (pp. 21-22, 23)?

  20. Now change the program as follows:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hello, world!"); 
        System.out.println("Hello, again!"); 
      } 
    } 
  21. Compile and run the program. What's the outcome?

  22. Make a new change, and try this program:
    public class One {
      public static void main(String[] args) {
        System.out.println("       again!"); 
        System.out.println("Hello,       "); 
      } 
    } 
  23. Then try this one:
    public class One {
      public static void main(String[] args) {
        System.out.println("    are     "); 
        System.out.println("How         "); 
        System.out.println("        you?"); 
      } 
    } 
  24. Can you write a program that produces this output?
         4
        4
       4
      4   4
     44444444
          4
          4
    It's a big "4" made out of little "4"'s. (See also exercise P1.2 in the book).

  25. Try the following program and explain its output:
    public class One {
      public static void main(String[] args) {
        System.out.println(1 + 2 + 3); 
      } 
    } 
  26. Try the following program and explain its output:
    public class One {
      public static void main(String[] args) {
        System.out.println(1 + 2 * 3); 
      } 
    } 
  27. Is the next program different?
    public class One {
      public static void main(String[] args) {
        System.out.println((1 + 2) * 3); 
      } 
    } 
  28. Solve problem P1.3 (page 45) now.

  29. Try this program:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hello, D'Artagnan!"); 
      } 
    } 
    We can print single quotes quite easily.

  30. Now put "D'Artagnan" in double quotes:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hello, "D'Artagnan"!"); 
      } 
    } 
    Compile and run this program. What happens?

  31. Try this:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hello, \"D'Artagnan\"!"); 
      } 
    } 
    Explain the outcome.

  32. Now try this program:
    public class One {
      public static void main(String[] args) {
        System.out.println("Hello, \nworld!"); 
      } 
    } 

    Explain what it does.

  33. Recall the program that outputs this:
         4
        4
       4
      4   4
     44444444
          4
          4
    Can you write that program with just one System.out.println?

  34. Can you write a program that outputs a "4" made out of double quotes (")?
         "
        "
       "
      "   "
     """"""""
          "
          "
  35. Try this program and explain the outcome:
    public class One {
      public static void main(String[] args) {
        System.out.print("Hello, "); 
        System.out.print("world!"); 
      } 
    } 
  36. What's the output of this program?
    public class One {
      public static void main(String[] args) {
        System.out.print("A"); 
        System.out.print("B\n"); 
        System.out.print("C"); 
        System.out.print("D"); 
        System.out.print("E\n"); 
      } 
    } 
  37. Read section 1.9 (pp. 27-29) now, carefully.

  38. One final challenge. Can you write a program that produces this:
              .8.           8 888888888o          ,o888888o.
             .888.          8 8888    `88.       8888     `88.
            :88888.         8 8888     `88    ,8 8888       `8.
           . `88888.        8 8888     ,88    88 8888
          .8. `88888.       8 8888.   ,88'    88 8888
         .8`8. `88888.      8 8888888888      88 8888
        .8' `8. `88888.     8 8888    `88.    88 8888
       .8'   `8. `88888.    8 8888      88    `8 8888       .8'
      .888888888. `88888.   8 8888    ,88'       8888     ,88'
     .8'       `8. `88888.  8 888888888P          `8888888P'
    You don't need to write this program, just think whether you can write it or not.

    The Minute Paper for this week is as follows:

    On a piece of paper write your name and username and describe your expectations for this class. Hand this piece of paper to me in lab, when I come to your section.
    Problem Set One will be posted over the weekend.

    Also compile and run this program, as shown in lecture:

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.SwingConstants;
    import javax.swing.event.ChangeListener;
    import javax.swing.event.ChangeEvent;
    
    public class SliderTest
    {
      public static void main(String[] args)
      {
        SliderFrame frame = new SliderFrame();
        frame.setTitle("SliderTest");
        frame.show();
      }
    }
    
    class SliderFrame extends JFrame
    {
      public SliderFrame()
      {
        final int DEFAULT_FRAME_WIDTH = 300;
        final int DEFAULT_FRAME_HEIGHT = 300;
        setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
        addWindowListener(new WindowCloser());
        colorPanel = new JPanel();
        ColorListener listener = new ColorListener();
    
        redSlider = new JSlider(0, 100, 100);
        redSlider.addChangeListener(listener);
    
        greenSlider = new JSlider(0, 100, 70);
        greenSlider.addChangeListener(listener);
    
    
        blueSlider = new JSlider(0, 100, 100);
        blueSlider.addChangeListener(listener);
    
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new GridLayout(3, 2));
        southPanel.add(new JLabel("Red", SwingConstants.RIGHT));
        southPanel.add(redSlider);
        southPanel.add(new JLabel("Green", SwingConstants.RIGHT));
        southPanel.add(greenSlider);
        southPanel.add(new JLabel("Blue", SwingConstants.RIGHT));
        southPanel.add(blueSlider);
    
        Container contentPane = getContentPane();
        contentPane.add(colorPanel, "Center");
        contentPane.add(southPanel, "South");
    
        setSampleColor();
      }
    
      public void setSampleColor()
      {
        float red = 0.01F * redSlider.getValue();
        float green = 0.01F * greenSlider.getValue();
        float blue = 0.01F * blueSlider.getValue();
    
        colorPanel.setBackground(new Color(red, green, blue));
        colorPanel.repaint();
      }
      
      private JPanel colorPanel;
      private JSlider redSlider;
      private JSlider greenSlider;
      private JSlider blueSlider;
       
      private class ColorListener implements ChangeListener
      {
        public void stateChanged(ChangeEvent event)
        {
          setSampleColor();
        }
      }
    
      private class WindowCloser extends WindowAdapter
      {
        public void windowClosing(WindowEvent event) 
        {
            System.exit(0);
        }
      }
    } 
    You don't need to understand this program, but you should have no problem creating, compiling, and running it -- and you should feel good about it!
    Last updated: Jan 12, 2001 by Adrian German for A201