Programming Principles using Java - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Programming Principles using Java

Description:

We create objects from classes and then use the objects by sending ... Applets do not have a main() method as Java applications do. Web pages and APPLET tags ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 21
Provided by: michae332
Category:

less

Transcript and Presenter's Notes

Title: Programming Principles using Java


1
Programming Principles using Java
2
Object Oriented Programs
  • Object Oriented Programs consist of classes and
    objects
  • We create objects from classes and then use the
    objects by sending messages to them

3
Defining the Dog class
  • What attributes do all dogs have in common?
  • What behaviours do all dogs have in common?

4
Class Diagram
Class name
Attribute name
Operations
5
Dog class in Java
  • class Dog
  • int head1, tail1, legs4//dog attributes
  • //dog behaviours
  • void run()
  • System.out.println(I am running)
  • void sit()
  • System.out.println(I am sitting)
  • void bite()
  • System.out.println(I am biting)

6
What is a Class ?
  • A class is a
  • Template for objects
  • Pattern for objects
  • Blueprint for objects
  • Factory for objects
  • Description for objects
  • Classes make objects in their own image
  • Classes are abstract, objects are concrete

7
Creating Objects from Classes
  • Dog snoopy new Dog()
  • Dog lassie new Dog()
  • Dog scooby new Dog()
  • Then we can make the objects do something by
    sending messages to them
  • snoopy.sit()
  • lassie.run()
  • scooby.bite()

8
Classes
  • Classes facilitate code reuse
  • Dog snoopy, lassie, scoobyetc
  • BankAccount jim_ac, tim_ac, jane_ac..etc
  • Classes facilitate code libraries
  • java.awt, java.net, java.io, etc

9
Inheritance reusing classes
10
Inheritance
  • Inheritance is a way of reusing classes
  • reusing classes saves writing code
  • Inheritance takes the attributes and methods of a
    class and adds to them

11
Inheritance
  • Inheritance implements an IS-A relationship
  • Where ANIMAL is the superclass and
  • DOG is a subclass
  • A DOG IS-A ANIMAL
  • So a dog inherits all the
  • features of an animal

12
Inheritance
Aeroplane
wings engines
take-off() fly() land()
PassengerJet
Fighter
passengers attendants
guns bombs
serve()
fire() drop()
13
Inheritance uses extends
  • public class PassengerJet extends Aeroplane
  • //attributes
  • private int passengers, attendants
  • //method
  • public void serve()
  • System.out.println( attendants serve passengers
    meals)

14
World Wide Web
  • Internet - a network of networks
  • World Wide Web - built on top of the Internet
  • contains web pages, held on web servers, which
    can be downloaded onto web clients - web browsers
  • web pages are written in HTML

15
Applets
  • Applets are Java programs that run in a web page
    in a browser
  • all Applets are subclasses of the Applet class
  • Applets do not have a main() method as Java
    applications do

16
Web pages and APPLET tags
  • ltHTMLgt
  • ltBODYgt
  • ltAPPLET CODELighting.class
  • HEIGHT300 WIDTH400gt lt/APPLETgt
  • lt/BODYgt
  • lt/HTMLgt

17
Applet methods
  • init() is used for initialiation, it is called
    only once, when the applet is first run
  • start() is run each time the web page is
    loaded
  • stop() is run when the browser leaves the web
    page containing the applet
  • destroy() used to relinquish any system
    resources it has allocated
  • You may need to override these inherited methods

18
  • import java.awt.
  • import java.awt.event. import
    java.applet.
  •  
  • public class Lighting extends Applet implements
    ActionListener
  • private Button lightswitch
  • private Label display
  • private boolean state false
  •  
  • public void init()
  •  
  • lightswitch new Button(On/Off) //
    creates a Button object
  • display new Label(OFF)
  • lightswitch.addActionListener(this)
  • //register the applet to receive and
    handle ActionEvent events from the button
  •  
  • add(lightswitch) //add the button to the
    applet
  • add(display)

19
private Button lightswitch private Label
display private boolean state false   public
void init()   lightswitch new
Button(On/Off) // creates a Button object
display new Label(OFF)
lightswitch.addActionListener(this)
//register the applet to receive and handle
ActionEvent events from the button  
add(lightswitch) //add the button to the applet
add(display)
20
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com