Download presentation
Presentation is loading. Please wait.
Published byAntony Davidson Modified over 9 years ago
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 books;
11
Code import java.util.ArrayList; public class Library { private String librarian; private ArrayList books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList (); } methods omitted … }
12
Class Diagram import java.util.ArrayList; public class Library { private String librarian; private ArrayList books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList (); } methods omitted … }
13
Class Diagram import java.util.ArrayList; public class Library { private String librarian; private ArrayList books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList (); } 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 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. Jones”); 1) it creates a new class instance of the object import java.util.ArrayList; public class Library { private String librarian; private ArrayList books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList (); } methods omitted … } myLibrary: Library librarian books null new
16
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 books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList (); } methods omitted … } myLibrary: Library librarian books :ArrayList :String “Mrs. Jones” librarianName new =
17
Adding ArrayList Items myLibrary: Library librarian books :ArrayList :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”); 0 1 :String “Little Women” :String “Wuthering Heights”.add
18
Object Diagram myLibrary: Library librarian books :ArrayList :String “Mrs. Jones” 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 0 1 :String “Little Women” :String “Wuthering Heights”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.