1 / 18

Problem Solve

Problem Solve. Suppose a librarian needs to keep track of all the books in a library. How would we write a program to solve this problem?. Class. Problem Suppose a librarian needs to keep track of all the books in a library. What would be the class for this project?. Class. Problem

aerick
Download Presentation

Problem Solve

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solve Suppose a librarian needs to keep track of all the books in a library. How would we write a program to solve this problem?

  2. Class Problem Suppose a librarian needs to keep track of all the books in a library. What would be the class for this project?

  3. Class Problem Suppose a librarian needs to keep track of all the books in a library. What would be the class for this project? Library

  4. Class Problem Suppose a librarian needs to keep track of all the books in a library. What would be the class for this project? Library How would I declare this Library class?

  5. Class Problem Suppose a librarian needs to keep track of all the books in a library. What would be the class for this project? Library How would I declare this Library class? public class Library

  6. Data Problem Suppose a librarian needs to keep track of all the books in a library. What data or information about the Library do we have?

  7. Data Problem Suppose a librarian needs to keep track of all the books in a library. What data or information about the Library do we have? librarian name collection of books

  8. Data Storage How do we store this data in our class? librarian name collection of books

  9. Data Storage How do we store this data in our class? librarian name String collection of books ArrayList of String

  10. Data Storage How is it defined in the class? librarian name String private String librarian; collection of books ArrayList of String private ArrayList<String> books;

  11. Code import java.util.ArrayList; public class Library { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … }

  12. Class Diagram import java.util.ArrayList; public classLibrary { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … }

  13. Class Diagram import java.util.ArrayList; public classLibrary { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … } Library

  14. Object Diagram Now suppose there is a specific library named myLibrary with a librarian named Mrs. Jones. We would execute the following to create it: myLibrary = new Library(“Mrs. Jones”); What does newreally do? 1) allocates memory for a new object instance 2) also executes the constructor for the class 3) returns the memory address of new object

  15. myLibrary = new Library(“Mrs. Jones”);1) allocates memory for a new object instance import java.util.ArrayList; public class Library { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … } myLibrary: Library new librarian books null null

  16. myLibrary = new Library(“Mrs. Jones”);2) also executes the constructor for the class import java.util.ArrayList; public class Library { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … } librarianName myLibrary: Library :String = librarian books “Mrs. Jones” new :ArrayList<String>

  17. Adding ArrayList Items myLibrary: Library :String “Mrs. Jones” • Now suppose we added 2 book names to our booksfield using external calls to the ArrayList .add method: • books.add(“Wuthering Heights”); • books.add(“Little Women”); librarian books :ArrayList<String> 0 1 .add :String .add “Wuthering Heights” :String “Little Women”

  18. Object Diagram • Complete object diagram contains: • 1 instance of a Libraryclassobject named myLibrary • 1 String object with value “Mrs. Jones” that fieldlibrarian points to • 1 ArrayListobject with 2 String items that fieldbooks points to • 2 String objects with values “Wuthering Heights” & “Little Women ” that index 0 & 1 of the ArrayList points to myLibrary: Library :String “Mrs. Jones” librarian books :ArrayList<String> 0 1 :String “Wuthering Heights” :String “Little Women”

More Related