Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
Chapter 7 Designing Classes Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling  To.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
Unified Modeling Language
Object-Oriented Analysis and Design
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
Writing Classes (Chapter 4)
OOD – Identifying Classes Identifying the nouns (and noun phrases) in the application description The attributes correspond to descriptive words The behavior.
Big Java Chapter 12. Software Process - Waterfall Analysis Design Implementation Testing Deployment Does not work well when rigidly applied! established.
Chapter 5 Class Design. The Deliverables of the Class Design Process Class diagrams are further refined in this phase of development Object diagrams are.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Chapter 8: Arrays.
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 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 12 – Object-Oriented Design.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Design Model Lecture p6 T120B pavasario sem.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright © 2013 by John Wiley & Sons. All rights reserved. Slides by Rick Giles OBJECT- ORIENTED DESIGN CHAPTER 12.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
UML Class Diagram. A class diagram shows 1.Classes 2.The relationships between them.
12 OBJECT-ORIENTED DESIGN CHAPTER
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
OO Methodology Elaboration Phase Iteration 1- Part 3.
CSC 480 Software Engineering PSP Project 2 August 27, 2004.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Object Oriented Programming and Data Abstraction Earl Huff Rowan University.
BTS430 Systems Analysis and Design using UML Design Class Diagrams (ref=chapter 16 of Applying UML and Patterns)
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 12 – Object-Oriented Design.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 12 – Object-Oriented Design
A Review or Brief Introduction
Ch 12-13: OOD and Recursion Yonglei Tao.
About the Presentations
Advanced Java Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object Oriented Programming
Chapter 4 Topics: class declarations method declarations
Basics of OOP A class is the blueprint of an object.
Chapter 12 – Object-Oriented Design
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Class Diagrams Class diagram is basically a graphical representation of the static view of the system and represents different aspects of the application.
CS 200 Creating Classes Jim Williams, PhD.
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Course Information Updates Moodle is available Office Hours: Tuesday 5pm-6pm, TEL3035 Only accept and mark assignments, midterm and final exam for the students who are officially registered in this course Will not transfer all weights for assignments and midterm to the final –You can only miss one assignment or the midterm –Need to provide supporting documents Deferred final –Within one week following the missed final, send documents to the main office of Information Technology (TEL3068) –

Building a Class Review last lecture –A class describes a set of objects with the same behavior –An object is an instance of a class –Define a class Attributes Methods –Getters –Setters Today –Design multi-class applications –Use Universal Modeling Language (UML) to describe the multi-class applications –Arrays of Objects

Building Multi-Class Applications Why multi-class? –Complicated systems –Not able to describe in one single class Identify classes –Which classes to build? –Which attributes and methods to implement for these classes? –What are the relationships between classes?

OOD – Identifying Classes (1) Class –Identifying the nouns (and noun phrases) in the application description Attributes –The attributes correspond to descriptive words What characteristics distinguish this class from others? What uniquely identifies this object from another object of the same class? How is this object in this class associated with objects in other classes? Methods –The behavior (verbs) corresponds to public methods contained in their corresponding classes Methods fulfill the responsibilities of a class

OOD – Identifying Classes (2) Occasionally classes are not build to generate objects but to collect static methods and constants. –Utility classes, e.g., Math, Color, Integer Create classes which represent a single concept Example: –Simulate purses and the coins within each purse.

class Purse { public Purse(); public void addNickels(int count); public void addDimes(int count); public void addQuarters(int count); public double getTotal(); } class Coin{ public Coin(double value, String aName); public double getValue(); } class Purse { public Purse(); public void add(Coin aCoin); public double getTotal(); } Two concepts: 1. a purse that holds coins, and 2. the value of each coin. Class Purse uses/depends on class Coin

UML Universal Modeling Language (UML) –a standard language for specifying, visualizing, constructing, and documenting the artifacts of software systems, as well as for business modeling and other non-software systems. Class diagram –a type of static structure diagram that describes the structure of a system –class name, attributes, methods –relationship between classes

Coin UML class diagram A class diagram is a rectangle. Class name in the top A line separates the class name from the rest of the diagram. class Coin{ } UML Class Diagrams (1)

