1 CSC 221: Computer Programming I Fall 2005 Class design & strings  design principles  cohesion & coupling  class examples: Hurricane, TaxReturn  objects.

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Java Programming Strings Chapter 7.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.
Programming 2 CS112- Lab 2 Java
Chapter 5 Decisions Goals To be able to implement decisions using if statements To be able to implement decisions using if statements To understand how.
Fundamental Programming Structures in Java: Strings.
Chapter 5 – Decisions Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
1 CSC 222: Object-Oriented Programming Spring 2012 Simulations & library classes  HW3: RouletteWheel, RouletteGame, RouletteTester  javadoc  java.lang.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
1 CSC 222: Object-Oriented Programming Spring 2013 interacting objects  abstraction, modularization  internal vs. external method calls  expressions,
1 Chapter 9 Scope, Lifetime, and More on Functions.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
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.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 CSC 221: Computer Programming I Spring 2008 Class design & strings  design principles  cohesion & coupling  class example: graphical DotRace  static.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 CSC 221: Computer Programming I Spring 2010 Design & text processing  design principles  cohesion & coupling  objects vs. primitives  String methods:
1 CSC 222: Computer Programming II Spring 2005 Java interfaces & polymorphism  Comparable interface  defining an interface  implementing an interface.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
Chapter 6 Decisions. Chapter Goals To be able to implement decisions using if statements To understand how to group statements into blocks To learn how.
1 CSC 221: Computer Programming I Fall 2009 Class design & strings  design principles  cohesion & coupling  example: graphical DotRace  objects vs.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
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.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 CSC 221: Computer Programming I Fall 2005 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
1 CSC 221: Computer Programming I Fall 2005 graphics & design  Dot & DotRace revisited  Circle class implementation  adding Circle methods  static.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
CSC 222: Object-Oriented Programming Fall 2017
More Sophisticated Behavior
Chapter Goals To implement decisions using if statements
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming Spring 2015
Nested Branches Nested set of statements: Example: Federal Income Tax
CSC 222: Object-Oriented Programming Fall 2017
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
Chapter 7: Strings and Characters
Chapter 4: Writing classes
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
CSC 222: Object-Oriented Programming Spring 2013
Suggested self-checks: Section 7.11 #1-11
CSC 222: Object-Oriented Programming Spring 2012
Presentation transcript:

1 CSC 221: Computer Programming I Fall 2005 Class design & strings  design principles  cohesion & coupling  class examples: Hurricane, TaxReturn  objects vs. primitives  String methods  class example: Bounce

2 Object-oriented design principles so far:  a class should model some entity, encapsulating all of its state and behaviors e.g., Circle, Die, Dot, DotRace, …  a field should store a value that is part of the state of an object (and which must persist between method calls) e.g., xPosition, yPosition, color, diameter, isVisible, … can be primitive (e.g., int, double ) or object (e.g., String, Die ) type should be declared private to avoid outside tampering with the fields – provide public accessor methods if needed static fields should be used if the data can be shared among all objects final-static fields should be used to define constants with meaningful names  a constructor should initialize the fields when creating an object can have more than one constructor with different parameters to initialize differently  a method should implement one behavior of an object e.g., moveLeft, moveRight, draw, erase, changeColor, … should be declared public to make accessible – helper methods can be private local variables should be used to store temporary values that are needed if statements should be used to perform conditional execution

3 Cohesion cohesion describes how well a unit of code maps to an entity or behavior in a highly cohesive system:  each class maps to a single, well-defined entity – encapsulating all of its internal state and external behaviors  each method of the class maps to a single, well-defined behavior advantages of cohesion:  highly cohesive code is easier to read don't have to keep track of all the things a method does if the method name is descriptive, it makes it easy to follow code  highly cohesive code is easier to reuse if the class cleanly models an entity, can reuse it in any application that needs it if a method cleanly implements a behavior, it can be called by other methods and even reused in other classes

