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?

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

M180: Data Structures & Algorithms in Java
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
Improving structure with inheritance. 25/11/2004Lecture 7: Inheritance2 Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables.
Inheritance Chapter 8.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Grouping Objects 1 Introduction to Collections.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
ArrayList, Multidimensional Arrays
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Grouping objects Introduction to collections 5.0.
1 CSC 221: Computer Programming I Fall 2004 Lists, data access, and searching  ArrayList class  ArrayList methods: add, get, size, remove  example:
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Comp1004: Inheritance I Super and Sub-classes. Coming up Inheritance and Code Duplication – Super and sub-classes – Inheritance hierarchies Inheritance.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
CSE 1201 Object Oriented Programming ArrayList 1.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Grouping objects Introduction to collections 5.0.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
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?
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Chapter 8: Understanding Collections Textbook: Chapter 4.
CMSC 202 ArrayList Aug 9, 2007.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
9 Improving structure with inheritance
Sixth Lecture ArrayList Abstract Class and Interface
(like an array on steroids)
Objects First with Java Introduction to collections
CS2006- Data Structures I Chapter 5 Linked Lists I.
C ++ Pointer code.
CSC 222: Object-Oriented Programming Fall 2017
University of Central Florida COP 3330 Object Oriented Programming
COMP 103 Testing with JUnit 2016-T2 extras
ARRAYLIST AND VECTOR.
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Java Array Lists 2/2/2016.
Still Aggregation and Composition
Research impact and library support
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Arrays of Objects.
CMSC 202 ArrayList Aug 9, 2007.
COS 260 DAY 18 Tony Gauvin.
Dynamic Data Structures and Generics
CMSC 202 ArrayList Aug 9, 2007.
Arrays and Array Lists CS 21a.
Java Array Lists 2/13/2017.
Dynamic Data Structures and Generics
Go to pollev.com/cse143.
JAVA CLASSES.
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Improving structure with inheritance
Lecture 15 Inheritance.
Arrays and ArrayLists.
Presentation transcript:

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

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

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

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;

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

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

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

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>

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> 0 1 .add :String .add “Wuthering Heights” :String “Little Women”

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> 0 1 :String “Wuthering Heights” :String “Little Women”