Following the class name we write the attributes of the class. A line separates the attributes from the rest of the diagram. Coin - coinValue: int Scope (visibility) + public - private Attribute name data type class Coin{ private int coinValue; } UML Class Diagrams (2)

Coin - coinValue: int Scope (visibility) + public - private Method name Return type Following the attributes, we write the methods. It is not necessary to show all attributes and methods. parameters class Coin{ private int coinValue; public Coin(double value, String aName); public double getValue(); } + Coin(double value, String aName) + getValue(): double UML Class Diagrams (3)

Relationships Between Classes The most common types of relationships: –Dependency –Aggregation –Inheritance

Dependency A class depends on another class if it uses objects of that class. The “knows about” relationship. Example: Purse depends on Coin class Coin{ public Coin(double value, String aName); public double getValue(); } class Purse { public Purse(); public void add(Coin aCoin); public double getTotal(); }

Dependency It is a good practice to minimize the coupling (i.e., dependency) between classes. When a class changes, coupled classes may also need updating.

Aggregation A class aggregates another if its objects contain objects of the other class. –Has-a relationship Example: a Quiz class aggregates a Question class. Aggregation is a stronger form of dependency.

Aggregation Use an instance variable public class Quiz { private ArrayList questions;... }

UML for Multiple Classes Using arrows to connect classes –Can represent relationships between the classes Dependency (Dotted line, open arrow tip) Aggregation (Solid line, diamond arrow tip) –Can represent relationship between instances of classes (multiplicities) Specific number (e.g. 1) Range of numbers (e.g. 1..5) Unbounded (e.g. 1..*) Any (*) –More types of relationships and symbols –Flexible in use

Case Study: Invoices Write a set of classes to support the creation and printing of invoices Classes: –Invoice –Product –LineItem –Address

public class Product { public Product(String aDescription, double aPrice) { description = aDescription; price = aPrice; } public String getDescription() { return description; } public double getPrice() { return price; } private String description; private double price; } public class LineItem{ public LineItem(Product aProduct, int aQuantity) { theProduct = aProduct; quantity = aQuantity; } public double getTotalPrice() { return theProduct.getPrice() * quantity; } public String format() { return String.format("%-30s%8.2f%5d%8.2f", theProduct.getDescription(), theProduct.getPrice(), quantity, getTotalPrice()); } private int quantity; private Product theProduct; } Product and LineItem classes

public class Address{ public Address(String aName, String aStreet, String aCity, String aState, String aZip) { name = aName; street = aStreet; city = aCity; state = aState; zip = aZip; } public String format() { return name + "\n" + street + "\n" + city + ", " + state + " " + zip; } private String name; private String street; private String city; private String state; private String zip; } Address class

public class Invoice{ public Invoice(Address anAddress) { items = new ArrayList (); billingAddress = anAddress; } public void add(Product aProduct, int quantity) { LineItem anItem = new LineItem(aProduct, quantity); items.add(anItem); } public String format() { String r = " I N V O I C E\n\n" + billingAddress.format() + String.format("\n\n%-30s%8s%5s%8s\n", "Description", "Price", "Qty", "Total"); for (LineItem i : items) { r = r + i.format() + "\n"; } r = r + String.format("\nAMOUNT DUE: $%8.2f", getAmountDue()); return r; } public double getAmountDue() { double amountDue = 0; for (LineItem i : items) { amountDue = amountDue + i.getTotalPrice(); } return amountDue; } private Address billingAddress; private ArrayList items; } Invoice class

