CSE 115 1st Edition Exam #2 Study Guide Lectures 10-15Lecture 10 ( 3/2)Variables, Composition, Naming Conventions, Object Behaviors- Local Variableo Declared within the constructor (method)o Scope = point of declaration to the end of method braceo Stored in stack- Instance Variableo Declared in the class body (before the constructor)o Scope = entire class bodyo Stored in heap- Binary Relationships – Compositiono Whole-part relationshipo Source: wholeo Target: part- Naming Conventionso Can have: Capital letters Lowercase Digits (0-9) Underscore First character can be a digito Package names All lowercase (can include underscores between words)o Class names First character: Uppercase Camel case after first word TheFirstExampleo Instance variables First character: Underscore Second character: Lowercase Camel case after first word _thisFirstExamplecse115.WholeWhole()cse115.PartPart()o Local variables First character: Lowercase Camel case after first word thisFirstExample- Object Behaviorso Constructors Called during object creation (during “new”)o Behaviors Called on existing objects (methods)Lecture 11 (3/4)Composition Object Diagram, Association, Accessor and Mutator Methods - Compositiono Source class uses instance variable for target variableo package cse115;public class Dog {private Tail _tail;public Dog () {_tail = new Tail();}}- Binary Relationships – Associationo No lifetime linko Whole takes no responsibility for creating Partcse115.WholeWhole(part)o public class Dog {private Collar _myCollar;public Dog(collar c) {_myCollar = c;o One object can communicate with anothero Objects in the relationship can change over timeo Target (collar) must exist before the Source (Dog) can be created Collar object must be provided in the constructor of the Dog object- Mutator methodso Change the value of an instance variableo public Dog(collar c) {cse115.PartPart()_collar = c;}public void setCollar(collar c) {_collar = c;}}- Constructors vs. Mutatorso Constructors set the value of the instance variable when the class is instantiated o Mutators set the value of the instance variable after the object already existso Constructors initialize all instance variableso Mutatorssets value of just one instance variable- Accessor Methodo Read value of private objectso publicexample1.Terrarium getTerrarium() {return_t;}}Lecture 12 (3/6)Association Object Diagram- Associationo public class Shape {privatejava.awt.Color _color;public Shape(java.awt.Color c) {_color = c;}Shape s1 = new Shape(java.awt.Color.BLUE)}Lecture 13 (3/9/15)s1cShapejava.awt.Color.BLUE_colorContainers, Components, Primitive Types, Layout Manager, java.lang package- Containerso Able to hold graphical objects (containers and objects)- Components o Must be put into containero Able to generate events- Graphical containerso Top level containers Do not need to be place inside any other containers JFrame Multiple paneso Other containers Must be placed in another container JPanel- Create frame, title, make frame visible, make application close when window is closedo package swing;importjavax.swing.JFrame() {privateJFrame _window;publicFirstJFrameExample() {_window = new JFrame(“Title”);_window.setVisible(true);_window.pack();_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);- Primitive typeso Value is not the same as the objecto No instance variableso Methods can’t be called of themo Boolean Operators: && (AND), || (OR), ! (NOT) Declaration and assignment: boolean x = true;o int Integral value within a range Operators: +, -, *, /, %- JFrame (Container)o Special methods setVisible (boolean) setDefaultCloseOperation (int) pack()- Layout managemento Layout manager arranges contents of a container using a layout strategyo FlowLayout (default)o panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));o panel.setLayout(new GridLayout(2,2));- java.lang packageo All elements are imported by defaulto java.lang.String String is a class String objects are immutable “This is a String” String concatenation- ‘+’ is a string concatenation operator, binary operator, infix operatoro “The answer is” + “ 17” = The answer is 17 Static methods- Invoked on a class, not an object- No access to instance variables - Invocation record has no “this”- Main Methodo Standard entry point for a Java programo public static void main (String [] args) {newClassName();} Static variables- Accessed via a class, not an object- Often declared public- All uppercase class name (java.awt.Color.BLUE)- Only exists once in memoryo java.lang.System System.out.println() System class is defined in java.lang package “out” is a public static variable of System class whose type is PrintStream the “PrintStream” class defines static method “println”- JButton (Component)o Typically set up to react to clickso Events Clicks on buttons, mouse movements = events A program can react to events by setting up event handlers Event handlers define what should happen when an event occurs Observer pattern: a component which gives rise to an event is decoupled from the part of code that handles the eventLecture 14 (3/13)Observer Pattern (Event and EventHandlers), Interfaces, Realization- Observer pattern in Javao Observer is called a listenero Button clicks are ActionEventso Handlers for ActionEvents are ActionListenerso Use “addActionListener” method to register a listener with a componento Invariant = event generator (JButton)o Variant = event handler (ActionListenerimplementor)- Interfaces and Realizatono Interfaces An interface is a collection of abstract methods Implemented by a class (public class ClassName implements InterfaceName) Contains behaviors that are implemented by a class Methods within an interface do not return a value (public void methods)o Realization Implementing an interface public class EventHandler1 implements ActionListener {public void actionPerformed(ActionEvent e) {System.out.println(“Something”);}} *SeparateActionListener class must be created and the .addActionListener method must be called on to the object in the Model classLecture 15 3/25Relationship between Model, View, and EventHandler classes- Modelo Create instance variables for the objectsJButton Object(Observable)AssociationEvent Handling Object(Observer)Respond to button clickDraws shape of buttono
View Full Document