180 likes | 205 Views
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
E N D
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 Suppose a librarian needs to keep track of all the books in a library. What would be the class for this project? Library
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?
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
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?
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
Data Storage How do we store this data in our class? librarian name collection of books
Data Storage How do we store this data in our class? librarian name String collection of books ArrayList of String
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;
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 … }
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 … }
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
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
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
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>
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”
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”