Designing and Exploring Classes We look further into classes and objects here, emphasizing the differences between OOP and procedural programming – Cohesion.

Slides:



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

Fields, Constructors, Methods
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
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 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Lecture 9 Concepts of Programming Languages
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Topic 3 The Stack ADT.
Chapter 3 Introduction to Collections – Stacks Modified
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
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 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Written by: Dr. JJ Shepherd
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Click to edit Master text styles Stacks Data Structure.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Chapter 10 Thinking in Objects
Class Abstraction and Encaspulation
Chapter 10 Thinking in Objects
Abstract Data Types and Encapsulation Concepts
Chapter 10 Thinking in Objects
Java Programming: Guided Learning with Early Objects
Chapter 3: Using Methods, Classes, and Objects
Chapter 10 Thinking in Objects
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Lecture 9 Concepts of Programming Languages
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 9 Thinking in Objects
Defining Classes and Methods
Chapter 9 Thinking in Objects
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Introduction to Data Structure
Java Programming Language
Defining Classes and Methods
Chapter 10 Thinking in Objects
Corresponds with Chapter 5
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Designing and Exploring Classes We look further into classes and objects here, emphasizing the differences between OOP and procedural programming – Cohesion – a class should describe a single entity the class methods should all support that one entity – Consistency – be consistent with naming conventions (across all classes, not just within a class) – Encapsulation – use private for all instance data, providing adequate accessor and mutator methods, all of which should be public methods called only from within the class should be private – Clarity – do not impose restrictions on how the class should be used in terms of whether it can be used within another class or must be used in sequence with other classes – Completeness – make sure that you define all of the methods needed for that class to model the actual entity

More Design Issues Visibility issues – Do you plan on having this class be extended? If so, use protected instead of private – Will this class be placed in a package which might share access with private items? If so, use the default modifier (no modifier) – this can be risky Do you expect multiple objects of this class to share a datum? If so, you must use static on the datum and should create static methods – This will usually not be the case, each object should stand alone Will you implement methods that do not operate on this object but on multiple objects? – If so, make the method static and pass all objects to be operated on to the method

OOP vs Procedural Programming In procedural programming (found in such languages as C, Pascal, Ada, FORTRAN and C++) – The focus is on having code that alters the state of the program the state are the variables defined in the program there are no instance data, instead each procedure/function has its own local variables the procedure/function operates on its local variables and any parameters passed to it – The design emphasis is on the procedures/functions to perform tasks related to the overall problem local variables are defined to support the procedure/function’s task parameters are used for communication between procedures/functions

OOP vs Procedural Programming In OOP, the focus is on designing classes Select classes needed to solve the problem – Each class describes just its own data and the operations needed to manipulate/access that data – Class methods can have local variables while classes themselves have instance data in a way, this is like global variables of procedural programming except that these data are limited in scope to the class – Parameters are still used but are usually limited to telling a method how to access or modify an instance datum Class instance data can be objects of other classes

Class Abstraction and Encapsulation If we properly implement information hiding, then we are hiding the details of the class from any user of the class – We refer to this view of the class as class abstraction – Users know how to use the class but not how it works internally In order to know how to use the class, we make an interface available – the public methods – We encapsulate into the class the hidden details and the interface – that is, all of this code is defined inside the class – Yet from outside, all users can see is the from the interface Our classes can contain other (already defined) classes as part of their instance data – In such a case we have a “has-a” relationship – the class we are currently defining “has-a” the class we have already defined – We might refer to these as an aggregating class and an aggregated class respectively

Aggregation There are two forms of aggregation Composition means that the instance datum is “owned” by the object – For instance, a Student object might have a Name object as a member, the Student owns his/her Name Otherwise, the aggregation is not exclusively owned and therefore might be or is shared among others – In such a case, we might specify a multiplicity that dictates how many might share the given datum – A value of 1 means that the datum is owned exclusively – A range, from instance 1..3 would indicate that up to 3 objects can share the same datum We modify the UML notation by using a diamond to indicate aggregation/composition – White diamond = aggregation, black diamond = composition

Indicating Aggregation public class Person { private Person supervisor;... } A class can contain an instance datum whose type is the class being defined Here a Person has a Supervisor which is a Person

Designing a Loan Class Code on pages Loan user program on page Loan is an aggregating class because it contains an object of type Date, which is an aggregated class

Immutable Objects Once set to a value, an object is known as immutable if that value cannot be changed – Strings are immutable String foo=“Frank Zappa”; foo.toUpperCase( ) ; -- does not change foo, it merely returns the letters in foo all upper cased A class is immutable if objects of its type are immutable To make a class immutable – Make all instance data private, or – Do not provide any mutator methods, and – If any instance data are themselves objects, do not provide any accessors that return reference variables to those objects

Scope of a Variable Scope of a variable consists of the locations where that variable can be accessed – For a local variable, its scope is the entire method from the point it is declared until the end of the method UNLESS an inner block declares a variable of the same name – For a parameter, its scope is the entire method it was passed to – For an instance data, its scope is the entire class UNLESS a method declares a variable of the same name as a local variable or parameter

