Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the.

Similar presentations


Presentation on theme: "Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the."— Presentation transcript:

1 Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the students who are officially registered in this course Will not transfer all weights for assignments and midterm to the final –You can only miss one assignment or the midterm –Need to provide supporting documents Deferred final –Within one week following the missed final, send documents to the main office of Information Technology (TEL3068) –http://www.registrar.yorku.ca/exams/deferred/

2 Building a Class Review last lecture –A class describes a set of objects with the same behavior –An object is an instance of a class –Define a class Attributes Methods –Getters –Setters Today –Design multi-class applications –Use Universal Modeling Language (UML) to describe the multi-class applications –Arrays of Objects

3 Building Multi-Class Applications Why multi-class? –Complicated systems –Not able to describe in one single class Identify classes –Which classes to build? –Which attributes and methods to implement for these classes? –What are the relationships between classes?

4 OOD – Identifying Classes (1) Class –Identifying the nouns (and noun phrases) in the application description Attributes –The attributes correspond to descriptive words What characteristics distinguish this class from others? What uniquely identifies this object from another object of the same class? How is this object in this class associated with objects in other classes? Methods –The behavior (verbs) corresponds to public methods contained in their corresponding classes Methods fulfill the responsibilities of a class

5 OOD – Identifying Classes (2) Occasionally classes are not build to generate objects but to collect static methods and constants. –Utility classes, e.g., Math, Color, Integer Create classes which represent a single concept Example: –Simulate purses and the coins within each purse.

6 class Purse { public Purse(); public void addNickels(int count); public void addDimes(int count); public void addQuarters(int count); public double getTotal(); } class Coin{ public Coin(double value, String aName); public double getValue(); } class Purse { public Purse(); public void add(Coin aCoin); public double getTotal(); } Two concepts: 1. a purse that holds coins, and 2. the value of each coin. Class Purse uses/depends on class Coin

7 UML Universal Modeling Language (UML) –a standard language for specifying, visualizing, constructing, and documenting the artifacts of software systems, as well as for business modeling and other non-software systems. Class diagram –a type of static structure diagram that describes the structure of a system –class name, attributes, methods –relationship between classes

8 Coin UML class diagram A class diagram is a rectangle. Class name in the top A line separates the class name from the rest of the diagram. class Coin{ } UML Class Diagrams (1)

9 Following the class name we write the attributes of the class. A line separates the attributes from the rest of the diagram. Coin - coinValue: int Scope (visibility) + public - private Attribute name data type class Coin{ private int coinValue; } UML Class Diagrams (2)

10 Coin - coinValue: int Scope (visibility) + public - private Method name Return type Following the attributes, we write the methods. It is not necessary to show all attributes and methods. parameters class Coin{ private int coinValue; public Coin(double value, String aName); public double getValue(); } + Coin(double value, String aName) + getValue(): double UML Class Diagrams (3)

11 Relationships Between Classes The most common types of relationships: –Dependency –Aggregation –Inheritance

12 Dependency A class depends on another class if it uses objects of that class. The “knows about” relationship. Example: Purse depends on Coin class Coin{ public Coin(double value, String aName); public double getValue(); } class Purse { public Purse(); public void add(Coin aCoin); public double getTotal(); }

13 Dependency It is a good practice to minimize the coupling (i.e., dependency) between classes. When a class changes, coupled classes may also need updating.

14 Aggregation A class aggregates another if its objects contain objects of the other class. –Has-a relationship Example: a Quiz class aggregates a Question class. Aggregation is a stronger form of dependency.

15 Aggregation Use an instance variable public class Quiz { private ArrayList questions;... }

16 UML for Multiple Classes Using arrows to connect classes –Can represent relationships between the classes Dependency (Dotted line, open arrow tip) Aggregation (Solid line, diamond arrow tip) –Can represent relationship between instances of classes (multiplicities) Specific number (e.g. 1) Range of numbers (e.g. 1..5) Unbounded (e.g. 1..*) Any (*) –More types of relationships and symbols –Flexible in use

17 Case Study: Invoices Write a set of classes to support the creation and printing of invoices Classes: –Invoice –Product –LineItem –Address

18 public class Product { public Product(String aDescription, double aPrice) { description = aDescription; price = aPrice; } public String getDescription() { return description; } public double getPrice() { return price; } private String description; private double price; } public class LineItem{ public LineItem(Product aProduct, int aQuantity) { theProduct = aProduct; quantity = aQuantity; } public double getTotalPrice() { return theProduct.getPrice() * quantity; } public String format() { return String.format("%-30s%8.2f%5d%8.2f", theProduct.getDescription(), theProduct.getPrice(), quantity, getTotalPrice()); } private int quantity; private Product theProduct; } Product and LineItem classes

19 public class Address{ public Address(String aName, String aStreet, String aCity, String aState, String aZip) { name = aName; street = aStreet; city = aCity; state = aState; zip = aZip; } public String format() { return name + "\n" + street + "\n" + city + ", " + state + " " + zip; } private String name; private String street; private String city; private String state; private String zip; } Address class

