Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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; /** * 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

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 – well 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 doesnt make sense to have a card with no face value or suit If we dont initialize them, primitive instance variables default to zero, so every card would start out as the Ace of Spades Wed 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 Weve seen this method in all of our previous labs – we just didnt 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 dont declare a constructor, the compiler will give us the default (no parameters) constructor that allocates the space, but doesnt initialize the instance variables – they default to zeros for the primitive types weve 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 @ 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 Wed 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

19 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?

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++; }


Download ppt "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."

Similar presentations


Ads by Google