Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.

Similar presentations


Presentation on theme: "Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria."— Presentation transcript:

1 Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

2 4-2 Objectives: l Get an introduction to classes, objects, fields, constructors, and methods; get a general idea of how a small program is put together l Explore how library classes are used in Java programs l Get a feel for how methods call each other; learn about private and public methods l Learn a little about event-driven applications and the event-handling mechanism in Java

3 4-3 Objects in the Ramblecs Applet Ramblecs, the applet itself LetterPanel whiteboard FallingCube cube JButton go

4 4-4 Classes and Source Files l A class defines a class of objects. Class name:File name: SomeClass Ramblecs FallingCube SomeClass.java Ramblecs.java FallingCube.java Convention: a class name starts with a capital letter Same upper / lower case letters

5 4-5 Programmers write classes l And extensively use library classes –either directly: JButton go = new JButton("Click here"); –or through inheritance: public class LetterPanel extends JPanel

6 4-6 Classes in the Ramblecs Applet Written by us From the library package javax.swing

7 4-7 Files and Folders javac automatically looks for classes (.java or.class files) in the current folder, or, if classpath is set, in folders listed in the classpath string. l A missing file may be reported as a syntax error when compiling another file. If you set classpath, include the current folder. It is denoted by a dot. For example:.; C:\javamethods\EasyIO l IDE helps take care of the file locations.

8 4-8 Libraries l Java programs are usually not written from scratch. l There are hundreds of library classes for all occasions. l Library classes are organized into packages. For example: java.util — miscellaneous utility classes java.awt — windowing and graphics toolkit javax.swing — newer GUI package Swing

9 4-9 import l Full library class names include the package name. For example: java.awt.Color javax.swing.JButton import statements at the top of your program let you refer to library classes by their short names: import javax.swing. JButton ;... JButton go = new JButton ("Click here"); Fully-qualified name

10 4-10 import (cont’d) You can import names for all the classes in a package by using a wildcard. * : import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; java.lang is imported automatically into all classes; defines System, Math, Object, String, and other commonly used classes. Imports all classes from awt, awt.event, and swing packages

11 4-11 public class SomeClass { l Fields l Constructors l Methods } private: visible only inside this class public: visible in other classes Attributes / variables that define the object’s state. Can hold numbers, characters, strings, other objects. Usually private. Code for constructing a new object and initializing its fields. Usually public. Actions that an object can take. Can be public or private.

12 4-12 public class FallingCube { private final int cubeSize; private int cubeX, cubeY; // Cube coordinates... private char randomLetter; // Cube letter public FallingCube(int size) { cubeSize = size;... } public void start() { cubeX = 0; cubeY = -cubeSize;... }... } Fields Constructor Methods The name of a constructor is always the same as the name of the class.

13 4-13 private (or public) [static] [final] datatype name ; Fields Usually private May be present: means the field is shared by all objects in the class May be present: means the field is a constant int, double, etc., or an object: String, JButton, FallingCube, Timer You name it!

14 4-14 Fields (cont’d) l May have primitive data types: int, char, double, etc. private int cubeX, cubeY; // cube coordinates... private char randomLetter; // cube letter

15 4-15 Fields (cont’d) l May be objects of different types: private FallingCube cube ; private Timer t ; private static final String letters ;

16 4-16 Constructors l Constructors are like methods for creating objects of a class. l Most constructors initialize the object’s fields. l Constructors may take parameters. l A class may have several constructors that differ in the number or types of their parameters. l All of a class’s constructors have the same name as the class.

17 4-17 Constructors (cont’d) go = new JButton("Go");

18 4-18 Constructors (cont’d) l Call them using the new operator: cube = new FallingCube (CUBESIZE);... t = new Timer (delay, this) Calls FallingCube ’s constructor with CUBESIZE as the parameter Calls Timer ’s constructor with delay and this (i.e. this object) as the parameters (see Java docs for javax.swing.Timer )

19 4-19 Methods l Call them for a particular object: cube.start(); whiteboard.dropCube(); randomLetter = letters.charAt(i); l But call static (“class”) methods for the whole class, not a specific object: y = Math.sqrt (x);

20 4-20 Methods (cont’d) l Constructors and methods can call other public and private methods of the same class. l Constructors and methods can call only public methods of another class. Class X private method Class Y public method

21 4-21 Methods (cont’d) l You can call methods with specific arguments: g.drawRect (75, 25, 150, 50); g.drawString ("Welcome", 120, 50); l The number and types of arguments must match the method’s parameters: public void drawRect ( int x, int y, int width, int height ) {...} public void drawString ( String msg, int x, int y ) {...}

22 4-22 Events l Can originate in the real world (mouse clicked, keyboard key pressed, cable gets connected, etc.) l Can come from the operating system (window resized or closed, e-mail message received, etc.) l Can originate in your program (a timer fires, a panel needs to be repainted, etc.)

23 4-23 Events (cont’d) l An object that generates events may have one or several listeners attached to it. l A listener is an object. l A listener’s method is called for each event. ActionListener object (ActionEvent e) { whiteboard.dropCube(); } public void actionPerformed Click!

24 4-24 Events (cont’d) public class Ramblecs extends JApplet implements ActionListener {... private JButton go; public void init() { go = new JButton("Go"); go.addActionListener(this);... } public void actionPerformed(AcionEvent e) { whiteboard.dropCube(); } Add a listener to the button. In this case, the listener object is the applet itself. Describes the details of this event. Not used here.

25 4-25 Ramblecs Events Ramblecs class applet object creates the whiteboard panel and the “Go” button calls whiteboard’s dropCube Applet starts init method actionPerformed method  “Go” clicked

26 4-26 Ramblecs Events (cont’d) LetterPanel class whiteboard object starts the timer and the cube moves the cube down; generates a repaint request restores the background; calls cube’s draw method Repaint request dropCube method actionPerformed method  Timer fires paintComponent method

27 4-27 Ramblecs Events (cont’d) FallingCube class cube object picks a random letter; resets cube’s position to the top checks whether cube reached the bottom; moves the cube down draws the cube start method moveDown method draw method

28 4-28 Review: l How many classes did we write for Ramblecs? l Name a few library classes that we used. l What are import statements used for? l What is a field? A constructor? A method? l Which operator is used to construct an object? l What is the difference between private and public methods? l Why are fields usually private?

29 4-29 Review (cont’d): l Define an event-driven application. l Why are GUI applications event-driven? l Is an event listener a class, an object, or a method? l How many action listeners are used in Ramblecs? l What does the following statement do? w.addWindowListener (new ExitButtonListener ( ) );


Download ppt "Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria."

Similar presentations


Ads by Google