Understanding Object-Oriented Design in Software Development

Slide Note
Embed
Share

Learn about the concept of object-oriented design in software development, including the importance of design, the structure of objects, building objects, and what a class typically looks like. Discover how design facilitates building scalable systems and fostering collaboration within development teams.


Uploaded on Aug 03, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Classes and Objects

  2. What is Design? The parts of the software including what information each part holds what things each part can do how the various parts interact

  3. Why do We Care About Design? As system grows larger helps us understand the software we are about to build helps teams develop components that will work together helps us find where we need to work to add new features fix defects

  4. Object Oriented Design Design of the system is structured around the objects in a system Objects are the things the system manages or uses to accomplish its goals Object == Instance

  5. Objects An object is made of two types of things: What it knows stored in instance variables What it does coded as methods

  6. How do we Build Objects? A class is template for objects of a particular type So a class must contain: instance variables variables that are not inside a method each instance of the class gets its own copy of those instance variables to that it can hold the information it needs methods encoding the operations the objects must be able to do

  7. The Over Used Word Class We use the word class to mean many things: the java code that is a template for building objects of a particular type a non-primitive type that we are defining the set of objects of the same type

  8. What a Class Looks Like public class Account { private double balance; Instance Variables * Deposit a certain amount into the account * @param depositAmount the amount to deposit */ public void deposit(double depositAmount) { balance = balance + depositAmount; } /** Method to deposit money into the account /** * Get the current balance of the account * @return the current balance */ public double getBalance() { return balance; } Method to see how much money is in the account }

  9. Creating and Using Objects Show this in Eclipse: public class Runner { public static void main(String[] args) { Account savings; Account ira; savings = new Account(); savings.deposit(32.13); ira = new Account(); ira.deposit(2324.23); ira.deposit(333.22); } } System.out.println("Savings balance: " + savings.getBalance()); System.out.println("IRA balance: " + ira.getBalance());

  10. Classes in Our Lab We want to build the start of a card playing game (up through shuffling the cards) The objects we need: 52 card objects 1 deck object

  11. Card Class A card needs to know two things: its face value and its suit we ll store both as ints face value: 0 = Ace, 1 = 2, . . . 9 = 10, 10 = Jack, 11 = Queen, 12 = King suit: 0 = spades, 1 = hearts, 2 = diamonds, 3 = clubs A card needs three operations one to retrieve its face value one to retrieve its suit one to build a description of this card Find these things in the code in Eclipse

  12. Creating a Card It doesn t make sense to have a card with no face value or suit If we don t initialize them, primitive instance variables default to zero, so every card would start out as the Ace of Spades We d like a way to initialize the card when we create it

  13. Constructors Special methods that are used only for the creation of objects We know a method is a constructor if it has no return value AND its name matches the name of the class

  14. Our Constructor and Its Use /** * Create a new card with a given suit and value * * @param v the face value of the card (0 - 12) * @param s the suit of the card (0 - 3) */ public Card(int v, int s) { faceValue = v; suit = s; } Card c2; c2 = new Card(12,3); What card will this create?

  15. CardRunner Whats that main method? Look at CardRunner class method with this declaration: public static void main(String[] args) This is the method that the Java Virtual Machine runs when you run your program We ve seen this method in all of our previous labs we just didn t explain what it is!

  16. Types of Classes We now have two types of classes Runnable: has a main() method and can be run by the Java Virtual Machine Instantiable: is a template for creating objects Has a constructor if we don t declare a constructor, the compiler will give us the default (no parameters) constructor that allocates the space, but doesn t initialize the instance variables they default to zeros for the primitive types we ve studies and nulls (no pointer) for reference types)

  17. toString() We often convert objects to Strings for example, when we concatenate them into output statements Java gives every class a method to do this provides default conversion <class name> @<heap address> Card@E0352 We can make our own to replace the default must be declared as: public String toString()

  18. Converting our int to a Suit We d like the description of a card to have Spades instead of 0 Make an array that encodes that conversion: SUIT_DESCRIPTION: String Spades String Hearts String String Clubs Diamonds

  19. In the Code private static final String[] SUIT_DESCRIPTION = { "Spades", "Hearts", "Diamonds", "Clubs" }; What will this evaluate to if suit has a value of 0? If suit has a value of 3? SUIT_DESCRIPTION[suit]

  20. The Deck Class Instance of deck need to know: what cards they have and what order they are in an array of type Card Instance of deck need to do: output the cards in order (toString()) shuffle

  21. Deck Constructor /** * Create the deck by filling it with the appropriate cards */ public Deck() { cards = new Card[NUMBER_OF_CARDS]; int position = 0; for (int suit = 0; suit < NUMBER_OF_SUITS; suit++) { for (int faceValue = 0; faceValue < NUMBER_OF_CARDS/NUMBER_OF_SUITS; faceValue++) { cards[position] = new Card(faceValue, suit); position++; } } }

Related