4 Coupling coupling describes the interconnectedness of classes in a loosely coupled system:  each class is largely independent and communicates with other classes via small, well-defined interfaces advantages of loose coupling:  loosely coupled classes make changes simpler can modify the implementation of one class without affecting other classes only changes to the interface (e.g., adding/removing methods, changing the parameters) affect other classes  loosely coupled classes make development easier you don't have to know how a class works in order to use it since fields/local variables are encapsulated within a class/method, their names cannot conflict with the development of other classes.methods

5 Class example: Hurricane suppose we wanted to enter and access information on a hurricane  create a Hurricane object with a name  enter a wind speed reading  get the hurricane's name  get the last wind speed reading  get the highest wind speed reading  get the current category level for the hurricane state? fields? constructor? methods?

6 Hurricane class public class Hurricane { private String officialName; private int lastWindSpeed; private int highestWindSpeed; public Hurricane(String name) { officialName = name; lastWindSpeed = 0; highestWindSpeed = 0; } public String getName() { return officialName; } public int getLastWindSpeed() { return lastWindSpeed; } public int getHighestWindSpeed() { return highestWindSpeed; } public void enterReading(int currentWindSpeed) { lastWindSpeed = currentWindSpeed; if (currentWindSpeed > highestWindSpeed) { highestWindSpeed = currentWindSpeed; } public int category() { if (lastWindSpeed > 155) { return 5; } else if (lastWindSpeed > 130) { return 4; } else if (lastWindSpeed > 110) { return 3; } else if (lastWindSpeed > 95) { return 2; } else if (lastWindSpeed > 73) { return 1; } else { return 0;// ??? } cohesive?

7 Hurricane class public String expectedDamage() { if (category() == 5) { return "Complete roof failure on many residences; " + "some complete building failures."; } else if (category() == 4) { return "Some complete roof structure failure on small residences."; } else if (category() == 3) { return "Some structural damage to small residences; " + "mobile homes are destroyed."; } else if (category() == 2) { return "Some roofing material, door, and window damage; " + "considerable damage to mobile homes."; } else if (category() == 1) { return "No real damage to building structures; " + "some damage to unanchored mobile homes."; } else { return "There is no hurricane condition."; } again, highly cohesive classes tend to be easier to modify  if we wanted to add a method for predicting damage, we don't have to think in terms of wind speed anymore  instead, can build upon the category method

8 Class example: TaxReturn in the text, a class is developed for calculating a person's 1992 income tax

9 TaxReturn class class TaxReturn { private static final double RATE1 = 0.15; private static final double RATE2 = 0.28; private static final double RATE3 = 0.31; private static final double SINGLE_CUTOFF1 = 21450; private static final double SINGLE_CUTOFF2 = 51900; private static final double SINGLE_BASE2 = ; private static final double SINGLE_BASE3 = ; private static final double MARRIED_CUTOFF1 = 35800; private static final double MARRIED_CUTOFF2 = 86500; private static final double MARRIED_BASE2 = 5370; private static final double MARRIED_BASE3 = 19566; public static final int SINGLE = 1; public static final int MARRIED = 2; private int status; private double income; /** * Constructs a TaxReturn object for a given income and marital status. anIncome the taxpayer income aStatus either TaxReturn.SINGLE or TaxReturn.MARRIED */ public TaxReturn(double anIncome, int aStatus) { income = anIncome; status = aStatus; }... the cutoffs and tax rates are magic numbers  represent as constants fields are needed to store income and marital status  here, marital status is represented using a constant  the user can enter a number OR the constant name

10 TaxReturn class... /** * Calculates the tax owed by the filer. the amount (in dollars) owed */ public double getTax() { double tax = 0; if (status == SINGLE) { if (income <= SINGLE_CUTOFF1) { tax = RATE1 * income; } else if (income <= SINGLE_CUTOFF2) { tax = SINGLE_BASE2 + RATE2 * (income - SINGLE_CUTOFF1); } else { tax = SINGLE_BASE3 + RATE3 * (income - SINGLE_CUTOFF2); } else if (income <= MARRIED_CUTOFF1) { tax = RATE1 * income; } else if (income <= MARRIED_CUTOFF2) { tax = MARRIED_BASE2 + RATE2 * (income - MARRIED_CUTOFF1); } else { tax = MARRIED_BASE3 + RATE3 * (income - MARRIED_CUTOFF2); } return tax; } the getTax method first tests to determine if SINGLE or MARRIED then tests to determine the tax bracket and calculate the tax  note: could have just returned the tax in each case instead of assigning to a variable cohesive?

11 Unknown status... public static final int SINGLE = 1; public static final int MARRIED = 2; public TaxReturn(double anIncome, int aStatus) { income = anIncome; status = aStatus; } public double getTax() { double tax = 0; if (status == SINGLE) { if (income <= SINGLE_CUTOFF1) { tax = RATE1 * income; } else if (income <= SINGLE_CUTOFF2) { tax = SINGLE_BASE2 + RATE2 * (income - SINGLE_CUTOFF1); } else { tax = SINGLE_BASE3 + RATE3 * (income - SINGLE_CUTOFF2); } else if (income <= MARRIED_CUTOFF1) { tax = RATE1 * income; } else if (income <= MARRIED_CUTOFF2) { tax = MARRIED_BASE2 + RATE2 * (income - MARRIED_CUTOFF1); } else { tax = MARRIED_BASE3 + RATE3 * (income - MARRIED_CUTOFF2); } return tax; } QUESTION: what if the user entered 3 for marital status? error? result?

12 Checking the status... /** * Calculates the tax owed by the filer. the amount (in dollars) owed */ public double getTax() { double tax = 0; if (status == SINGLE) { if (income <= SINGLE_CUTOFF1) { tax = RATE1 * income; } else if (income <= SINGLE_CUTOFF2) { tax = SINGLE_BASE2 + RATE2 * (income - SINGLE_CUTOFF1); } else { tax = SINGLE_BASE3 + RATE3 * (income - SINGLE_CUTOFF2); } else if (status == MARRIED) { if (income <= MARRIED_CUTOFF1) { tax = RATE1 * income; } else if (income <= MARRIED_CUTOFF2) { tax = MARRIED_BASE2 + RATE2 * (income - MARRIED_CUTOFF1); } else { tax = MARRIED_BASE3 + RATE3 * (income - MARRIED_CUTOFF2); } return tax; } could add an extra test to make sure status is 1 (SINGLE) or 2 (MARRIED)  what is returned if status == 3?

13 A nicer version?... private String status; private double income; /** * Constructs a TaxReturn object for a given income and status. anIncome the taxpayer income aStatus either "single" or "married" */ public TaxReturn(double anIncome, String aStatus) { income = anIncome; status = aStatus; } /** * Calculates the tax owed by the filer. the amount (in dollars) owed */ public double getTax() { double tax = 0; if (status == "single") {... } else if (status == "married") {... } return tax; } suppose we wanted to allow the user to enter a word for marital status will this work? not quite – you can't use == on Strings WHY?

14 Strings vs. primitives although they behave similarly to primitive types (int, double, char, boolean), Strings are different in nature  String is a class that is defined in a separate library: java.lang.String  a String value is really an object  you can call methods on a String  also, you can Inspect the String fields of an object

15 Comparing strings comparison operators ( >= ) are defined for primitives but not objects String str1 = "foo"; // EQUIVALENT TO String str1 = new String("foo"); String str2 = "bar"; // EQUIVALENT TO String str2 = new String("bar"); if (str1 < str2) … // ILLEGAL == and != are defined for objects, but don't do what you think if (str1 == str2) … // TESTS WHETHER THEY ARE THE // SAME OBJECT, NOT WHETHER THEY // HAVE THE SAME VALUE! Strings are comparable using the equals and compareTo methods if (str1.equals(str2)) … // true IF THEY REPRESENT THE // SAME STRING VALUE if (str1.compareTo(str2) < 0) … // RETURNS -1 if str1 < str2 // RETURNS 0 if str1 == str2 // RETURNS 1 if str1 > str2

16 A nicer version... private String status; private double income; /** * Constructs a TaxReturn object for a given income and status. anIncome the taxpayer income aStatus either "single" or "married" */ public TaxReturn(double anIncome, String aStatus) { income = anIncome; status = aStatus; } /** * Calculates the tax owed by the filer. the amount (in dollars) owed */ public double getTax() { double tax = 0; if (status.equals("single")) {... } else if (status.equals("married")) {... } return tax; } to test whether two Strings are the same, use the equals method  what is returned if status == "Single" ?

17 String methods many methods are provided for manipulating Strings boolean equals(String other) returns true if other String has same value int compareTo(String other) returns -1 if less than other String, 0 if equal to other String, 1 if greater than other String int length() returns number of chars in String char charAt(int index) returns the character at the specified index (indices range from 0 to str.length()-1) int indexOf(char ch) returns index where the specified char/substring int indexOf(String str) first occurs in the String (-1 if not found) String substring(int start, int end) returns the substring from indices start to (end-1) String toUpperCase() returns copy of String with all letters uppercase String toLowerCase() returns copy of String with all letters lowercase

18 An even nicer version... private String status; private double income; /** * Constructs a TaxReturn object for a given income and status. anIncome the taxpayer income aStatus either "single" or "married" */ public TaxReturn(double anIncome, String aStatus) { income = anIncome; status = aStatus.toLowerCase(); } /** * Calculates the tax owed by the filer. the amount (in dollars) owed */ public double getTax() { double tax = 0; if (status.charAt(0) == 's') {... } else if (status.charAt(0) == 'm') {... } return tax; } we would like to allow a range of valid status entries  "s" or "m"  "S" or "M"  "single" or "married"  "Single" or "Married"  "SINGLE" or "MARRIED" ... to be case-insensitive  make the status lowercase when constructing to handle first letter only  use charAt to extract the char at index 0

19 Class example: Bounce suppose we want to animate a circle, so that it bounces around the Canvas  since Canvas and Circle are highly cohesive, it greatly simplifies the task need to create a class with a Circle as field at each step, move the Circle a set amount along a trajectory  need to recognize when the Circle reaches a wall  if so, change the trajectory so that it bounces off the wall to avoid having the user specify each step, we want to automate the repetition all movements will utilize well-defined, public methods of the Circle class  the implementation of Bounce is separate from the implementation of Circle  as long as the required methods are still implemented, changes can be made to the Circle class without affecting Bounce  thus, they are loosely coupled

20 Bounce class public class Bounce { private static final int WINDOW_SIZE = 300; private static final int DELTA_RANGE = 5; private int xDelta; private int yDelta; private Circle ball; public Bounce() { ball = new Circle(); ball.makeVisible(); xDelta = (int)((2*DELTA_RANGE+1)*Math.random())-DELTA_RANGE; yDelta = (int)((2*DELTA_RANGE+1)*Math.random())-DELTA_RANGE; } public void go() { int newX = Math.max(0, Math.min(ball.getXPosition()+xDelta, WINDOW_SIZE-ball.getSize())); if (newX == 0 || newX == WINDOW_SIZE-ball.getSize()) { xDelta = -xDelta; } int newY = Math.max(0, Math.min(ball.getYPosition()+yDelta, WINDOW_SIZE-ball.getSize())); if (newY == 0 || newY == WINDOW_SIZE-ball.getSize()) { yDelta = -yDelta; } ball.moveTo(newX, newY); go();// SCARY! BUT OK FOR NOW } the changes in X & Y positions are defined by xDelta & yDelta  each is initialized to be a random int between ±DELTA_RANGE  at each step, the X & Y positions of the ball are updated by xDelta and yDelta, resp.  note: need to stop at a window edge and reverse the delta note: the go method calls itself to produce (uncontrolled) repetition!