public class ScopeExample { private int x, y; public ScopeExample (int a, int x) { x=a; y=x;// x refers to? } public void method1() { x++; } public void method2(int x) { y=y+x;// x refers to? } public void method3(int a) { int x; x=a*2;// x refers to? y=x-2; } public void method4(int a) { int b=a; b++; { int b=y; x=b*2;// b refers to? } x=x-b;// b refers to? } This is a dumb example – do not name parameters using the same name as any instance data in your class (except possibly the constructor -- see the next slide

The this Reference this refers to this object We can use this when specifying a listener (we cover this later in the semester) responsible for handling actions We can also use this to refine a variable’s name when there are scope issues like we saw in method3 from the previous example We will commonly use this in a constructor to differentiate between an instance datum and a parameter of the same name public void SomeClass(String name) { name=name; } public void SomeClass(String name) { this.name=name; } Wrong – both name’s refer to the param Correct – this.name refers to instance datum

Example: BMI Class We see the UML for the BMI (body mass index) class which computes a person’s BMI based on weight and height What might we add to make this class more complete? See the code on pages

Building a Stack Class A stack is a data structure (container class) that stores items such that you can only add and remove items from one end – Think of a stack of trays in a cafeteria, you push your used tray onto the stack and the next person pops that tray off of the stack this represents a first-in last-out access (FILO) or a last-in first-out (LIFO) access Let’s assume that we want to store int values in our Stack – We will implement an array of ints and then provide methods to manipulate the array as we only want to access the stack at one end, the top, we won’t have to shuffle array elements around our “top” will move down the array and then back up as we push and pop elements, we keep track of this with an int variable top (stores the index in the array of the top of stack)

StackOfIntegers Class elements stores the stack (the int values) As we push an element onto the stack, we add it to elements at location size (our “top”) and increment size

public class StackOfIntegers { private int[] elements; private int size; public static final int CAPACITY=100; public StackOfIntegers() { this(CAPACITY); } public StackOfIntegers(int capacity) { elements=new int[capacity]; size=0; } public void push(int value) { if(size==elements.length) System.out.println ("Cannot push onto full stack"); else { elements[size]=value; size++; } public int getSize() { return size; }

public int pop() { if(size==0) { System.out.println ("Cannot pop from empty stack"); return ; } else { size--; return elements[size]; } public int peek() { if(size==0) { System.out.println("Stack is empty!"); return ; } else return elements[size]; } public boolean empty() { return size==0; }

Primitive Types vs Wrapper Types We may wish to treat all data as objects for consistency – Each of the 8 primitive types in Java have a corresponding class called a wrapper class e.g., Integer, Double, Character, Boolean there are also types called BigInteger and BigDouble that allow for any precision rather than a preset precision as with int, double, long – We can create one using Type var=new Type(value); where value is a primitive – For instance, we might take an int x and make it into Integer xAsObject=new Integer(x); – Placing a primitive into a wrapper type is called boxing – This will work if we declare an array of a wrapper type as well as in Integer[ ] ints={1, 2, 3}; making ints[0] an Integer storing the boxed int 1

Numeric Wrappers The numeric wrapper classes have several static methods and some constants – constants: MAX_VALUE, MIN_VALUE These methods are used to convert a String to either the numeric type of this class (e.g., to an Integer) or the corresponding primitive type – Integer.parseInt(someStringOfAnInteger); – Double.parseDouble(someStringOfADouble); – Integer.valueOf(someStringOfAnInteger); – Double.valueOf(someStringOfADouble); Each of these classes also contain two constructors (one of a String and one of the primitive type, e.g., String and int for Integer, String and double for Double) Methods are shown on the next slide

Integer and Double in UML NOTE: since there are no mutator methods, these classes are immutable

BigInteger and BigDecimal Classes Used for storing very large values – Recall an int can store a number as large as about 2 billion and a long can store up to 9,223,372,036,854,775,807 Methods include bitCount, bitLength, toString, and various conversions of the stored BigInteger/BigDouble Static methods, which are passed another BigInteger or BigDouble (or in some cases, and Object) include add, subtract, multiply, divide, mod, compareTo, equals, and, or, xor, gcd Both classes are part of java.math – Both classes are immutable – Both classes accept Strings in their constructors BigInteger a = new BigInteger(“ ”); BigDouble can also accept doubles but BigInteger cannot accept ints! – The integer’s value is stored in 2’s complement

Using BigInteger & BigDecimal To perform one of the arithmetic operators, use the following approach –BigInteger x=new BigInteger(…); –BigInteger y=new BigInteger(…); –x=x.add(y); – or –x=x.add(new BigInteger(…)); There is no limit to the precision for these two classes – Although it is possible that the divide method can throw an ArithmeticException if the division does not terminate For instance: –BigDecimal x=new BigDecimal(1); –x=x.divide(new BigDecimal(3)); Results in Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.