1 / 18

Classes and Objects

Classes and Objects. B.Ramamurthy. Topics for Discussion. Problem to Classes ScoreBoard class Designing and implementing classes Methods Data Using classes in an Application. Problem Statement to Classes. Underline the nouns in a problem statement.

Télécharger la présentation

Classes and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes and Objects B.Ramamurthy B.Ramamurthy

  2. Topics for Discussion • Problem to Classes • ScoreBoard class • Designing and implementing classes • Methods • Data • Using classes in an Application 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. 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. 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) 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 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(){ }; B.Ramamurthy

  8. ScoreBoard class (contd.) //5. Predicate methods :return true/false boolean shutDown() { }; } Implement the contents of the methods. B.Ramamurthy

  9. 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

  10. Method Definition : Header definition Header body modifiers parameter list return type method name { statements } B.Ramamurthy

  11. 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

  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 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) B.Ramamurthy

  14. Application class superBowl{ //main method …//inside main method //1. create two object reference for the two teams scoreBoard Denver, Atlanta; 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 } 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(); } 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 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. B.Ramamurthy

More Related