(answers in web view)
Name: _______________________________
Student ID: _______________________________

Total points: 70
  1. Describe the MVC design pattern, and give an example of each component of the MVC system. [5]
    Model:
    Data structures and methods to interact with the data. e.g., 2D array of letters for word-search
    View:
    Display/output, summary of the data, statistics, etc. e.g., Swing labels, text output
    Controller:
    Input/control from the user or other programs. e.g., Swing buttons, keyboard input






  2. What is an abstract superclass? What would you use one for? Give an example of such a situation (no code required, just describe the situation). Why does your example need to use an abstract superclass instead of a regular superclass? [5]
    An abstract class is not meant to be instantiated directly. An abstract superclass can be used as a way of organizing its subclasses under one category. For example, Dog, Cat, and Cow all are within the Mammal category. It makes sense to have specific instances of Dog, Cat, and Cow, but not of Mammal. Mammal is the abstract superclass. In Java, an abstract class is declared with the 'abstract' keyword. Concrete subclasses must override all abstract functions and provide bodies to them.







  3. What does the final keyword mean in Java as applied to (a) attributes, (b) methods, or (c) classes? [4]
    A final attribute is a constant: its value cannot be changed. A final method cannot be overriden by subclasses. A final class cannot be subclassed.



  4. Name and briefly describe at least three of the five Swing layout managers we learned about. [5]
    FlowLayout:
    widgets are placed side-by-side in order of add().
    BorderLayout:
    widgets can be placed on NORTH, SOUTH, EAST, WEST, and CENTER of the container.
    GridLayout:
    widgets are placed on a regular grid of equal-size cells.
    GridBagLayout:
    like GridLayout, but cells may be non-uniform size, and widgets may span multiple cells. Akin to HTML tables.
    GroupLayout:
    Specify constraints as ParallelGroups or SequentialGroups, independently for each axis.







  5. Tell me everything you know about serialization in Java. [5]
    Serialization is converting an abstract data structure (object) into a series of bytes suitable for writing into a file or sending over the network. (In Python this is "pickling".) In Java an object is serializable if its class implements the (empty) interface Serializable and if every non-transient attribute is serializable. The primitive types (int, char, etc.) and arrays of them are serializable. Non-serializable attributes can be skipped over by declaring them transient. A serializable object can be written out to an ObjectOutputStream by calling its writeObject() method. Similarly, objects (of unknown type) may be read from an ObjectInputStream by calling readObject().
  6. What is a copy constructor used for? On a separate page, implement in Java a Student class with attributes name (String), ID (int), and GPA (float). Write three constructors: one with no parameters, one with three parameters (String, int, float), and one that is a copy constructor. [8]
    A copy constructor performs deep copy: the one parameter is another object of the same type, and the copy constructor copies attributes from the other object into the current object.
    	public class Student {
    		String name;
    		int ID;
    		float GPA;
    
    		public Student( String name, int ID, float GPA ) {
    			if (name == null) name = "John Doe";
    			if (ID < 0) ID = 0;
    			if (GPA < 0.0) GPA = 0.;
    			if (GPA > 5.0) GPA = 5.0;
    			self.name = name;
    			self.ID = ID;
    			self.GPA = GPA;
    		}
    
    		public Student() { self( null, 0, 0. ); }
    
    		public Student( Student other ) {
    			if (other == null) {
    				self();
    			} else {
    				self( other.name, other.ID, other.GPA );
    			}
    		}
    	}
      
  7. What is the catch-or-declare rule in Java? Write a short function or two in Java that illustrates this rule. [6]
    Catch-or-declare means that if a method runs code that could throw an Exception, that method must either handle the exception itself or "pass the buck" by declaring that it could throw that exception: e.g., public void myMethod() throws IOException. If it declares the exception, then any code that calls the method is faced with the same catch-or-declare choice.
    	public Scanner openInput(File filename) throws IOException {
    		return new Scanner( filename );
    	}
    	public static void main( String[] args ) {
    		Scanner kbd;
    		try {
    			kbd = openInput("input.txt");
    		} except IOException {
    			System.out.println("error reading file");
    		}
    	}
    










  8. On a separate page, declare simple Java classes and sketch a UML class diagram that formalizes the following set of statements. Declare and diagram every (non-primitive) class used. Implementations of methods are not required.[10]
    interface Drawable {
    	void draw();
    }
    abstract class Shape implements Drawable {
    	private Colour lineColour;
    	private Colour fillColour;
    	public void draw() {};
    }
    class Rectangle extends Shape {
    	private float width;
    	private float height;
    }
    class Oval extends Shape {
    	private float radius;
    	private float eccentricity;
    }
    class Dot extends Shape {}
    class Square extends Rectangle {}
    class Circle extends Oval {}
    
  9. On a separate page, write a Java function that reads a list of integers from the file input.txt and prints out to the console: for each unique integer in the file, print the number of times it occurs in the file (i.e., a frequency table.) You may assume that the input will only be integers between 0 and 99. For example, if input.txt contained the numbers 12, 2, 0, 5, 2, 2, 12, then the output could be:
    N   Count
    12  2
    2   3
    0   1
    5   1
    
    Format the output so it lines up in neat columns. The output does not need to be in sorted order. You do not need to write a complete program that tests this (and you do not need keyboard-interactive user input), just a stand-alone function. Docstrings and comments are not necessary but may be good for partial credit. [12]
    See FreqTable.java.
  10. The Java code below implements a bare-bones Swing application (just an empty window). The code works properly. Modify the code to add a button labelled "Quit" that closes the application (use System.exit(0)) when clicked. Docstrings and comments are not necessary but may be good for partial credit. If you do not remember the exact names of classes/interfaces/methods, just make up something close and explain what you are trying to do. [10]
    import javax.swing.*;
    import java.awt.event.*;
    
    public class HiBye extends JFrame {
    
    
    
    
    
    	public HiBye() {
    		super( "HiBye: Click to quit" );
    
    
    
    
    
    
    
    
    
    
    		setSize( 100, 100 );
    		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		setVisible( true );
    
    
    	}
    
    
    
    
    
    
    
    
    	public static void main( String[] args ) {
    		new HiBye();
    	}
    }
    
    Either use an inner class to define the event handler (see HiBye_Inner.java), or make the HiBye class itself the event handler (see HiBye_AllInOne.java).