classes and objects. what is design? the parts of the software including – what information each...
TRANSCRIPT
![Page 1: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/1.jpg)
Classes and Objects
![Page 2: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/5.jpg)
Objects
• An object is made of two types of things:– What it knows• stored in “instance variables”
– What it does• coded as methods
![Page 6: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/8.jpg)
What a Class Looks Likepublic class Account{ private double balance;
/** * Deposit a certain amount into the account * @param depositAmount the amount to deposit */ public void deposit(double depositAmount)
{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
![Page 9: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/9.jpg)
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());}
}
![Page 10: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/10.jpg)
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
![Page 11: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/11.jpg)
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
![Page 12: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/14.jpg)
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?
![Page 15: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/15.jpg)
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!
![Page 16: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/16.jpg)
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)
![Page 17: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/17.jpg)
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()
![Page 18: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/18.jpg)
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“Diamonds”
String“Spades”
String“Clubs”
String“Hearts”
![Page 19: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/19.jpg)
In the Code
private static final String[] SUIT_DESCRIPTION ={ "Spades", "Hearts", "Diamonds", "Clubs" };
SUIT_DESCRIPTION[suit]What will this evaluate to if suit has a value of 0? If suit has a value of 3?
![Page 20: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/20.jpg)
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
![Page 21: 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](https://reader035.vdocuments.us/reader035/viewer/2022062620/5519c0645503467a578b4b59/html5/thumbnails/21.jpg)
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++;
} }
}