Shit Sheet

of 36

  • date post

    10-Apr-2018
  • Category

    Documents

  • view

    223
  • download

    0

Embed Size (px)

Transcript of Shit Sheet

  • 8/8/2019 Shit Sheet

    1/36

    I/O Streams and Files

    // Example for File I/O

    import java.lang.System;

    import java.io.FileInputStream;

    import java.io.FileOutputStream;

    import java.io.File;

    import java.io.IOException;

    public class FileIOApp {

    public static void main ( String [] arg0 ) throws IOException {

    FileOutputStream outStream =

    new FileOutputStream ("test.txt");

    String s = "This is a test.";

    for ( int i =0; i

  • 8/8/2019 Shit Sheet

    2/36

    then uses the method delete( ) to delete the file.

    The program's output is given by:

    The inStream has 15 bytes available.

    15 bytes were read.

    They are: This is a test.

    Chapter 11 Exception Handling

    An exception is an event that occurs during the execution of a

    program and makes program continuation impossible or undesirable.

    Examples of exception are: integer division by zero, array reference

    with index out of bounds, loading a file that cannot be found on disk,

    printer out of paper in an attempt to print, and so on.

    If any of the code in a method throws an exception of a type other

    than the ones named in the catch clauses, Java exits this method

    immediately. (Hopefully, one of its callers has already coded a catch

    clause for that type). Any exception that occurred during the executionof doFileProcessing() or displayResults() would be caught and

    processed. If an exception occurred during doFileProcessing(),

    displayResults() would never get called, and execution would proceed

    directly to the catch block.

    The finallystatement can be used in conjunction with a tryblock.

    Basically, it ensures that if

    an exception occurs, any cleanup work that is necessary is taken care of.

    code in a finallystatement is guaranteed to run no matter whether an exception occurs o

    not.

    Example:

    Graphics g = image.getGraphics();

    try

    {

    // section 1

    code that might throw exceptions// section 2

    }

    catch ( IOException e )

    {

    // section 3

    show error dialog

    // section 4

    }

    finally

    {

    // section 5

  • 8/8/2019 Shit Sheet

    3/36

    g.dispose();

    }

    // section 6

    Scenarios:

    1. The code throws no exceptions(Execution path: section 1, 2, 5, 6 )

    2. The code throws an exception that is caught in a catch clause, in our

    case, an IOException. In this scenario, execution passes through points

    1, 3, 4, 5, and 6. If the catch clause throws an exception, then the

    exception is thrown back to he caller of this method, and execution

    passes through points 1, 3, and 5 only (no 6).

    3. The code throws an exception that is not caught in any catch clause.

    (Execution path: 1 and 5 only, no 6)

    Exception (all checked, except for RuntimeException)

    RuntimeException (unchecked) Other subclasses of

    Exception, all checked.

    ArithmeticException IOException (checked)IndexOutOfBoundsException

    FileNotFoundException

    ArrayIndexOutOfBoundsException

    MalformedURLException

    IllegalArgumentException InterruptedException

    (checked)

    NumberFormatException

    The argument to throw can be any expression that returns an

    object of a subclass of Throwable. You usually use new and a

    constructorto create the object. To throw an exception, we find an

    appropriate exception class in the class library if there is one.

    Otherwise, we define our own class by extending the Throwable class

    or one of its subclasses. We then make an object of that class and

    throw it.

    Users of a method need to know the exceptions it might throw sothey can be prepared to handle them.Java requires that a method

    definition includes a list of the checked exceptions the method throws

    in the header.

    import java.lang.*;

    import java.io.*;

    public class example2

    { public static void main(String[] args) throws IOException

    {

    System.out.println("Within main\n");

    test1();

    }

  • 8/8/2019 Shit Sheet

    4/36

    public static void test1() throws IOException

    {

    try {

    System.out.println("Within test1\n");

    test2();

    }

    catch( RuntimeException e ) {

    System.out.println("Caught RuntimeException\n");

    }

    }

    public static void test2() throws IOException

    // it can throw spuerclass for checking but in test1 and main need to

    check spuerclass {

    try {

    System.out.println("Within test2\n");

    throw new IOException();}

    catch ( NumberFormatException e ){

    System.out.println("Caught NumberFormatException\n");

    }

    }

    }

    Case 1: A checked exception

    public void openFile()try {

    infile = new BufferedReader ( new

    FileReader ("myFile.txt") );

    }

    catch ( FileNotFoundException e) {

    errorField.setText ("Missing file, please try again!");

    }

    // The FileReader constructor throws a checked exception which wemust deal with. We can either use try-catch, or specify that the

    method which contains the above statement can throw the exception.

    The second approach is to pass the exception up to the invoking

    method as follows:

    public void openFile() throws IOException {

    infile = new BufferedReader ( new

    FileReader ( "myFile.txt") );

    }

    Case 2: An unchecked exception

    Because NumberFormatException (a subclass of RuntimeException)

    is unchecked, the compiler will not force us to check it. We can catch it

  • 8/8/2019 Shit Sheet

    5/36

    or ignore it (but there is no advantage to pass it upward).

    Case 3: A RuntimeException

    If we want the origin of the exception to be indicated so that it can

    be tracked down and fixed, the easy way is to ignore the exception.

    (It's ok, because the compiler does not check for this kind of

    exception).

    Programming pitfalls: Failing to acknowledge a checked exception will produce a

    compilation error. You must either catch an exception locally, or

    specify that your method throws it. Do not micromanage exceptions, i.e., do not put each line or small

    section of code in a separate try-catch block. Do not squelch exceptions, i.e., if something goes wrong, do not

    shut up that error by doing

    try

    { doing something wrong }catch ( Throwable e ) {}

    Allowing an exception to be thrown from a method, when it can

    sensibly be handled locally, dramatically slows down your code.

    Chapter 9 User Interface Components with Swing

    The flow layoutmanageris the default layout manager for apanel.

    Flow layoutmanager: lines the components horizontally until there

    is no more room and then starts a new row of components.e.g.,

    setLayout (new FlowLayout(FlowLayout.LEFT));

    // align to the left; default = center alignment

    (The method setLayoutis defined injava.awt.Container.)

    Border Layout(p.371) - using the borderlayoutmanager, you can

    choose to place the component in the center, north, south, east, or

    west of the container. It is the default layout manager of the content

    pane of everyJFrame.e.g.,

    //To change the layout manager of a

    // JPanel object.

    class MyPanel extends JPanel {

    setLayout ( new BorderLayout() );

    . . .

    add ( yellowbutton, BorderLayout.SOUTH);

    }

    The Grid Layout Manager. All rows and columns have the same size.

    public GridLayout(int rows, int cols)

    public GridLayout(int rows, int cols, int hgap, int vgap) // hgap, vgap:

  • 8/8/2019 Shit Sheet

    6/36

    horizontal and vertical gap

    panel.setLayout( new GridLayout(5,4) );

    JLabel

    JLabels are components that hold text. They do not react to user

    input.

    JLabel label = new JLabel( User name: , SwingConstants.RIGHT );

    panel.add( label );

    Text Input

    JTextField(text fields): single line of text

    JTextArea (text areas): multiple lines of text

    Both inherit from the abstract classJTextComponent.

    For text fields (p.378):

    JPanel p = new JPanel ();

    JTextField tf = new JTextField ("Default input", 20);

    // Adds a text field and initializes its content;sets the width equal to 20 columns.

    p.add (tf);

    tf.setText ( "HKBU");

    //Changes the content of the text field.

    tf.setColumn (10);

    // Change the size of the text field.

    p.validate();

    // Recompute the size and layout of all components in a container.String s = tf.getText().trim();

    // Return the exact text and trim any extraneous spaces for the data in

    the text field.

    interface DocumentListener(3 methods)

    insertUpdate(..)

    character(s) inserted

    removeUpdate(..)

    character(s) deletedchangedUpdate(..)

    changed in formatting, etc.

    DocumentListener listener = new ClockFieldListener();

    // add a panel with text fields

    JPanel panel = new JPanel();

    panel.add(new JLabel("Hours:"));

    hourField = new JTextField("12", 3);

    panel.add(hourField);

  • 8/8/2019 Shit Sheet

    7/36

    hourField.getDocument().addDocumentListener(listener);

    panel.add(new JLabel("Minutes:"));

    minuteField = new JTextField("00", 3);

    panel.add(minuteField);

    minuteField.getDocument().addDocumentListener(listener);

    add(panel, BorderLayout.SOUTH);

    // add the clock

    clock = new ClockPanel();

    add(clock, BorderLayout.CENTER);

    pack();

    }

    /**

    Set the clock to the values stored in the text fields.*/

    publicvoid setClock()

    {

    try

    {

    int hours = Integer.parseInt(hourField.getText().trim());

    int minutes = Integer.parseInt(minuteField.getText().trim());

    clock.setTime(hours, minutes);}

    catch (NumberFormatException e) {}

    // don't set the clock if the input can't be parsed

    }

    publicstaticfinalint DEFAULT_WIDTH = 300;

    publicstaticfinalint DEFAULT_HEIGHT = 300;

    private JTextField hourField;private JTextField minuteField;

    private ClockPanel clock;

    privateclass ClockFieldListener implements DocumentListener

    {

    publicvoid insertUpdate(DocumentEvent event) { setClock(); }

    publicvoid removeUpdate(DocumentEvent event) { setClock(); }

    publicvoid changedUpdate(DocumentEvent event) {}

    }

    }

    Note: The model for all text components is described by the Document

  • 8/8/2019 Shit Sheet

    8/36

    interface. In the statement

    minuteField.getDocument().addDocumentListener(listener);

    the minuteFieldobtains the data model "document" through the meth

    getDocument( ).

    For text areas, the user can enter any number of lines of text, using

    the ENTER key to separate them.

    JTextArea textArea = new JTextArea (8, 40);

    // 8 lines of 40 columns

    frame.add (textArea);

    Internally, each line ends with '\n'.

    textArea.setLineWrap (true); // long lines are wrapped

    visually; no '\n' characters are inserted into the text.Automatic Scrolling

    textArea = new JTextArea ( 8, 40 );

    JScrollPane scrollPane = new JScrollPane( textArea );

    frame.add( scrollPane, BorderLayout.CENTER );

    JPasswordField

    The characters that the user entered are not actually displayed, bu

    represented by an echo character (typically *).

    JPasswordField( String text, int columns)

    // add button to append text into the text area

    JButton insertButton = new JButton("Insert");

    buttonPanel.add(insertButton);

    insertButton.addActionListener(new

    ActionListener()

    {

    publicvoid actionPerformed(ActionEvent event){

    textArea.append("The quick brown fox jumps over the lazy

    dog. ");

    }

    });

    // add button to turn line wrapping on and off

    wrapButton = new JButton("Wrap");buttonPanel.add(wrapButton);

    wrapButton.addActionListener(new

  • 8/8/2019 Shit Sheet

    9/36

    ActionListener()

    {

    publicvoid actionPerformed(ActionEvent event)

    {

    boolean wrap = !textArea.getLineWrap();

    textArea.setLineWrap(wrap);

    scrollPane.revalidate();

    wrapButton.setText(wrap ? "No Wrap" : "Wrap");

    }

    });

    Radio Buttons

    ButtonGroup group = new ButtonGroup();

    JRadioButton smallButton = newJRadioButton( Small, false );group.add( smallButton );

    JRadioButton mediumButton = newJRadioButton( Medium,

    false );

    group.add( mediumButton );

    Sliders

    JSlider slider = new JSlider(min, max, initialValue); // default: 0, 100,

    JSlider slider = new JSlider(SwingConstants.VERTICAL, min, max,

    initialValue);ChangeEvent, addChangeListener(), stateChanged()

    snap to ticks move slider to closest tick

    // common listener for all sliders

    listener = new ChangeListener()

    {

    public void stateChanged(ChangeEvent event)

    {

    // update text field when the slider value changesJSlider source = (JSlider) event.getSource();

    textField.setText("" + source.getValue());

    }

    };

    // add a plain slider

    JSlider slider = new JSlider();

    addSlider(slider, "Plain");

    // add a slider with major and

    minor ticks

    slider = new JSlider();

    slider.setPaintTicks(true);

    slider.setMajorTickSpacing(20

    );

    slider.setMinorTickSpacing(5);

    addSlider(slider, "Ticks");

    // add a slider that snaps to

  • 8/8/2019 Shit Sheet

    10/36

    ticks

    slider = new JSlider();

    slider.setPaintTicks(true);

    slider.setSnapToTicks(true);

    slider.setMajorTickSpacing(20

    );

    slider.setMinorTickSpacing(5);

    addSlider(slider, "Snap to

    ticks");

    // add a slider with no track

    slider = new JSlider();

    slider.setPaintTicks(true);

    slider.setMajorTickSpacing(20

    );

    slider.setMinorTickSpacing(5);

    slider.setPaintTrack(false);addSlider(slider, "No track");

    // add an inverted slider

    slider = new JSlider();

    slider.setPaintTicks(true);

    slider.setMajorTickSpacing(20

    );

    slider.setMinorTickSpacing(5);

    slider.setInverted(true);addSlider(slider, "Inverted");

    // add a slider with numeric

    labels

    slider = new JSlider();

    slider.setPaintTicks(true);

    slider.setPaintLabels(true);

    slider.setMajorTickSpacing(20

    );slider.setMinorTickSpacing(5);

    addSlider(slider, "Labels");

    // add a slider with alphabetic

    labels

    slider = new JSlider();

    slider.setPaintLabels(true);

    slider.setPaintTicks(true);

    slider.setMajorTickSpacing(20

    );

    slider.setMinorTickSpacing(5);

    Dictionary labelTable = new

    Hashtable();

    labelTable.put(0, new

    JLabel("A"));

    labelTable.put(20, new

    JLabel("B"));

    labelTable.put(40, newJLabel("C"));

    labelTable.put(60, new

    JLabel("D"));

    labelTable.put(80, new

    JLabel("E"));

    labelTable.put(100, new

    JLabel("F"));

    slider.setLabelTable(labelTable);

    addSlider(slider, "Custom

    labels");

    // add a slider with icon labels

    slider = new JSlider();

    slider.setPaintTicks(true);

    slider.setPaintLabels(true);

    slider.setSnapToTicks(true);slider.setMajorTickSpacing(20

    );

    slider.setMinorTickSpacing(20);

    labelTable = new Hashtable();

    // add card images

    labelTable.put(0, new JLabel(new ImageIcon("nine.gif")));

    labelTable.put(20, new JLabel(new ImageIcon("ten.gif")));

    labelTable.put(40, new JLabel(new ImageIcon("jack.gif")));

    labelTable.put(60, new JLabel(new ImageIcon("queen.gif")));

    labelTable.put(80, new JLabel(new ImageIcon("king.gif")));

  • 8/8/2019 Shit Sheet

    11/36

    labelTable.put(100, new JLabel(new ImageIcon("ace.gif")));

    slider.setLabelTable(labelTable);

    addSlider(slider, "Icon labels");

    // add the text field that displays the slider value

    textField = new JTextField();

    add(sliderPanel, BorderLayout.CENTER);

    add(textField, BorderLayout.SOUTH);

    }

    Chapter 8 Event Handling

    What is an event?

    An event is (in most cases) an action taken by the user. For example,

    the user presses a key, clicks the mouse, etc

    In Java, you completely control how an event is transmitted from the

    event source (buttons, scrollbars, etc.) to the event listener(an object

    that can carry out the response you want for the event). This Eventdelegationmodelgives us much more flexibility than VB (in VB, the

    listener is predetermined; in Java, you can designate any object to be

    an event listener).

    eventSourceObject.addEventListener( eventListenerObject);

    e.g.,

    ActionListener listener = ;

    JButton b = new JButton ("OK");

    b.addActionListener(listener);// the method addActionListener is defined in class Button.

    E.g.,

    . . .

    ActionListener listener = ;

    JButton b = new JButton ("OK");

    b.addActionListener(listener);

    . . .

    class MyListener implements ActionListener{ . . .

    public void actionPerformed ( ActionEvent evt )

    { // reaction to button click goes here

    . . .

    }

    Listener Interfaces (See p.359 for a complete list.)

    Name Methods Accessors Events

    generated by

    ActionListener actionPerformed getSource

    JButton

    isSelected CheckBox

  • 8/8/2019 Shit Sheet

    12/36

    getActionCommand MenuItem

    (getSource) JComboBox

    Abstractbutton

    JTextField

    Timer

    TextListener textValueChanged getText

    TextComponent

    #DocumentListener getText

    TextComponent

    insertUpdate

    removeUpdate

    changedupdate

    MouseListener mousePressed MouseEventComponent

    mouseReleased getClickCount

    mouseEntered getX, getY

    mouseExited getPoint

    mouseClicked isPopupTrigger

    WindowListener windowClosing getWindow Window

    windowOpenedwindowIconified

    windowDeconified

    windowClosed

    windowActivated

    windowDeativated

    KeyListener keyPressed getKeyCode Component

    keyReleased getKeyCharkeyTyped getKeyModifiersText

    #ListSelectionListener

    valueChanged getSource List

    Adapter Classes

    Implementing six methods that do nothing is a tedious work. To

    simplify this work, each of the listener interfaces that has more than

    one method comes with a companion adapterclass that implements

    all the methods in the interface but does nothing with them.

    For example, the WindowAdapter class has seven do-nothing

  • 8/8/2019 Shit Sheet

    13/36

    methods. This means the adapter class automatically satisfies the

    technical requirements of using interface.

    These are adapters:

    WindowAdapter, ComponentAdapter, ContainerAdapter,

    FocusAdapter,

    KeyAdapter, MouseAdapter, MouseMotionAdapter.

    addWindowListener ( new WindowAdapter()

    { public void windowClosing (WindowEvent e) { System.exit

    (0);}

    }

    );

    Handling Keyboard Events

    Pressed the SHIFT key (keyPressedfor VK_SHIFT)

    Pressed the "A" key (keyPressedfor VK_A)

    Typed "A" key (keyTypedfor A)Released the "A" key (keyReleasedfor VK_A)

    Released the SHIFT key (keyReleasedfor VK_SHIFT)

    The keyTypedevent reports the characters that were typed (A)

    whereas the keyPressesdand keyReleasedevents report on the raw

    keystrokes (representing by the virtual key codes).

    When we override the method keyPressedand keyReleased, we use

    the getKeyCode method to obtain the virtual key code:void keyPressed( keyEvent evt) {

    int keyCode = evt.getKeyCode ();

    }

    When we override the keyTypedmethod, we use the getKeyChar

    method to obtain the actual character typed:

    char keyChar = evt.getKeyChar();

    Mouse Events (p.349)You do not need to handle mouse events explicitly if you just want

    the user to be able to click on a button or menu.

    These mouse operations are handled internally by the various

    components in the GUI. If you want to draw with the mouse, you

    need to trap mouse move, click, and drag events.

    Methods of MouseListener interface:

    mousePressed, mouseReleased, mouseClicked,

    mouseEntered, mouseExitedMethods of MouseMotionListener interface:

    mouseDragged, mouseMoved

  • 8/8/2019 Shit Sheet

    14/36

    Method of MouseWheelListener interface:

    mouseWheelMoved

    A useful method: MouseEvent.getClickCount()

    To detect if the right (nonprimary) mouse button is down:

    if ((event.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK)

    != 0 ) {}

    // Note that MouseEvent extends InputEvent.

    // the square containing the mouse cursor

    private class MouseHandler extends MouseAdapter

    {

    public void mousePressed(MouseEvent event)

    {

    // add a new square if the cursor isn't inside a square

    current = find(event.getPoint());

    if (current == null) add(event.getPoint());

    }public void mouseClicked(MouseEvent event)

    {

    // remove the current square if double clicked

    current = find(event.getPoint());

    if (current != null && event.getClickCount() >= 2)

    remove(current);

    }

    }private class MouseMotionHandler implements MouseMotionListener

    {

    public void mouseMoved(MouseEvent event)

    {

    // set the mouse cursor to cross hairs if it is inside

    // a rectangle

    if (find(event.getPoint()) == null)setCursor(Cursor.getDefaultCursor());

    else

    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

    }

    public void mouseDragged(MouseEvent event)

    {

    if (current != null)

    {

    int x = event.getX();

    int y = event.getY();

  • 8/8/2019 Shit Sheet

    15/36

    // drag the current rectangle to center it at (x, y)

    current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2,

    SIDELENGTH, SIDELENGTH);

    repaint();

    }

    }

    }

    Actions

    to separate the responsibilities of getting user input and executing

    commands;

    multiple ways to activate the same command: menu, keystroke,

    button, etc.

    one class object for each command;

    one listener for multiple event sources

    MulicastingOne event source, many listeners

    Simply add multiple listeners to an event source.

    There is no guarantee about the order in which the events are

    delivered to the listeners.

    Chapter 7 Graphics Programming

    Java Graphics libraries

    AWT (Abstract Window Toolkit) - for basic GUI (Graphical User Interfaceprogramming

    Swing - additional set of user interface components provided by Java 1.

    Frame - a top-level window (a window that is not contained inside

    another window) with a title and a border is called a frame. Frame is a

    subclass of class Container.

    import javax.swing.*;

    publicclass SimpleFrameTest

    {publicstaticvoid main(String[]

    args)

    {

    SimpleFrame frame = new

    SimpleFrame();

    frame.setDefaultCloseOperation(

    JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    }

    }

    class SimpleFrame extends

    JFrame

    {

    public SimpleFrame(){

    setSize(DEFAULT_WIDTH,

    DEFAULT_HEIGHT);

    }

    publicstaticfinalint

    DEFAULT_WIDTH = 300;

    publicstaticfinalint

    DEFAULT_HEIGHT = 200;

    }

    // get screen dimensions

  • 8/8/2019 Shit Sheet

    16/36

    Toolkit kit =

    Toolkit.getDefaultToolkit();

    Dimension screenSize =

    kit.getScreenSize();

    int screenHeight =

    screenSize.height;

    int screenWidth = screenSize.wi

    2D Shapes

    The Java 2D library organizes geometric shapes in an object-oriented

    fashion. There are

    abstract classes Line2D, Rectangle2D,

    Ellipse2D, and etc. Each class contains two static inner classes Float

    Double, e.g.,

    Rectangle2D.Float floatRect = new Rectangle2D.Float( 10.0F, 25.0F,

    22.5F, 20.0F );

    // left x, top y, width, height; float values

    Rectangle2D.Double doubleRect = new Rectangle2D.Double( 10.0, 2

    22.5, 20.0 );// double values

    //example

    Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width,

    height);

    g2.draw(rect);

    // draw the enclosed ellipse

    Ellipse2D ellipse = new Ellipse2D.Double();

    ellipse.setFrame(rect);g2.draw(ellipse);

    // draw a diagonal line

    g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY +

    height));

    // draw a circle with the same center

    double centerX = rect.getCenterX();

    double centerY = rect.getCenterY();double radius = 150;

    Ellipse2D circle = new Ellipse2D.Double();

    circle.setFrameFromCenter(centerX, centerY, centerX + radius,

    centerY + radius);

    g2.draw(circle);

    Colors

    g2.setPaint( Color.red ); // predefined red color

    g2.drawString( Warning!, 100, 100 );

    Predefined colors: red, blue, green, black, magenta, yellow, etc. (RED,

    BLUE, etc. are also ok.)

    Custom colors specification:

  • 8/8/2019 Shit Sheet

    17/36

  • 8/8/2019 Shit Sheet

    18/36

  • 8/8/2019 Shit Sheet

    19/36

    cannot have any static method.

    Any class can promise to implement the interface using the keyword

    implements. Any class that implements an interface must

    implement all its methods.

    Differences between using interfaces and abstract classes:

    If multiple inheritance is important, use interfaces, otherwise use

    abstract classes. This is because an abstract class can provide someor all of the methods that can be inherited easily.

    Interfaces never have instance fields, and the methods are never

    implemented in the

    interfaces.

    Arrays.sort(staff); //Library method calls compareTo().

    class Employee implements Comparable

    {

    public Employee(String n, double s){ }

    publicint compareTo(Employee other)

    {

    if(salary < other.salary) return -1; //to compare two string return

    name.conmpareTo(other.name);

    if(salary > other.salary) return 1; //to compare two hireday

    return hireday.compareTo(other.hireday);

    return 0;}

    }

    x = new Comparable( ); // error

    Comparable y; // ok

    y = new Employee( ); // ok if Employee implements Comparable

    if ( y instanceof Comparable ) { } // ok

    You can extend interfaces. You can also put constants in interfaces. The

    fields are always

    public static final.

    Object Cloning

    Employee original = new Employee(John Public, 50000);

    Employee copy = original; // copying

    copy.raiseSalary(10); // oops also changed original

    Using the clone() method in the Object class copies only the first level of

    fields. If there are immutable subobjects in the

    data fields (such as a String object), this library clone() method works fiEmployee copy = original.clone();

    copy.raiseSalary(10); // OK original unchanged.

  • 8/8/2019 Shit Sheet

    20/36

    Quite frequently, subobjects are mutable, and one must override the clo

    method to make

    a deep copy that clones the subobjects as well. The relevant class mu

    also

    implement the Cloneable interface.

    class Employee implementsCloneable {

    public Employee(String n, double s) { ... }

    public Employee clone() { ... }

    }

    public Employee clone() throws CloneNotSupportedException

    {

    // call Object.clone()

    Employee cloned = (Employee) super.clone();

    // clone mutable fields

    cloned.hireDay = (Date) hireDay.clone();return cloned;

    }

    Inner classes

    Inner classes are classes defined within another class.

    Only inner classes can be private. Regular classes always have either package or

    public visibility.

    Advantages: ability to access instance fields of the outer classes; inner classes can beprivate to ensure encapsulation

    publicvoid start()

    {

    // Creating an inner class object.

    ActionListener listener = new TimePrinter();

    Timer t = new Timer(interval, listener);

    t.start();

    }Local inner classes

    Define a class within a method. The class is usable only in that method (always private to

    method). No access modifier is needed.

    For example, in Example 6-4, the inner class TimePrinter is used only once.

    public void start()

    {

    class TimePrinter implements ActionListener

    {

    public void actionPerformed(ActionEvent event)

    {

    Date now = new Date();

  • 8/8/2019 Shit Sheet

    21/36

  • 8/8/2019 Shit Sheet

    22/36

    methods of the old class (superclass). However, objects of the superclass cannot use fields newly

    defined in the subclass.

    A subclass object cannot access the private fields of the superclass object.

    Polymorphism

    Another way of formulating the is a rule is the substitution principle. You can use a

    subclass object whenever the program expects a superclass object.

    Employee e;

    e = new Employee( ); // Employee object expected

    e = new Manager( ); //OK, a Manager object can be used as well.

    Object variables in Java are polymorphic, i.e., an object variable of a class can refer to an

    object of this particular class or any of the subclasses.

    Another form of polymorphism is discussed in the section about overriding (see below),

    where one method name is being overloaded or overridden.

    We can assign a subclass object to a superclass variable.

    Manager boss = new Manager ( John, 70000, 1990, 1, 2 );

    Employee em1 = new Employee ( Lee, 50000, 1992, 3, 1 );

    Employee em2 = new Employee ( Wong, 55000, 1994, 9, 4 );

    Employee em3 = new Employee (Chan, 60000, 1996, 3, 8 );

    Employee EM = boss; // ok!

    /* In this case, EM and boss refer to the same area of memory. The actual type of

    EM is Manager, but the variable type of EM is Employee. */Employee[] staff = new Employee[4];

    staff [0] = boss;

    /*Ok, although the type ofstaff[0] isEmployee and the type ofboss is Manager. */

    staff [1] = em1;

    staff [2] = em2;

    staff [3] = em3;

    However, the converse (assigning a superclass object to a subclass variable) is false in general.

    superclass object cannot be used as a subclass object.Overloading and overriding

    The scenario that methods in different classes of an inheritance hierarchy have the same

    signature (method name and parameter list) and same return type but different contents is

    called overriding that method name (polymorphism). Notice that the return type is not

    part of the signature.

    Overloading a class provides more than one method with the same name, but with

    different signatures.

    Overriding a class hierarchy replaces the superclass's implementation of a method inthe subclass. Their signatures and return types must be identical. Note that only

    non-static methods may be overridden. For example,

  • 8/8/2019 Shit Sheet

    23/36

    // in class Employee

    public void raiseSalary( double byPercent ) {

    salary *= ( 1. + byPercent / 100. );

    }

    // in class Manager

    public void raiseSalary( double byPercent ) {

    Date today = new Date();

    //addition 0.5% for each year of service

    double bonus_percent =

    0.5* ( today.getYear() getHireDay().getYear() );

    super.raisesalary( byPercent + bonus_percent );

    }

    When you override a method, the subclass method must be at least as visible as the

    superclass method. For example,

    If the superclass method is public, the subclass method must also be public.

    If the superclass method is private, the subclass method can be public. Also, in this case, if the method is named, say,func(), the subclass method cannot call

    super.func().

    When an object of a subclass calls a method name (this method may or may not have been

    overridden in the inheritance hierarchy), which one will actually be invoked? The rules are:

    (1) The subclass checks whether or not it has a method with that name and with exactly

    the same parameters. If so, use it. If not,

    (2) Java moves to the parent class and looks there for a method with that name and thoseparameters. If so, it calls that method. If not,

    (3) Java continues to move up the inheritance chain. The parent classes are checked

    until the chain of inheritance stops (an error) or until Java finds a matching method

    This is one of the fundamental inheritance rules: a method redefined in a subclass hides orove

    the method of the ancestor class.

    Dynamic binding

    Dynamic binding occurs when a superclass and a subclass both have a method of the same sig

    and return type.

    Ifan object of a subclass is assigned to a superclass variable, and the variable calls an overrid

    method, which one of the overridden methods in the inheritance hierarchy will be invoked?

    The answer is - according to the actual type of the object itself, not the type of the variable.

    Consider the code from Example 5-1:

    for (int i = 0; i < staff.length; i++) {

    Employee e = staff[i];

    // staff[0] is of type boss; staff[1] and staff[2] are of type EmployeeSystem.out.println( "name=" +

    e.getName() + //static binding

  • 8/8/2019 Shit Sheet

    24/36

    ", salary = " + e.getSalary()); // dynamic binding

    }

    The Java compiler produces the method tables for the Employee and Manager classes (in Exa

    5-1):

    Employee:

    getName() Employee.getName()

    getSalary() Employee.getSalary()

    getHireDay() Employee.getHireDay()

    raiseSalary( double ) Employee.raiseSalary( double )

    Manager:

    getName() Employee.getName()

    getSalary()Manager.getSalary()

    getHireDay() Employee.getHireDay()

    raiseSalary( double ) Employee.raiseSalary( double )

    setBonus( double ) Manager.setBonus( double )

    Constructors in inheritance hierarchy

    Every constructor of a subclass must somehow call a constructor of the superclass (to set up

    the data fields of the superclass).

    (1) In a constructor of a subclass, we can explicitly usesuperwith the appropriate

    (2) parameters to invoke one of the superclass's constructors. This must be done in the

    first statement of the subclasss constructor.

    (2) If the subclass constructor does not call a superclass constructor explicitly, the default

    constructor of the superclass will be called automatically by the compiler.

    By convention, if a class does not have any constructor, the Java compiler will

    automatically generate a default constructor. However, if the class has one or more cons

    that requires parameters (but no default constructor), the compiler will not

    generate a default constructor. Thus

    (i) if the superclass has no default constructor but does possess other constructors, w

    explicitly use one of these constructors viasuper. Otherwise the compiler will rep

    error;

    (ii) if the superclass has no constructor at all and the subclass constructor does not usthe default constructor provided by the compiler will be called.

    Existence of superclasss constructors: When a constructor of asubclass does not invokethe superclasss constructorexplicitly, the Javacompiler

    existence of

    default (no-argument)constructor

    existence of

    constructorsrequiringparameters

  • 8/8/2019 Shit Sheet

    25/36

    Case 1:No Nouses the defaultconstructor provided bythe compiler

    Case 2:NoYes reports an error

    Case 3: Yes No uses the defaultconstructor

    Case 4: Yes Yes uses the defaultconstructor

    Abstract Classes and Abstract Methods

    When one moves up an inheritance hierarchy, the ancestor class may become sogeneralthat

    we think of it more as a framework for other classes than as a class that we want to use. We

    may use the keyword abstract to declare such general classes.Abstract classes cannot be instantiated, meaning that if a class is declared as abstract, no

    instance of that class can be created.

    Abstract Method - in an abstract class, you can define (deferring implementation) only the

    signatures of some of its methods, leaving extended classes to provide specific implemen

    tation of these methods.

    Each method that has not been implemented in the class is specifically marked abstract.

    For added clarity, a class with one or more abstract methods must itself be declared abstract. F

    example,

    abstract class Person {

    public Person(String n){

    name = n;

    }

    public abstract String getDescription();

    public String getName() {return name;

    }

    private String name;

    }

    Abstract classes can have concrete data and methods. Also, a class can even be declared

    as abstract even though it has no abstract methods.

    All non-abstract subclasses of the abstract class must implement all the abstract methods of

    the abstract class (overriding). So abstract methods act as placeholder methods that are

    implemented in the subclasses.

    final classes and methods

  • 8/8/2019 Shit Sheet

    26/36

  • 8/8/2019 Shit Sheet

    27/36

    upstatic fields or other necessary states.

    public static double [] buffer = new double[BUFFER_SIZE];

    static{

    int i;

    for ( i = 0 ; i < buffer.length ; i++ )

    buffer[i] = java.lang.Math.random( );

    }

    Thus a static method cannot use the implicit parameterthis.

    Finalizers

    Every class in Java can have a finalizer method that releases resources back to the operating

    system. This method is called before the garbage collector sweeps away the object. A

    classs finalizer method always has the namefinalize, receiving no parameters and returning n

    value. (I.e., its return type is void.)

    Chapter 4 (Part 1)Fundamental Concepts ofObject-oriented Approach

    Quality Factors:

    Correctness --- Correctness is the ability of software products to exactly perform their tasks,

    as defined by the requirements and specification.

    Robustness --- Robustness is the ability of software systems to function even in abnormal

    conditions.

    Extendibility --- Extendibility is the ease with which software products may be adapted to

    changes of specification.Reusability --- Reusability is the ability of software products to be reused, in whole or in part,

    for new applications.

    O-O Analysis

    Tradition structured analysis focus upon the flow of data within a system; O-O analysis

    emphasizes on the building of real-world models.

    What is an object?

    An object is an entity that contains both the attributes that describe the state of a real-world

    object and the actions that are associated with the real-world object.With an O-O language, an object would capture both the data (attributes) and methods

    ( actions) associated with the object.

    What is a class?

    Object-Oriented programs usually operate with hundreds or thousands of objects. Many object

    common properties ( same structure, same interface, same behavior ) --- they are

    the same kind.

    We define a class of objects to be one that has the same properties to describe objects in

    this class, which messages they should accept, which methods they should execute in

    response to these messages, and the attributes of them.

    Polymorphism

    Polymorphism represents a concept in type theory in which a single name

  • 8/8/2019 Shit Sheet

    28/36

    (e.g., the name of a method) may denote things of many different classes that are

    related by some common superclass.

    The identically named method may have different implementations in the subclasses

    Chapter 3: Basics of Java

    Data Type

    All data types are classes, except several "primitive" types built-in to support integer, floating-

    point, boolean, and character values.

    Classes

    Classes are user-defined data types. (There are lots of libraries classes.) We will study how to

    write user-defined classes in chapter 4.

    Constants

    Java uses the keywordfinalto denote a constant (In C, use the keyword const.) The keyword

    finalindicates that you can assign to the variable only once.

    Enumerated Types

    enum Size {SMALL, MEDIUM, LARGE, EXTRA_LARGE}

    Size pizza = Size.MEDIUM;

    Further discussion in Chapter 5.

    Strings (java.lang.String p.53)

    The standard Java library contains a predefined class String to deal with sequences of character da

    provide shorthand to declare and initialize them.

    labeled break (breaking out of one or nested loops)

    int n;read_data:

    while ( ) { // This loop statement is tagged with the label.

    for ( ) { // This inner loop is not labeled.

    System.out.print(Enter a number >= 0: );

    n = in.nextInt( );

    if ( n < 0 ) // should never happen cant go on

    break read_data;// break out of read_data loop

    }

    }

    // This statement is executed immediately after the break.

    if ( n < 0 ) { // check for bad situation

    // deal with bad situation

    }

    else {

    // carry out normal processing

    }

    // Second example

  • 8/8/2019 Shit Sheet

    29/36

    label:

    {

    if ( condition ) breaklabel; //exits block

    }

    // jumps here when the break statement executes

    MP1

    import java.io.*;

    import java.util.*;

    public class MP1 {

    public static void main(String[] args)throws IOException{

    int i = 0, j = 0;

    int ii, jj;int maxpara = 1000;

    int maxline = 1000;

    String [][] contents = new String[maxpara]

    [maxline];

    if (args[0].substring(0,1).equals("-")){

    if (args[0].substring(0,2).equals("-s")){

    Scanner in = new Scanner(new File(args[1]));

    while(in.hasNextLine()){String temp =

    in.nextLine();

    if(temp.equals("")){

    i++;

    j=0;

    }else{

    contents[i][j] =

    temp;j++;

    }

    }

    itemS(args[0], i, j, maxline,maxpara, contents);

    in.close();

    else{

    Scanner in = new Scanner(new File(args[0]));

    PrintWriter out = new PrintWriter(new

    File(args[1]));

    while(in.hasNextLine()){

  • 8/8/2019 Shit Sheet

    30/36

    String temp = in.nextLine();

    if(temp.equals("")){

    i++;

    j=0;

    }else{

    contents[i][j] = temp;

    j++;

    }

    }

    for(ii = 0;ii

  • 8/8/2019 Shit Sheet

    31/36

    this.name = n;

    this.acct_no = an;

    this.customer_since = cs;

    this.mileage_list = ml;

    }

    public Blue_Card(){}

    public Blue_Card claim_ticket(int km){

    Collections.sort(mileage_list);

    return this;

    }

    class Trip implements Comparable{

    public int compareTo(Trip other){

    return

    date_granted.compareTo(other.date_granted);

    }

    }public class MP2 {

    public static void main(String[] args){

    Scanner in = new Scanner(System.in);

    ArrayList Ada_list = new ArrayList();

    Ada_list.add(new Trip(20000, new Date(2007-

    1900,1-1,5)));

    Ada_list.add(new Trip(15000, new Date(2008-

    1900,8-1,8)));System.out.print("Please enter mileage earned:");

    int me = in.nextInt();

    }

    }

    if(option == 6){

    System.exit( 0 );

    }

    MP3import java.awt.*;

    import java.awt.geom.*;

    import javax.swing.*;

    import java.awt.event.*;

    import java.util.*;

    import java.awt.event.*;

    import javax.swing.*;

    public class MP3 {

    public static void main(String[] args){

    KFrame frame = new KFrame();

  • 8/8/2019 Shit Sheet

    32/36

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

    }

    }

    class KFrame extends JFrame{

    public KFrame(){

    setTitle("MP3");

    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    KPanel panel = new KPanel();

    add(panel);

    }

    public static final int DEFAULT_WIDTH = 315;

    public static final int DEFAULT_HEIGHT = 635;

    }

    class MyMouseAdapter extends MouseAdapter{ public void mousePressed( MouseEvent e){

    int x =e.getX();

    int y = e.getY();

    }

    }

    class MyKeyAdapter extends KeyAdapter

    {

    public void keyPressed(KeyEvent event){ int code = event.getKeyCode();

    char code2 = event.getKeyChar();

    }

    }

    MP4

    public KFrame(){

    setTitle("MP4");

    main_panel = new JPanel();main_panel.setLayout( new BorderLayout() );

    left_panel = new FillPanel();

    left_panel.setPreferredSize(new Dimension(400,

    700));

    main_panel.add(left_panel,BorderLayout.WEST);

    right_panel = new RPanel();

    right_panel.setLayout( new BorderLayout() );

    right_panel.setPreferredSize(new Dimension(300,

    700));

  • 8/8/2019 Shit Sheet

    33/36

    right_top = new RTPanel();

    right_top.setPreferredSize(new Dimension(300,

    250));

    right_panel.add(right_top, BorderLayout.NORTH);

    right_middle = new RMPanel();

    right_middle.setLayout(new BorderLayout());

    right_middle.setPreferredSize(new Dimension(300,

    250));

    JPanel right_middle_bottom = new JPanel();

    right_middle_bottom.setPreferredSize(new

    Dimension(300, 150));

    right_middle_bottom.setLayout(new

    GridLayout(8,1));

    top_scores = new JLabel(" Top Scores");

    name = new JLabel(" Name Scores");

    first = new JLabel("1.");second = new JLabel("2.");

    third = new JLabel("3.");

    fourth = new JLabel("4.");

    fifth = new JLabel("5.");

    JTextField tf = new JTextField (" ", 10);

    right_middle_bottom.add(top_scores);

    right_middle_bottom.add(name);

    right_middle_bottom.add(first);right_middle_bottom.add(second);

    right_middle_bottom.add(third);

    right_middle_bottom.add(fourth);

    right_middle_bottom.add(fifth);

    right_middle_bottom.add(tf);

    right_middle.add(right_middle_bottom,

    BorderLayout.SOUTH);

    right_panel.add(right_middle,BorderLayout.CENTER);

    right_bottom = new RBPanel();

    right_bottom.setPreferredSize(new Dimension(300,

    200));

    slider_panel = new SPanel();

    slider_panel.setLayout( new BorderLayout() );

    slider = new JSlider(1,100,50);

    slider.setPaintTicks(true);

    slider.setMajorTickSpacing(20);

    slider.setMinorTickSpacing(5);

    slider.addChangeListener(new ChangeListener()

  • 8/8/2019 Shit Sheet

    34/36

  • 8/8/2019 Shit Sheet

    35/36

    }

    }

    );

    top_left_down.add(square);

    top_left_down.add(triangle);

    top_left_down.add(circle);

    top_left.setLayout(new GridLayout(2,1));

    top_left.add(label);

    top_left.add(top_left_down);

    ButtonGroup group = new

    ButtonGroup();

    JRadioButton fill = new

    JRadioButton("Filled figue", true);

    JRadioButton outline = newJRadioButton("Outline only",

    false);

    fill.addActionListener(new

    ActionListener()

    {

    public void

    actionPerformed(ActionEvent

    event){fill_out = true;

    repaint();

    }

    }

    );

    top_right.setLayout(new

    GridLayout(2,1));

    group.add(fill);group.add(outline);

    top_right.add(fill);

    top_right.add(outline);

    top_panel.add(top_left,BorderLay

    out.CENTER);

    top_panel.add(top_right,

    BorderLayout.EAST);

    bottom_panel.setLayout(new

    BorderLayout());

    bottom_left = new JPanel();

    bottom_right = new JPanel();

    JLabel label5 = new

    JLabel("Status");

    bottom_left.setLayout(new

    GridLayout(2,1));

    JLabel label1 = new JLabel("Sizeof square, triangle, or circles:");

    JSlider slider = new

    JSlider(5,300,50);

    slider.setPaintTicks(true);

    slider.setMajorTickSpacing(20);

    slider.setMinorTickSpacing(5);

    bottom_left.add(label1);

    bottom_left.add(slider);bottom_right.setLayout(new

    GridLayout(2,3));

    JLabel label2 = new JLabel("Red

    intensity:");

    JLabel label3 = new JLabel("Green

    intensity:");

    JLabel label4 = new JLabel("Blue

    intensity:");JTextField tf = new JTextField("",

    5);

    JTextField tf1 = new JTextField("",

    5);

    JTextField tf2 = new JTextField("",

    5);

    bottom_right.add(label2);

    bottom_right.add(label3);

    bottom_right.add(label4);

    bottom_right.add(tf);

    bottom_right.add(tf1);

  • 8/8/2019 Shit Sheet

    36/36

    bottom_right.add(tf2);

    bottom_panel.add(bottom_left,Bo

    rderLayout.WEST);

    bottom_panel.add(bottom_right,B

    orderLayout.CENTER);

    bottom_panel.add(label5,BorderL

    ayout.SOUTH);

    add(top_panel,BorderLayout.NOR

    TH);

    add(middle_panel,BorderLayout.C

    ENTER);

    add(bottom_panel,BorderLayout.S

    OUTH);

    public void

    paintComponent(Graphics g){

    super.paintComponent(g);Graphics2D g2 = (Graphics2D) g;

    int w = getWidth(), h =

    getHeight();

    int margin = h/2;

    int w2 = w / 2;

    if(figure == 2){

    Polygon poly;

    g2.setPaint( Color.green );

    poly = new Polygon();

    poly.addPoint( w2 - s, h -

    margin );

    poly.addPoint( w2 + s, h -

    margin );

    poly.addPoint( w2, h - margin -

    s );

    active.add(poly);

    if(fill_out){

    g2.fill( poly );

    }else{

    g2.draw( poly );}

    //poly.reset();

    }