Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu 1

2 Agenda Announcements – Cell phones / laptops off & away / Name signs out Last time –interfaces and realization –type hierarchy –introduction to graphics and event handling Today –main method –import directive –java.lang package –System.out.println –Observer pattern for event handling

3 Announcements Exam 2 –covers material from exam 1 up to & including 10/19 –review on Monday 10/22 –exam on Wednesday 10/24 No recitations in exam week: 10/22 – 10/26

4 main method public static void main(String[] args) { … } main method is standard entry point for a Java program main method is invoked by Java runtime system static –reserved word –indicates member is associated with CLASS not INSTANCE parameter of main –square brackets are special syntax used with arrays we will discuss arrays at start of CSE116 –the parameter ‘args’ is initialized with ‘command line arguments’ – arguments given on the command line when the program is run

5 static members: methods and variables static methods –a static method is invoked on a class, rather than an object –a static method has no access to instance variables –its invocation record has no ‘this’ static variables –a static variable is accessed via a class, rather than an object –a static variable is often declared ‘public’ –a static variable is often given a name of all upper- case letters, as in java.awt.Color.RED

6 import directive an import directive is used to allow unqualified use of a name which would otherwise need to be fully qualified form: import ; examples: import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; These examples allow the names GridLayout, JButton, JFrame and Jlabel to be used without full qualification. This improves both the writability and readability of code.

7 java.lang package the java.lang package is special in that its elements are all imported by default

8 System.out.println The System class is defined in the java.lang package. The name ‘System’ can therefore be used in an unqualified way, even without an explicit import directive. ‘out’ is a public and static variable of the System class, whose type is PrintStream The ‘PrintStream’ class defines a static method named ‘println’ ‘println’ accepts an argument of any type, and prints a textual representation of the argument on the console (see the output in the console view if running in Eclipse, or the terminal window from which the program was started if running outside of Eclipse).

9 String concatenation ‘+’ is the name of the String concatenation operator ‘+’ is a binary operator, meaning it takes two arguments (also called operands) ‘+’ is an infix operator, meaning it is written between its two arguments “Hi” + “there” is an expression whose value is “Hithere”. “The answer is ”+17 is an expression whose value is “The answer is 17”. The int expression 17 is converted to a textual equivalent, the String “17” (which consists of the two characters ‘1’ and ‘7’)

10 Observer pattern for event handling Invariant – Variant decomposition –good software design practice Invariant –the part of the system that does not change from one usage scenario to the next –for event-handling, this is the event-generating component (e.g. JButton) –a JButton has the same basic functionality no matter how it is being used visual representation the ability to register mouse-clicks over top of it the ability to respond to mouse-clicks in some way (as defined by the (variant) event handler) Variant part: the event-handling code –the part of the system that can change from one usage scenario to the next –for event-handling, this is the event handler itself (e.g. the ActionListener implementor) –the ActionListener interface introduces a point of flexibility into the software: different implementations of the interface can exist all implementations MUST adhere to the ‘contract’ that the interface defines (i.e. all event handlers for a JButton must define the actionPerformed method, as specified in the ActionLIstener interface.


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1."

Similar presentations


Ads by Google