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

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Objects First With Java A Practical Introduction Using BlueJ Improving structure with inheritance 2.0.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Lab 10: Bank Account II Transaction List, error checking & aggregation.
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.
Copyright by Scott GrissomCh 5 Sophisticated Behavior Slide 1 Java Class Library a very large collection of classes for you to use as needed the challenge.
Inheritance Chapter 8.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Grouping Objects 1 Introduction to Collections.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul
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
Grouping objects Arrays, Collections and Iterators 1.0.
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.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
OBJECT INTERACTION CITS1001. Overview Coupling and Cohesion Internal/external method calls null objects Chaining method calls Class constants Class variables.
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.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
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.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 8: Understanding Collections Textbook: Chapter 4.
Objects First with Java CITS1001 week 4
Objects First with Java Introduction to collections
CS2006- Data Structures I Chapter 5 Linked Lists I.
C ++ Pointer code.
University of Central Florida COP 3330 Object Oriented Programming
ARRAYLIST AND VECTOR.
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?
Interfaces and Inheritance
Still Aggregation and Composition
Research impact and library support
Arrays of Objects.
Dynamic Data Structures and Generics
JAVA CLASSES.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Improving structure with inheritance
Lecture 15 Inheritance.
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

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 books;

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 … }

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 … }

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

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; 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

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 =

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

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”