review introduction to computer science i
play

Review Introduction to Computer Science I CSE 1020 moodle.yorku.ca - PowerPoint PPT Presentation

Review Introduction to Computer Science I CSE 1020 moodle.yorku.ca CSE 1020 moodle.yorku.ca Aggregation Combine simple data into more complex data. String Date String Date CSE 1020 moodle.yorku.ca Aggregation Definition Aggregation is


  1. Review Introduction to Computer Science I CSE 1020 moodle.yorku.ca CSE 1020 moodle.yorku.ca

  2. Aggregation Combine simple data into more complex data. String Date String Date CSE 1020 moodle.yorku.ca

  3. Aggregation Definition Aggregation is a binary relation on classes. The pair ( A , P ) of classes is in the aggregation relation if class A (aggregate) has an attribute of type P (part). The aggregation relation is also known as the has-a relation. Instead of saying that ( A , P ) is in the aggregation relation, we often simply say that A has-a P . Example Stock has-a String . Investment has-a Stock . CSE 1020 moodle.yorku.ca

  4. UML Diagrams 1 1 Investment Stock String CSE 1020 moodle.yorku.ca

  5. How to Copy an Object? We will show three ways to copy an object: create an alias, create a shallow copy, and create a deep copy. The created copies are fundamentally different. CSE 1020 moodle.yorku.ca

  6. Alias Investment investment = Investment.getRandom(); Investment alias = investment; CSE 1020 moodle.yorku.ca

  7. Alias 100 main invocation investment 400 400 alias 200 String object ”HR.Z” 300 Stock object symbol 200 400 Investment object 300 stock quantity 8 25.50 bookValue CSE 1020 moodle.yorku.ca

  8. Shallow Copy Investment investment = Investment.getRandom(); Stock stock = investment.getStock(); int quantity = investment.getQty(); double bookValue = investment.getBookValue(); Investment shallowCopy = new Investment(stock, quantity, bookValue); CSE 1020 moodle.yorku.ca

  9. Shallow Copy 100 main invocation investment 400 stock 300 quantity 8 bookValue 25.50 shallowCopy 500 200 String object ”HR.Z” 300 Stock object symbol 200 400 Investment object 300 stock 8 quantity 25.50 bookValue 500 Investment object 300 stock 8 quantity 25.50 bookValue CSE 1020 moodle.yorku.ca

  10. Deep Copy Investment investment = Investment.getRandom(); Stock stock = investment.getStock(); String symbol = stock.getSymbol(); int quantity = investment.getQty(); double bookValue = investment.getBookValue(); Stock stockCopy = new Stock(symbol); Investment deepCopy = new Investment(stockCopy, quantity, bookValue); CSE 1020 moodle.yorku.ca

  11. Deep Copy 100 main invocation 400 investment deepCopy 500 500 Investment object 600 stock quantity 8 25.50 bookValue 600 Stock object 200 symbol CSE 1020 moodle.yorku.ca

  12. Composition Composition is a special type of aggregation. The aggregate A and its part P form a composition if “ A owns P ”, that is, each object of type A has exclusive access to its attribute of type P . The designer and the implementer of a class determine whether an aggregation is a composition. Java does not provide any special language constructs for implementing compositions. The constructors, accessors and mutators are implemented in a particular way (the details will be covered in CSE1030). CSE 1020 moodle.yorku.ca

  13. UML Diagrams 2 String CreditCard Date 2 CSE 1020 moodle.yorku.ca

  14. Accessor CreditCard card = new CreditCard(123456, "Jane Doe"); Date expiryDate = card.getExpiryDate(); CSE 1020 moodle.yorku.ca

  15. Accessor 100 main invocation card 200 expiryDate 700 200 CreditCard object number 300 name 400 issueDate 500 expiryDate 600 300 String object "123456" 400 String object "Jane Doe" 500 Date object now 600 Date object two year from now 700 Date object two years from now CSE 1020 moodle.yorku.ca

  16. Mutator CreditCard card = new CreditCard(123456, "Jane Doe"); Date expiryDate = card.getExpiryDate(); final int YEAR = 113; expiryDate.setYear(YEAR); // set year to 1900 + YEAR CSE 1020 moodle.yorku.ca

  17. Mutator 100 main invocation 200 card 700 expiryDate 113 YEAR 200 CreditCard object 300 number 400 name issueDate 500 expiryDate 600 300 String object "123456" 400 String object "Jane Doe" 500 Date object now 600 Date object two years from now 700 Date object one year ago CSE 1020 moodle.yorku.ca

  18. Collection We distinguish between static allocation: the maximum number of elements (capacity) is fixed when the collection is created dynamic allocation: the number of elements is unbounded and list: duplicates are allowed and the elements are ordered set: duplicates are disallowed and the elements are not ordered CSE 1020 moodle.yorku.ca

  19. Traversals for each element of the collection ... We distinguish two types of traversals: indexed traversals Iterator-based traversals CSE 1020 moodle.yorku.ca

  20. Indexed Traversals ... collection = ... ... for (int i = 0; i < collection.size(); i++) { ... element = collection.get(i); ... } CSE 1020 moodle.yorku.ca

  21. Iterator-Based Traversals CollectionOfTs iterator() : Iterator < T > Iterator < E > hasNext() : boolean next() : E CSE 1020 moodle.yorku.ca

  22. Iterator-Based Traversals ... collection = ... ... Iterator<...> iterator = collection.iterator(); while (iterator.hasNext()) { ... element = iterator.next(); ... } CSE 1020 moodle.yorku.ca

  23. Iterator-Based Traversals ... collection = ... ... Iterator<...> iterator = collection.iterator(); while (iterator.hasNext()) { ... element = iterator.next(); ... } The above can be abbreviated using the advanced for loop: ... collection = ... ... for (... element : collection) { ... } CSE 1020 moodle.yorku.ca

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend