Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 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; Library public class Library { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … } 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 myLibrary = new Library(“Mrs. Jones”); 1) it creates a new class instance of the object import java.util.ArrayList; new public class Library { private String librarian; private ArrayList<String> books; public Library(String librarianName) { librarian = librarianName; books = new ArrayList<String>(); } methods omitted … } myLibrary: Library librarian null books null 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 … } myLibrary: Library librarian librarianName = books new :ArrayList<String> :String “Mrs. Jones” 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”); librarian books :ArrayList<String> 0 :String 1 .add .add “Wuthering Heights” • books.add(“Little Women”); :String “Little Women” Object Diagram Complete object diagram contains: • 1 instance of a Library class object named myLibrary myLibrary: Library “Mrs. Jones” librarian books :ArrayList<String> 0 • 1 String object with value “Mrs. Jones” that field librarian points to • 1 ArrayList object with 2 String items that field books points to :String 1 :String “Wuthering Heights” • 2 String objects with values :String “Wuthering Heights” & “Little Women ” that “Little Women” index 0 & 1 of the ArrayList points to