Download presentation
Presentation is loading. Please wait.
Published byMarlene Skinner Modified over 8 years ago
1
Designing Classes Lab
2
The object that you brought to class Put it in the basket we will exchange them now
3
Designing a Class on your own Think – What is the responsibility of this class? (name it appropriately) – What functionality or behavior should it provide? (methods) – What state do the objects need to carry out their responsibilities? (attributes or fields or properties)
4
Example: Designing a BankAccount Class Think – What is the responsibility of this class? To define a bank account entity for purposes of managing bank accounts in a system. – What functionality or behavior should it provide? (methods) What does the bank account do? – What state do the objects need to carry out their responsibilities? (attributes) What does it need to know about itself? Bank Account: Every Bank Account must have a name (typically the owner), a unique account number, and a balance. It should provide methods to: deposit or withdraw money query the current balance query the account name print a neat one line summary of owner, account number, balance
5
Responsibility of a BankAccount Class Every Bank Account should provide methods to: deposit or withdraw money query the current balance query the account name print a neat one line summary of owner, account number, balance
6
Functionality a BankAccount Class Behavior: deposit( amount ) : add money to the account balance debit( amount ) : deduct money from the balance getName( ) : return the account name (String) getBalance( ) : return the current balance (double)
7
State of a BankAccount Class Looking at the required behavior, what attributes [or fields or properties] does a Bank Account object need? balance : the current balance. We'll use type "double". name : a String containing the account name. accountID : the account ID. String or int or long? (Note: Account numbers are always 10-digits.) Behavior: deposit( amount ) : add money to the account balance debit( amount ) : deduct money from the balance getName( ) : return the account name (String) getBalance( ) : return the current balance (double)
8
Writing it all down: Class Diagram for a BankAccount Class BankAccount name accountID balance getBalance( ) deposit( double amount ) debit( double amount ) getName( ) A UML class diagram The methods = what it DOES The Class Name The attributes or state = what it KNOWS
9
In class exercise part 1 Look at your assigned object and identify – The responsibility – The functionality – And the attributes that it needs to carry out the functionality Write them down informally [like slide 4]
10
In class exercise part 2 Now make a class diagram from your thoughts in part 1 [like slide 8] We will present and discuss as many of these as possible in class.
11
Homework Your Turn: Do another class diagram at home. Take both of your class diagrams to the tutor [Spencer] in the lab [CLCC 385] to get his feedback and return your object to the basket. He will give you something [I’m not telling you what it is because you have to visit him to get it] and you will need to bring it to class next lab class. [Tuesdays are lecture; Thursdays are lab class] I will return your objects to you during the Thursday lab class next week. Spencer’s hours are posted on Blackboard in the announcements.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.