Classes and Objects - PowerPoint PPT Presentation

About This Presentation
Title:

Classes and Objects

Description:

Example Application: Simulate Super Bowl (Jan 1999) 6/7/09. B.Ramamurthy. 14. Application. class superBowl{ //main method ...//inside main method ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 19
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:
Tags: bowl | classes | objects | super

less

Transcript and Presenter's Notes

Title: Classes and Objects


1
Classes and Objects
  • B.Ramamurthy

2
Topics for Discussion
  • Problem to Classes
  • ScoreBoard class
  • Designing and implementing classes
  • Methods
  • Data
  • Using classes in an Application

3
Problem Statement to Classes
  • Underline the nouns in a problem statement.
  • Using the problem context and general knowledge
    about the problem domain decide on the important
    nouns.
  • Design and implement classes to represent the
    nouns.

4
Examples
  • Drawing package Design a user interface for
    drawing various shapes circle, square,
    rectangle.
  • Football scores Keep track of football score.
  • General purpose counter To keep of track of
    count for various applications.
  • Library Books, different categories of books,
    details of student borrower, library personnel.

5
Designing Classes
  • A class represents a class of objects.
  • A class contains the data declarations (parts)
    and methods (behaviors or capabilities ).
  • OO Design
  • Data Declarations are answers to What is it made
    of? (It has a ____, ____, etc.)
  • Methods are answers to What can it do? (verbs
    in the problem)

6
ScoreBoard class
  • class scoreBoard
  • //1. data
  • int Score
  • String Name // team name
  • //2. Constructors to create objects
  • ScoreBoard() //default constructor
  • ScoreBoard(String teamName) // initializing
    constructor

7
ScoreBoard class (contd.)
  • //3. Interface Methods to perform operation
  • void touchDown()
  • void extraPoint()
  • void twoPointConv()
  • void fieldGoal()
  • void safety()
  • //4. Utility methods to get and set
  • void setName(string teamName)
  • int getScore()

8
ScoreBoard class (contd.)
  • //5. Predicate methods return true/false
  • boolean shutDown()
  • Implement the contents of the methods.

9
Method Definition and Call
Method
10
Method Definition Header
definition
Header
body
modifiers
parameter list
return type
method name
statements
11
Parameters and Return Value
P2
P1
P3
aMethod
Assume P1, P2 and P3 are integers and aMethod
returns P3 which is the sum of P1 and P2. Then
definition of aMethod is int aMethod(int P1,
int P2) int P3 P3 P1 P2 return P3
12
Items Accessible to a Method
  • All the local data
  • All the parameters
  • All the data declared in the enclosing class
  • All the methods in the enclosing class

13
Using Classes
  • An application creates objects it requires by
    instantiating them from classes.
  • The statements of the application use the
    accessing the public data and by invoking the
    public methods of the objects.
  • Example Application Simulate Super Bowl (Jan
    1999)

14
Application
  • class superBowl
  • //main method
  • //inside main method
  • //1. create two object reference for the two
    teams
  • scoreBoard Denver, Atlanta

15
Application (contd.)
  • // 2. instantiate objects two ways
  • Denver new scoreBoard(Broncos)
  • Atlanta new ScoreBoard()
  • Atlanta.setName(Falcons)
  • //3. Use them dot notation
  • Devnver.touchDown()
  • Denver.extraPoint()
  • Denver.display()
  • displayWinner( Denver, Atlanta)// is a method of
    the application

16
Application (contd.)
  • void displayWinner(scoreBoard team1, scoreBoard
    team2)
  • System.out.println(The winner is)
  • if (team1.getScore() team2.getScore())
  • team1.display()
  • else
  • team2.display()

17
Creating the Executable
  • Enter ScoreBoard class in a file ScoreBoard.java
  • javac ScoreBoard.java
  • Enter SuperBowl application in a file
    SuperBowl.java
  • javac SuperBowl.java
  • Execute application by
  • java SuperBowl

18
Summary
  • We discussed complete development cycle using
    java classes.
  • Discussed (instance) methods and implementation
    of methods.
  • Using classes for instantiating objects, and dot
    notation for accessing items in a class were
    illustrated.
Write a Comment
User Comments (0)
About PowerShow.com