20 public class Invoice{ public Invoice(Address anAddress) { items = new ArrayList (); billingAddress = anAddress; } public void add(Product aProduct, int quantity) { LineItem anItem = new LineItem(aProduct, quantity); items.add(anItem); } public String format() { String r = " I N V O I C E\n\n" + billingAddress.format() + String.format("\n\n%-30s%8s%5s%8s\n", "Description", "Price", "Qty", "Total"); for (LineItem i : items) { r = r + i.format() + "\n"; } r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue()); return r; } public double getAmountDue() { double amountDue = 0; for (LineItem i : items) { amountDue = amountDue + i.getTotalPrice(); } return amountDue; } private Address billingAddress; private ArrayList items; } Invoice class

21 public class InvoiceTester{ public static void main(String[] args) { Address samsAddress = new Address("Sam's Small Appliances", "100 Main Street", "Anytown", "CA", "98765"); Invoice samsInvoice = new Invoice(samsAddress); samsInvoice.add(new Product("Toaster", 29.95), 3); samsInvoice.add(new Product("Hair dryer", 24.95), 1); samsInvoice.add(new Product("Car vacuum", 19.99), 2); System.out.println(samsInvoice.format()); } InvoiceTester

22 Design your projects Hints: –Work from a requirements specification –Good idea to keep a list of candidate classes. –Cross not useful ones later. –Concepts from the problem domain are good candidates for classes. –Not all classes can be discovered from the program requirements After you have a set of classes –Define the behavior (methods) of each class Look for verbs in the task description –Match the verbs to the appropriate objects

23 A Case Study An Application Centre for university admissions A student can choose 3 different universities, and let’s assume that this center will accept up to 100 different applications. This program has to allow the user to: –enter the student applications, –accept students in some of the universities that they chose, –display the status of the applications.

24 Class Candidates Based on Nouns Application Centre University Admissions Student Universities Applications Status of Applications Two classes: ApplicationCentre and StudentApplication ✓ ✗ ✓ ✓ ✓ ✗ Too simple to constitute separate classes Should be a property

25 UML Class Diagram Application Centre - st[100]: StudentApplication - name : String + addStudent(): boolean + getStudent(int): StudentApplication StudentApplication - name: String - univ1, univ2, univ3: String - acc1, acc2, acc3: boolean + setAcceptance(): void + toString(): String * 1

26 1. class StudentApplication{ 2.private String name; 3.private String university0; 4.private String university1; 5.private String university2; 6.private boolean accept0; 7.private boolean accept1; 8.private boolean accept2; 9.public StudentApplication (String n, String u0, String u1, String u2){ 10.name = n; 11.university0=u0; 12.university1=u1; 13.university2=u2; 14.accept0=accept1=accept2=false; 15.} 16.public void setAcceptance(int which, boolean decision){ 17.switch(which){ 18.case 0: accept0=decision; break; 19.case 1: accept1=decision; break; 20.case 2: accept2=decision; break; 21.} 22.} 23.public String toString(){ 24.String result = name + ":\n"; 25. result += university0; 26.if (accept0) result += " - accepted\n"; 27.else result += " - rejected\n"; 28. result += university1; 29.if (accept1) result += " - accepted\n"; 30.else result += " - rejected\n"; 31. result += university2; 32.if (accept2) result += " - accepted\n"; 33.else result += " - rejected\n"; 34. return result; 35.} 36. }

27 1.class ApplicationCentre { 2. private String name; 3. private StudentApplication[] st; 4. private int studentCount; 5. private int size; 6. 7. public ApplicationCentre(String s){ 8. name=s; 9. size=100; 10. st = new StudentApplication[size]; 11. studentCount=0; 12. } 13. public String getName() { 14. return name; 15. } 16. public boolean addStudent(StudentApplication s){ 17. if (studentCount==size) return false; 18. st[studentCount]=s; 19. studentCount ++; 20. return true; 21. } 22. public StudentApplication getStudent(int which){ 23. if ( which studentCount-1){ 24. return null; 25. } 26. return st[which]; 27. } 28.}

28 Arrays Related data items of same type Group of contiguous memory locations Each memory location has same name Each memory location has same type

29 A 12-element array -45 6 0 72 1543 -89 0 62 -3 1 6453 78 c[ 1 ] c[ 2 ] c[ 4 ] c[ 3 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] c[ 11 ] c[ 0 ] Name of array (Note that all elements of this array have the same name, c ) Position number (index of subscript) of the element within array c

30 Declaring and Allocating Arrays Declaring and allocating arrays –Arrays are objects that occupy memory –Allocated dynamically with operator new int c[] = new int[ 12 ]; –Equivalent to int c[]; // declare array c = new int[ 12 ]; // allocate array We can allocate arrays of objects too String b[] = new String[ 100 ]; Obtaining the size of an array –c.length

31 Initializing Arrays Initialize array elements –Use initializer list Items enclosed in braces ( {} ) Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; –Creates a five-element array –Subscripts of 0, 1, 2, 3, 4 –Do not need operator new

32 Arrays of Objects Allocating memory for an array does not automatically allocate the memory for each element! –Only references to the elements are created –The objects need to be allocated separately Example Account accounts[] = new Account[12]; accounts[0].getBalance(); // wrong, Account[0] is not initialized Account accounts[] = new Account[12]; for (int i = 0; i < accounts.length; i++) accounts[i] = new Account(); // initializing the array elements accounts[0].getBalance(); // correct

33 Reading Textbook Ch12


Download ppt "Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the."

Similar presentations


Ads by Google