Presentation is loading. Please wait.

Presentation is loading. Please wait.

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?

Similar presentations


Presentation on theme: "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?"— 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 How do we store this data in our class?
Data Storage How do we store this data in our class? librarian name collection of books

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

10 private ArrayList<String> books;
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 class Library { 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 class Library { private String librarian; private ArrayList<String> books; public Library(String librarianName) librarian = librarianName; books = new ArrayList<String>(); } methods omitted … Library

14 myLibrary = new Library(“Mrs. Jones”);
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 new really do? 1) it creates a new class instance of the object 2) it also executes the constructor for the class

15 myLibrary = new Library(“Mrs
myLibrary = new Library(“Mrs. Jones”); 1) it creates a new class instance of the object 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

16 myLibrary = new Library(“Mrs
myLibrary = new Library(“Mrs. Jones”); 2) it 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 books field using external calls to the ArrayList .add method: books.add(“Wuthering Heights”); books.add(“Little Women”); librarian books :ArrayList<String> .add :String .add “Wuthering Heights” :String “Little Women”

18 Object Diagram Complete object diagram contains:
1 instance of a Library class object named myLibrary 1 String object with value “Mrs. Jones” that field librarian points to 1 ArrayList object with 2 String items that field books 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> :String “Wuthering Heights” :String “Little Women”


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

Similar presentations


Ads by Google