James Tam Object-Oriented Principles in Java: Part II Issues associated with objects containing/composed of other objects: Composition, Reuse Issues associated.

Slides:



Advertisements
Similar presentations
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Advertisements

Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
James Tam CPSC 233: Introduction to Classes And Objects, Part II More on Java methods Relations between classes Association Aggregation Multiplicity Issues.
James Tam Intermediate Pascal to Java examples Pascal To Java: The Transition Documentation Variables and constants Advanced input and output Decision.
James Tam CPSC 233: Introduction to Classes and Objects, Part I Attributes and methods Creating new classes References: Dynamic memory allocation and.
James Tam Object-Oriented Design Approaches to object-oriented design Some principles for good design Benefits and Drawbacks of the Object-Oriented Approach.
James Tam Data structures in Java Data Structures In Java In this section of notes you will learn about two common types of data structures: Queues Stacks.
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
James Tam CPSC 233: Introduction to Classes and Objects, Part II More on Java methods Relations between classes Association Aggregation Multiplicity Issues.
James Tam Java Packages Packages, a method of subdividing a Java program and grouping classes.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
James Tam Object-Oriented Design And Software Testing In this section of notes you will learn about principles of good design as well how testing is an.
James Tam Object-Oriented Principles in Java: Part I Encapsulation Information Hiding Implementation Hiding Creating Objects in Java Class attributes.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
Writing Classes (Chapter 4)
James Tam Advanced Java Programming After mastering the basics of Java you will now learn more complex but important programming concepts as implemented.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
SEEM Java – Basic Introduction, Classes and Objects.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Java linked list.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Chapter 9 Introduction to Arrays Fundamentals of Java.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java: Base Types All information has a type or class designation
Arrays.
Java: Base Types All information has a type or class designation
CS2006- Data Structures I Chapter 5 Linked Lists I.
Java Classes and Objects 3rd Lecture
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
Advanced Java Programming
Classes CS 21a: Introduction to Computing I
Data Structures & Algorithms
Advanced Java Programming
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Classes and Objects Object Creation
Chapter 7 Objects and Classes
CMSC 202 Constructors Version 9/10.
Presentation transcript:

James Tam Object-Oriented Principles in Java: Part II Issues associated with objects containing/composed of other objects: Composition, Reuse Issues associated with object references Assignment, comparisons Useful for operations The String class/Displaying object attributes

James Tam Object-Oriented Principles in Java: Part II Composition: Books As I indicated in Section 3.3, the action history (ninth row) includes all of the low-level edits in the workspace. It is obvious that this information is of paramount importance when tracking changes as it may be the first and only piece of information that a person will desire when catching up on changes. What is missing from this display mechanism is information on ‘where’ changes were made (edit history) and ‘how’ things changed (process and outcome history). The main reason for this absence is due to the drawback inherent of text (described previously) – often representing information about these categories of questions involves the representation of spatial information. Chapter 1Chapter 2Chapter 3Chapter 4

James Tam Object-Oriented Principles in Java: Part II Composition: Lists Assume that a list has been implemented as a linked list. The list can be again viewed as a hierarchy: Linked list Node datanext Node datanext Node datanext Node datanext Node datanext

James Tam Object-Oriented Principles in Java: Part II Composition: Lists Alternative representation (containment): Linked list datanext Node datanext Node datanext Node datanext Node

James Tam Object-Oriented Principles in Java: Part II A Simple Linked List Implemented In Java LinkedList -head: Node +LinkedList () +display () Node -data: int -next: Node +Node () +getData () +setData () +getNext () +setNext () Driver

