210 likes | 300 Vues
Classes and Objects. 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. Why do We Care About Design?. As system grows larger helps us understand the software we are about to build
E N D
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
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
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
Objects • An object is made of two types of things: • What it knows • stored in “instance variables” • What it does • coded as methods
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
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
What a Class Looks Like public class Account { private double balance; /** * Deposit a certain amount into the account * @paramdepositAmount the amount to deposit */ public void deposit(doubledepositAmount) { balance = balance + depositAmount; } /** * Get the current balance of the account * @return the current balance */ public double getBalance() { return balance; } } Instance Variables Method to deposit money into the account Method to see how much money is in the account
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()); } }
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
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
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
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
Our Constructor and Its Use /** * Create a new card with a given suit and value * * @paramvthe face value of the card (0 - 12) * @paramsthe suit of the card (0 - 3) */ public Card(intv, ints) { faceValue = v; suit = s; } Card c2; c2 = new Card(12,3); What card will this create?
CardRunner – What’s 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!
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)
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()
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 String String String “Spades” “Hearts” “Diamonds” “Clubs”
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]
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
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 (intfaceValue = 0; faceValue < NUMBER_OF_CARDS/NUMBER_OF_SUITS; faceValue++) { cards[position] = new Card(faceValue, suit); position++; } } }