180 likes | 201 Views
Learn how to design and implement classes in Java for various applications like drawing package, football scores, and libraries. Understand methods, data declarations, and access control in object-oriented design.
E N D
Classes and Objects B.Ramamurthy B.Ramamurthy
Topics for Discussion • Problem to Classes • ScoreBoard class • Designing and implementing classes • Methods • Data • Using classes in an Application B.Ramamurthy
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. B.Ramamurthy
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. B.Ramamurthy
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) B.Ramamurthy
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 B.Ramamurthy
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(){ }; B.Ramamurthy
ScoreBoard class (contd.) //5. Predicate methods :return true/false boolean shutDown() { }; } Implement the contents of the methods. B.Ramamurthy
Call Definition Defines: 1) Header* 2) Formal parameter names and types 3) Statements to be executed 4) Return statement(s) Specifies: 1) Name of the function 2) The actual parameters Method Definition and Call Method B.Ramamurthy
Method Definition : Header definition Header body modifiers parameter list return type method name { statements } B.Ramamurthy
Parameters and Return Value P2 P3 P1 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} B.Ramamurthy
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 B.Ramamurthy
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) B.Ramamurthy
Application class superBowl{ //main method …//inside main method //1. create two object reference for the two teams scoreBoard Denver, Atlanta; B.Ramamurthy
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 } B.Ramamurthy
Application (contd.) void displayWinner(scoreBoard team1, scoreBoard team2) { System.out.println(“The winner is”); if (team1.getScore() > team2.getScore()) team1.display(); else team2.display(); } B.Ramamurthy
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 B.Ramamurthy
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. B.Ramamurthy