James Tam Object-Oriented Principles in Java: Part II Composition: The Driver Class The following example can be found in the directory: /home/profs/tamj/233/examples/composition class Driver { public static void main (String [] argv) { LinkedList integerList = new LinkedList (); integerList.display(); }

James Tam Object-Oriented Principles in Java: Part II Composition: The Linked List Class class LinkedList { private Node head; public LinkedList () { int i = 1; Node temp; head = null; for (i = 1; i <= 4; i++) { temp = new Node (); temp.setNext(head); head = temp; }

James Tam Object-Oriented Principles in Java: Part II Composition: The Linked List Class (2) public void display () { int i = 1; Node temp = head; while (temp != null) { System.out.println("Element No. " + i + "=" + temp.getData()); temp = temp.getNext(); i++; } // End of class Linked List

James Tam Object-Oriented Principles in Java: Part II Composition: The Node Class import java.util.Random; class Node { private int data; private Node next; Node () { data = (int) (Math.random() * 100); next = null; } public int getData () { return data; }

James Tam Object-Oriented Principles in Java: Part II Composition: The Node Class (2) public void setData (int num) { data = num; } public Node getNext () { return next; } public void setNext (Node nextNode) { next = nextNode; } // End of class Node

James Tam Object-Oriented Principles in Java: Part II Composition And Code Reuse Node -data -next +Node () +getData () +setData () +getNext () +setNext () Class Linked List { Node temp = new Node (); : }

James Tam Object-Oriented Principles in Java: Part II Composition And Code Reuse Node -data -next +Node () +getData () +setData () +getNext () +setNext () Class Linked List { Node temp = new Node (); : } It’s “for free”

James Tam Object-Oriented Principles in Java: Part II Composition: Alternative Names “Whole-part” “Has-A” “Includes” / “Part-of”

James Tam Object-Oriented Principles in Java: Part II Issues Associated With Object References Assignment Comparisons

James Tam Object-Oriented Principles in Java: Part II Issues Associated With Object References Assignment Comparisons

James Tam Object-Oriented Principles in Java: Part II Reference: Can’t Be De-referenced By Programmer Node temp;temp = new Node (); datanext

James Tam Object-Oriented Principles in Java: Part II Assignment Operator: Works On The Reference Node n1 = new Node (); Node n2 = new Node (); n2 = n1; n2.setData(888);

James Tam Object-Oriented Principles in Java: Part II Copying Data Between References Perform a field-by-field copy Clone the object

James Tam Object-Oriented Principles in Java: Part II Field-By-Field Copy class IntegerWrapper { private int num; public void setNum (int no) { num = no; } public int getNum () { return num; }

James Tam Object-Oriented Principles in Java: Part II Comparisons: Comparing The References Node n1 = new Node (); Node n2 = new Node (); if (n1 == n2) System.out.println("Same node"); else System.out.println("Two different nodes"); n2 = n1; if (n1 == n2) System.out.println("Same node"); else System.out.println("Two different nodes");

James Tam Object-Oriented Principles in Java: Part II Comparing Data For References Use equals () Node n1 = new Node (); Node n2 = new Node (); if (n1.equals(n2)) System.out.println("Equal data"); else System.out.println("Data not equal");

James Tam Object-Oriented Principles in Java: Part II Passing By Reference For Simple Types It can be done in Java Just use a wrapper!

James Tam Object-Oriented Principles in Java: Part II Passing By Reference For Simple Types (2) class IntegerWrapper { private int num; public int getNum () { return num; } public void setNum (int no) { num = no; } } class Driver { public static void method (IntegerWrapper temp) { temp.setNum(10); } public static void main (String [] argv) { IntegerWrapper temp = new IntegerWrapper (); temp.setNum(1); method(temp); }

James Tam Object-Oriented Principles in Java: Part II The String Class Revisited A Java class but the attribute fields can be displayed directly (via print/println) A String is created like a simple type (when double quotes are encountered). Any of the simple types will be converted to a string when passed to the print/println method. String concatenation: Plus sign: e.g., System.out.println(“Str1” + “Str2”); Method: e.g., public String concat (String str) For more detailed information regarding the String class goto the url:

James Tam Object-Oriented Principles in Java: Part II A Simple Book Collection Example DriverCollectionManager - bookCollection: Book + CollectionManager () + getBook () Book -length: int -title: String -authors: String [] -publisher: String //Numerous accessors The full example can be found in the directory: / home/profs/tamj/233/examples/displayingClasses/fieldByField

James Tam Object-Oriented Principles in Java: Part II The Driver Class import tio.*; class Driver { public static void main (String [] argv) { int i, j; Book tempBook; CollectionManager tamjCollection = new CollectionManager (); System.out.println("\nJAMES' BOOK COLLECTION"); for (i = 0; i < 80; i++) System.out.print("-"); System.out.println();

James Tam Object-Oriented Principles in Java: Part II The Driver Class (2) for (i = 0; i < CollectionManager.NOBOOKS; i++) { System.out.println("\tBook: " + (i+1)); tempBook = tamjCollection.getBook(i); System.out.println("\tTitle..." + tempBook.getTitle()); System.out.println("\tLength..." + tempBook.getLength() + " pages"); System.out.print("\tAuthors: "); for (j = 0; j < CollectionManager.NOAUTHORS; j++) System.out.print(tempBook.getAuthorAt(j) + " "); System.out.println(); System.out.println("\tPublisher..." + tempBook.getPublisher()); for (j = 0; j < 80; j++) System.out.print("~"); System.out.println("Hit return to continue"); Console.in.readChar(); }

James Tam Object-Oriented Principles in Java: Part II The Collection Manager Class class CollectionManager { private Book [] bookCollection; public static final int NOBOOKS = 4; public static final int NOAUTHORS = 3; public CollectionManager () { int i; bookCollection = new Book[NOBOOKS]; for (i = 0; i < NOBOOKS; i++) { bookCollection[i] = new Book (); }

James Tam Object-Oriented Principles in Java: Part II The Collection Manager Class (2) public Book getBook (int index) { return bookCollection[index]; }

James Tam Object-Oriented Principles in Java: Part II Portions Of The Book Class class Book { private int length; private String title; private String [] authors; private String publisher; private static int seriesNumber = 1; private static final int NOAUTHORS = 3;

James Tam Object-Oriented Principles in Java: Part II Portions Of The Book Class (2) public Book () { int i; length = (int) (Math.random() * 900) + 100; title = "How to series, Number " + Book.seriesNumber; Book.seriesNumber++; authors = new String[NOAUTHORS]; for (i = 0; i < NOAUTHORS; i++) { authors[i] = "Author-" + (i+1); } publisher = "Book publisher"; } // Numerous accessor (get/set) methods. // A second constructor }

James Tam Object-Oriented Principles in Java: Part II A Revised Version Of The Book Example DriverCollectionManager - bookCollection: Book + CollectionManager () + displayCollection () Book -length: int -title: String -authors: String [] -publisher: String +toString () //Numerous accessors The full example can be found in the directory: / home/profs/tamj/233/examples/displayingClasses/toString

James Tam Object-Oriented Principles in Java: Part II The Driver Class class Driver { public static void main (String [] argv) { int i; Book tempBook; CollectionManager tamjCollection = new CollectionManager (); System.out.println("\nJAMES' BOOK COLLECTION"); for (i = 0; i < 80; i++) System.out.print("-"); System.out.println(); tamjCollection.displayCollection(); }

James Tam Object-Oriented Principles in Java: Part II The CollectionManager Class import tio.*; class CollectionManager { private Book [] bookCollection; public static final int NOBOOKS = 4; public CollectionManager () { int i; bookCollection = new Book[NOBOOKS]; for (i = 0; i < NOBOOKS; i++) { bookCollection[i] = new Book (); }

James Tam Object-Oriented Principles in Java: Part II The Collection Manager Class (2) public void displayCollection () { int i; for (i = 0; i < NOBOOKS; i++) { System.out.println(bookCollection[i]); System.out.println("Hit return to continue"); Console.in.readChar(); }

James Tam Object-Oriented Principles in Java: Part II The Book Class class Book { private int length; private String title; private String [] authors; private String publisher; private static final int NOAUTHORS = 3; private static int seriesNumber = 1;

James Tam Object-Oriented Principles in Java: Part II The Book Class (2) public String toString () { String allFields = new String (); int i; allFields = allFields.concat("\tBook Title..." + title + "\n"); allFields = allFields.concat("\tLength..." + length + " pages\n"); allFields = allFields.concat("\tAuthors: "); for (i = 0; i < authors.length; i++) { allFields = allFields.concat(authors[i] + " "); } allFields = allFields.concat("\n"); allFields = allFields.concat("\t" + publisher + "\n"); for (i = 0; i < 80; i++) allFields = allFields.concat("~"); allFields = allFields.concat("\n"); return allFields; }

James Tam Object-Oriented Principles in Java: Part II The Book Class (2) public String toString () { String allFields = new String (); int i; allFields = allFields.concat("\tBook Title..." + title + "\n"); allFields = allFields.concat("\tLength..." + length + " pages\n"); allFields = allFields.concat("\tAuthors: "); for (i = 0; i < authors.length; i++) { allFields = allFields.concat(authors[i] + " "); } allFields = allFields.concat("\n"); allFields = allFields.concat("\t" + publisher + "\n"); for (i = 0; i < 80; i++) allFields = allFields.concat("~"); allFields = allFields.concat("\n"); return allFields; } Automatically called when an instance of the class is passed as parameter to print/println

James Tam Object-Oriented Principles in Java: Part II Summary You should now know: What composition means in terms of Object-Oriented theory and how to implement it in Java. How assignment and comparisons work with Java objects (references) Another example of why implementation hiding is a useful principle with the creation of the toString () method.