CIT 591 Final Exam

Name __________________________________
  1. (28 points) In this question you will write a complete Java program that repeatedly (1) reads in an integer from the user, (2) prints "Buzz" if the integer is divisible by 7, otherwise prints the integer, and (3) quits when a negative number or zero is entered.

    Include all necessary braces, semicolons, etc. so that your answers, taken together, are a complete, runnable Java program.

    1. Write the Java line that declares your program is in a folder named buzzer.
      package buzzer;

    2. You will need a Scanner. Import the appropriate class from java.util.
      import java.util.scanner;

    3. Write a Javadoc comment that, using the appropriate tag, names you as the program author.
      /**
        * @author Dave Matuszek
        */


    4. Declare the class. Name it Buzz.
      class Buzz {

    5. Write the method header for the main method.
          public static void main(String[] args) {

    6. Create a Scanner object to read input from the keyboard, and save it in a variable.
              Scanner scanner = new Scanner(System.in);

    7. Declare an integer variable, and use the Scanner to read in a value for it from the user.
              int n = scanner.nextInt();

    8. Start a loop that will keep running as long as the value you got from the user is positive.
              while (n > 0) {

    9. Call a method buzz (which you will write below). Pass it the integer as a parameter.
                  buzz(n);

    10. Read in the next integer.
                  n = scanner.nextInt();

                  }
            }
        
    11. Write the header for the buzz method, which takes one integer as a parameter.
          static void buzz(int n) {

    12. Test if the parameter is divisible by seven.
              if (n % 7 == 0) {

    13. Print "Buzz" if the above test passes.
                  System.out.println("Buzz");

    14. Print the number itself if the above test fails.
             } else System.out.println(n);


                          }
                }


  2. (3 points) In the above program, should the method buzz be marked static? Why or why not?

    Yes, because it doesn't need any instance information from the containing class.
    In fact, since no instance was created, the method must be static.






  3. (4 points) Assume you are inside a class Point with two instance variables, double x and double y.

    1. Write a constructor for Point which takes two parameters, x and y, and sets the corresponding instance variables.

      Point(double x, double y) {
          this.x = x;
          this.y = y;
      }


    2. Write a second constructor for Point which takes no parameters, and uses the first constructor to set both x and y to zero.

      Point() {
          this(0.0, 0.0);
      }


  4. (2 points) When a variable is declared within the body of a method, what is the scope of that method variable?
    From the point of declaration to the next closing brace.

  5. (2 points) What does a break statement break out of? (Give a complete answer, not just one thing.)
    Any loop (for, new for, while, do-while) and switch statements.


  6. (2 points) If a method is overloaded, what must be different between different versions of the method? (Be specific.)
    Number and/or types of parameters.



  7. (2 points) What is the very first thing that any constructor does?
    Calls the constructor for its superclass.


  8. (2 points) What does the optional @Override annotation do?
    Provide a warning if the method does not, in fact, override anything.

  9. (4 points) The ++ operator is supposed to add 1 to a variable. Write a complete unit test method, containing one test, to show that it works.

    @Test
    public static void testIncrement() {
        int n = 2;
         assertEquals(3, n++)
    }



  10. (2 points)
    1. What is the abstract superclass of the Exception class?
      Throwable

    2. What is the subclass of Exception used for unchecked exceptions?
      RuntimeException


  11. (6 points) Name each of the following layout managers:
    layouts
        BorderLayout                       GridLayout                       FlowLayout
  12. (6 points) What are the two ways of creating a new thread?
    a. Extend what class?
    Thread (extended by class X)

    d. Implement what interface?
    Runnable (implemented by class X)

    b. Provide what method?
    public void run()

    e. Provide what method?
    public void run()

    c. Start the thread running?
    create object of type X, call its start() method

    f. Start the thread running?
    create an object of type X, construct a Thread with X as a parameter, call the Thread's start() method


  13. (6 points) Write statements to do the following (each should be a single line):

    1. Create a button named okButton, with the text OK.

      JButton okButton = new JButton("OK");

    2. Assume you have an ActionListener class named MyActionListener. Create an instance of this class and attach it to the okButton.

      okButton.addActionListener(new MyActionListener());

    3. Assume you have a JPanel named buttonPanel. Put the okButton into this panel.

      buttonPanel.add(okButton);


  14. (4 points) When using Observer/Observable,

    1. What two statements should the Observable instance execute to indicate that something has changed?
      setChanged(true);
      notifyObservers();


    2. What method must the Observer instance provide? Give the complete signature.
      public void update(Observable obs, Object obj)



  15. (2 points) What does the Javadoc program do with the first sentence of a Javadoc comment?
    Use it as a summary statement.



  16. (2 points) What kind of a Reader will allow you to read a file one line at a time?
    BufferedReader



  17. (5 points) Write a complete method named nargs that takes any number of integer arguments, and returns the integer count of how many arguments it was given (Example: nargs(10, 20, 30) should return 3).

    public static int nargs(int...n) {
        return n.length;
    }





  18. (4 points) What are the two methods provided by every Iterator?
    boolean hasNext()
    T next()




  19. (4 points) Declare and define a Map variable named counts, whose keys are Strings and the values associated with those keys are integers.
    Map counts = new HashMap<Integer, String();


  20. (6 points) What is the git command to:

    1. Put all the files in this directory under the control of git
      git add .


    2. Copy a git repository from a given URL
      git clone the_url


    3. Merge changes from a remote repository into your local repository




  21. (4 points) charBlock is a two-dimensional array of printable characters. Write statements (not a complete method) to print out the array.
    for (i = 0; i < charBlock.length; i++) {
      for (int j = 0; j < charBlock[i].length; j++) {
        System.out.print(charBlock[i][j]);
      }
      System.out.println();
    }