public class InvoiceTester{ public static void main(String[] args) { Address samsAddress = new Address("Sam's Small Appliances", "100 Main Street", "Anytown", "CA", "98765"); Invoice samsInvoice = new Invoice(samsAddress); samsInvoice.add(new Product("Toaster", 29.95), 3); samsInvoice.add(new Product("Hair dryer", 24.95), 1); samsInvoice.add(new Product("Car vacuum", 19.99), 2); System.out.println(samsInvoice.format()); } InvoiceTester

Design your projects Hints: –Work from a requirements specification –Good idea to keep a list of candidate classes. –Cross not useful ones later. –Concepts from the problem domain are good candidates for classes. –Not all classes can be discovered from the program requirements After you have a set of classes –Define the behavior (methods) of each class Look for verbs in the task description –Match the verbs to the appropriate objects

A Case Study An Application Centre for university admissions A student can choose 3 different universities, and let’s assume that this center will accept up to 100 different applications. This program has to allow the user to: –enter the student applications, –accept students in some of the universities that they chose, –display the status of the applications.

Class Candidates Based on Nouns Application Centre University Admissions Student Universities Applications Status of Applications Two classes: ApplicationCentre and StudentApplication ✓ ✗ ✓ ✓ ✓ ✗ Too simple to constitute separate classes Should be a property

UML Class Diagram Application Centre - st[100]: StudentApplication - name : String + addStudent(): boolean + getStudent(int): StudentApplication StudentApplication - name: String - univ1, univ2, univ3: String - acc1, acc2, acc3: boolean + setAcceptance(): void + toString(): String * 1

1. class StudentApplication{ 2.private String name; 3.private String university0; 4.private String university1; 5.private String university2; 6.private boolean accept0; 7.private boolean accept1; 8.private boolean accept2; 9.public StudentApplication (String n, String u0, String u1, String u2){ 10.name = n; 11.university0=u0; 12.university1=u1; 13.university2=u2; 14.accept0=accept1=accept2=false; 15.} 16.public void setAcceptance(int which, boolean decision){ 17.switch(which){ 18.case 0: accept0=decision; break; 19.case 1: accept1=decision; break; 20.case 2: accept2=decision; break; 21.} 22.} 23.public String toString(){ 24.String result = name + ":\n"; 25. result += university0; 26.if (accept0) result += " - accepted\n"; 27.else result += " - rejected\n"; 28. result += university1; 29.if (accept1) result += " - accepted\n"; 30.else result += " - rejected\n"; 31. result += university2; 32.if (accept2) result += " - accepted\n"; 33.else result += " - rejected\n"; 34. return result; 35.} 36. }

1.class ApplicationCentre { 2. private String name; 3. private StudentApplication[] st; 4. private int studentCount; 5. private int size; public ApplicationCentre(String s){ 8. name=s; 9. size=100; 10. st = new StudentApplication[size]; 11. studentCount=0; 12. } 13. public String getName() { 14. return name; 15. } 16. public boolean addStudent(StudentApplication s){ 17. if (studentCount==size) return false; 18. st[studentCount]=s; 19. studentCount ++; 20. return true; 21. } 22. public StudentApplication getStudent(int which){ 23. if ( which studentCount-1){ 24. return null; 25. } 26. return st[which]; 27. } 28.}

Arrays Related data items of same type Group of contiguous memory locations Each memory location has same name Each memory location has same type

A 12-element array c[ 1 ] c[ 2 ] c[ 4 ] c[ 3 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] c[ 10 ] c[ 11 ] c[ 0 ] Name of array (Note that all elements of this array have the same name, c ) Position number (index of subscript) of the element within array c

Declaring and Allocating Arrays Declaring and allocating arrays –Arrays are objects that occupy memory –Allocated dynamically with operator new int c[] = new int[ 12 ]; –Equivalent to int c[]; // declare array c = new int[ 12 ]; // allocate array We can allocate arrays of objects too String b[] = new String[ 100 ]; Obtaining the size of an array –c.length

Initializing Arrays Initialize array elements –Use initializer list Items enclosed in braces ( {} ) Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; –Creates a five-element array –Subscripts of 0, 1, 2, 3, 4 –Do not need operator new

Arrays of Objects Allocating memory for an array does not automatically allocate the memory for each element! –Only references to the elements are created –The objects need to be allocated separately Example Account accounts[] = new Account[12]; accounts[0].getBalance(); // wrong, Account[0] is not initialized Account accounts[] = new Account[12]; for (int i = 0; i < accounts.length; i++) accounts[i] = new Account(); // initializing the array elements accounts[0].getBalance(); // correct

Reading Textbook Ch12