Download presentation
Presentation is loading. Please wait.
Published byRenáta Svobodová Modified over 5 years ago
1
Classes and Objects B.Ramamurthy 5/9/2019 B.Ramamurthy
2
Topics for Discussion Problem to Classes
ScoreBoard class Designing and implementing classes Methods Data Using classes in an Application 5/9/2019 B.Ramamurthy
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. 5/9/2019 B.Ramamurthy
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/9/2019 B.Ramamurthy
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) 5/9/2019 B.Ramamurthy
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 5/9/2019 B.Ramamurthy
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(){ }; 5/9/2019 B.Ramamurthy
8
ScoreBoard class (contd.)
//5. Predicate methods :return true/false boolean shutDown() { }; } Implement the contents of the methods. 5/9/2019 B.Ramamurthy
9
Method Definition and Call
Specifies: 1) Name of the function 2) The actual parameters Definition Defines: 1) Header* 2) Formal parameter names and types 3) Statements to be executed 4) Return statement(s) 5/9/2019 B.Ramamurthy
10
Method Definition : Header
body modifiers parameter list return type method name { statements } 5/9/2019 B.Ramamurthy
11
Parameters and Return Value
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} 5/9/2019 B.Ramamurthy
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 5/9/2019 B.Ramamurthy
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) 5/9/2019 B.Ramamurthy
14
Application class superBowl{ //main method …//inside main method
//1. create two object reference for the two teams scoreBoard Denver, Atlanta; 5/9/2019 B.Ramamurthy
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 } 5/9/2019 B.Ramamurthy
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(); } 5/9/2019 B.Ramamurthy
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 5/9/2019 B.Ramamurthy
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. 5/9/2019 B.Ramamurthy
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.