ECE 242 Spring 2003 Data Structures in Java - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

ECE 242 Spring 2003 Data Structures in Java

Description:

Attribute. name. telephone number. Behavior. print information (name ... Attribute: name, address, checking interest and saving interest. ECE242 Spring 2003 ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 29
Provided by: jxia
Category:

less

Transcript and Presenter's Notes

Title: ECE 242 Spring 2003 Data Structures in Java


1
ECE 242 Spring 2003Data Structures in Java
  • http//rio.ecs.umass.edu/ece242
  • Object-Oriented Programming (1)
  • Prof. Lixin Gao

2
Todays Topics
  • Object Oriented Design
  • Class
  • Instance variables
  • Methods
  • Constructor

3
Cartoon
4
Object Oriented Design
  • Describe real world as well as virtual world
  • How?
  • Procedural-Oriented Programming
  • A sequence of actions
  • Object-Oriented Programming
  • Describe objects

5
Object Oriented Design
World
Class
Class
Class attributes behavior
Object
Object
Object
Class
  • Class a generic description for a set of objects.

6
Car
  • Attribute
  • id
  • model
  • year build
  • color
  • Behavior
  • start the engine
  • stop the engine
  • speed up
  • change gear

7
Employee
  • Attribute
  • name
  • telephone
  • Behavior
  • print information
  • change telephone

8
MP3 file
  • Attribute
  • title
  • artist
  • Behavior
  • start playing song
  • stop playing song

9
Movie
  • Attribute
  • title
  • cinema
  • rating
  • star
  • votes for Oscar
  • Behavior
  • nominate for Oscar

10
Bank
  • Attribute
  • bank name
  • bank address
  • checking interest
  • saving interest
  • Behavior
  • change address
  • change checking interest
  • change saving interest
  • print information

11
Object
  • Attribute
  • Behavior
  • Instance variable
  • Method/Function

12
Class Definition
  • class
  • instance variables
  • methods
  • constructor

13
Organization of Class
  • class OurClass
  • int firstMethod()
  • int secondMethod(float f)
  • OurClass() // constructor
  • //end of class OurClass

14
Employee
  • Create a class for Employee
  • Attribute
  • name
  • telephone number
  • Behavior
  • print information (name telephone)

15
Employee Class
  • class Employee
  • String name
  • String tel
  • public void printinfo()
  • System.out.println(name"'s number is
    "tel)

Attribute name telephone
Behavior printinfo()
16
MP3 Song
  • Create a class for MP3 Song
  • Attribute
  • title
  • artist
  • Behavior
  • start playing song
  • stop playing song

17
MP3 Class
  • class MP3
  • String title
  • String artist
  • public void startPlaying()
  • // Call Windows MediaPlayer to start
    playing song
  • public void stopPlaying()
  • // Call Windows MediaPlayer to stop playing
    song

Attribute title, artist
Behavior start/stop song
18
Bank
  • Create a class for Bank
  • Attribute
  • name
  • address
  • checking Interest
  • saving Interest
  • Behavior
  • change address
  • change checking interest
  • change saving interest
  • print information

19
Bank Class --- Attribute
  • class Bank
  • String name
  • String address
  • double checkingInterest
  • double savingInterest

Attribute name, address, checking interest and
saving interest
20
Bank Class --- Behavior
  • void changeAddress(String newaddress)
  • address newaddress
  • void changeCheckingInterest(double val)
  • checkingInterest val
  • void changeSavingInterest(double val)
  • savingInterest val

21
Bank Class --- Behavior
  • void printinfo()
  • System.out.println(bank name is name )
  • System.out.println(bank address is
    address)
  • System.out.println(checking interest is
    checkingInterest)
  • System.out.println(saving interest is
    savingInterest)

22
Constructor
  • Special method
  • Name is the same as class
  • Initialize instance variable

23
Employee Class with Constructor
  • class Employee
  • String name
  • String tel
  • public void printinfo()
  • System.out.println(name"'s number is
    "tel)
  • Employee( String n, String t )
  • name n
  • tel t

Constructor
24
Create Object
  • new Create a new object of a class
  • Employee gaonew Employee("Gao", "4548")
  • gao.printinfo()
  • Method and instance variables
  • gao is an object
  • printinfo() is a method belong to object gao

25
Executable
  • Make java file executable
  • main method
  • Syntax
  • public static void main(String args)

26
Executable Employee Class
  • Make class executable (Employee.java)
  • public static void main(String args)
  • //where program gets executed
  • Employee gaonew Employee("Gao", "4548")
  • gao.printinfo()

27
MP3 Class with Constructor
  • class MP3
  • String title
  • String artist
  • MP3( String t, String a)
  • title t
  • artist a

Constructor
28
Create MP3 Object
  • create MP3 object
  • MP3 bye new MP3("Bye Bye Bye", "NSync")
  • MP3 brand new MP3(Brand New Day", Sting")
  • Behaviors for these objects
  • bye.startPlaying()
  • bye.stopPlaying()
  • brand.startPlaying()
Write a Comment
User Comments (0)
About PowerShow.com