Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 8 Designing Classes. Assignment Chapter 9 Review Exercises (Written)  R8.1 – 8.3, 8.5 – 8.7, 8. 10, 8.11, 8.13, 8.15, 8.19, 8.20 Due Friday,
Chapter Goals To learn how to choose appropriate classes to implement
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Road Map Introduction to object oriented programming. Classes
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.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
Writing Classes (Chapter 4)
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain Name.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A class represents a single concept from the problem domain, or a.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
Chapter 9 – Designing Classes Goals Learning to design classes Learning to design classes Preconditions and postconditions for methods Preconditions.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Designing Classes. Chapter Goals  To learn how to choose appropriate classes to implement  To understand the concepts of cohesion and coupling.
Chapter 6: The Analysis Workflow Chapter 7: Classes and Objects Chapter 8: Finding Analysis Classes [Arlow and Neustadt, 2005] CS 426 Senior Projects in.
Chapter 9 – Designing Classes. Chapter Goals Discovering and learning to Design classes Discovering and learning to Design classes Preconditions and postconditions.
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSSE 220 Day 17 Introduction to your Vector Graphics project
Lecture 7 Designing Classes
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Introduction to Computer Science and Object-Oriented Programming
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
JAVA CLASSES.
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Classes.
Introduction to Computer Science and Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 12 Topics Choosing Classes Cohesion and Coupling Accessors, Mutators, and Immutable Classes Side Effects

Choosing Classes Designing classes is an art and a science Remember that in general classes should be nouns and methods should be verbs One approach is to think of classes as being one of four types: –Domain classes –Actors –Utility classes –Library classes

Choosing Classes Cont. Domain classes are typically extracted from the problem domain (what you are trying to solve). For example, for a payroll system, some classes could be Employee, TimeCard and PayCheck. Domain classes are entities that have well-defined properties. For example, one would expect to be able to withdraw and deposit money into a BankAccount class.

Choosing Classes Cont. Actors do some kind of work for you. For a payroll system, some actors could be CheckProcessor, TaxCalculator and CheckPrinter. Examples of Java API actors are the Scanner class and the Random class. As a rule of thumb, actors do things for or to a domain class and don’t have their own data. Typically, actors should end with an –er or an –or.

Choosing Classes Cont. You do not create objects from utility classes; they contains a collection of related static methods and constants. The Math class is a typical example. Remember that with a utility class you call methods on the class name, for example: double d = Math.sqrt(aValue);

// File Utility class public class FileUtil { public static BufferedReader openInputFile(String fileName) { BufferedReader aReader = null; try {... // Example code BufferedReader inEmp = null; String empFileName = “employee.dat”; inEmp = FileUtil.openInputFile(empFileName);

Choosing Classes Cont. Library classes contain reusable components that are used to assemble a program. The ArrayList Java API class is one example. You create objects from the ArrayList class (the objects have state, identity and behavior) and they are useful for many programs. There are many third-party library classes available also.

Cohesion and Coupling Cohesion is a measure of the degree to which a class models a single concept. The public interface (public methods and constants) should all be closely related to the real-world entity being modeled. High cohesion is desirable. A program that does everything in a single class would not be cohesive, but a program that is represented by a set of collaborative objects, each of which encapsulates a single concept, would be cohesive.

Cohesion and Coupling Cont. Coupling is a measure of the degree to which a class depends on other classes to work properly. A dependency (“A depends on B”) exists between classes A and B if objects of class A use objects of class B as instance fields, parameters, or temporary local variables. In general, the higher the degree of coupling a class has, the more difficult the class is to develop, test, and maintain because changes to class B affect class A; therefore, low coupling is desirable.

Accessors, Mutators, and Immutable Classes Accessors and mutators are methods that are part of the public interface of a class. An accessor provides a way of extracting information from an object that has properly encapsulated its data. A mutator, on the other hand, alters the state of the object by changing instance fields. A class that has no mutators is called, appropriately, immutable. Immutable objects help minimize side effects. The most common immutable objects are the String class and the wrapper classes ( Integer, Double, etc.).

Side Effects A mutator changes the state of an object by altering its instance fields, and the author of a class knows which methods are mutators and which are accessors. However, consider that another programmer who uses such a mutable object may not be aware of the change. Calling a mutator method has an externally visible change on the object called a side effect. Some side effects may not be expected, for example, when the change happens to a parameter to a method in another class.

// Example of an explicit parameter update public class BankAccount { public void transfer(double amount, BankAccount other) { balance = balance – amount; other.balance = other.balance + amount; }... The update of the passed-in object is a side effect. The rule of thumb is to minimize side effects to explicit parameters and to document all mutator methods.

Reference: Big Java 2 nd Edition by Cay Horstmann Choosing Classes (section 9.1 in Big Java) Cohesion and Coupling (section 9.2 in Big Java) Accessors, Mutators, and Immutable Classes (section 9.3 in Big Java) Side Effects (section 9.4 in Big Java)