CompSci 230 Software Construction

Slides:



Advertisements
Similar presentations
Phil Campbell London South Bank University Java 1 First Steps.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
UML Basics & Access Modifier
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Writing Classes (Chapter 4)
Writing Classes You have already used classes –String, Random, Scanner, Math, Graphics, etc –To use a class: import the class or the package containing.
Introduction to Programming Writing Java Beginning Java Programs.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Java Classes Using Java Classes Introduction to UML.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Introduction to Programming Writing Java Beginning Java Programs.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Unified Modeling Language (UML)
Introduction to Object-oriented Programming
Topic: Classes and Objects
GC211 Data structure Lecture 3 Sara Alhajjam.
CompSci 230 Software Construction
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Java Programming: Guided Learning with Early Objects
Lecture 5: Some more Java!
Lecture 6: Object-Oriented Design, Part 3
Testing and Debugging.
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
CompSci 230 Software Construction
More Object Oriented Programming
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Implementing Non-Static Features
CHAPTER 6 GENERAL-PURPOSE METHODS
Implementing Classes Chapter 3.
Classes, Objects, Methods and Strings
Tonga Institute of Higher Education
Basics of OOP A class is the blueprint of an object.
Object Oriented Programming Review
Object Oriented Programming in java
Java Programming Language
Dr. R Z Khan Handout-3 Classes
References Revisted (Ch 5)
Lecture 1 Introduction to Software Construction
Chapter 7 Objects and Classes
Presentation transcript:

CompSci 230 Software Construction Lecture Slides #4: Introduction to OOD Part 2 S1 2016

Agenda Topics: Static (class) variables & methods vs. instance variables & methods COMPSCI 230: IOOD

Instance vs. Class Variables Class variables are statically allocated, so they are shared by an entire class of objects. The runtime system allocates class variables once per class, regardless of the number of instances created of that class. Static storage is allocated when the class is loaded. All instances share the same copy of the class variables. In Java, class variables are declared as static. Instance variables are dynamically allocated, so they may have different values in each instance of an object. When an object is instantiated, the runtime system allocates some memory to this instance – so that it can “remember” the values it stores in instance variables. COMPSCI 230: IOOD

Instance & Class Methods Instance methods operate on the object's instance variables. They also have read & write access to class variables. They can call all other methods in the class: instance methods or class methods Class methods are static. Class methods cannot access instance variables. Class methods can access class variables. Class methods are handled by the “class object” – they can be called even if there are no instances of this class. (Example on the next slide.) COMPSCI 230: IOOD

Example: MixedCounter public class MixedCounter { private static int sharedCounterValue = 0; private int counterValue = 0; public int count() { sharedCounterValue++; return ++counterValue; } public int getInstanceCount() { return counterValue; public static int getSharedCount() { return sharedCounterValue; Class variable (static) Instance variable (not static) Note: When the initialization value is available, we can initialize in the declaration rather than in the constructor count() method is an instance method – increments both the instance (individual object) and shared counter value This getter returns the count stored in the object This getter returns the count stored in the class Question: Can we use this class as a counter without instantiating it? COMPSCI 230: IOOD

Using the MixedCounter class and objects public class MixedCounterProgram { public static void main(String[] args) { MixedCounter c1 = new MixedCounter(); MixedCounter c2 = new MixedCounter(); c1.count(); c2.count(); System.out.println("c1.getInstanceCount() returns " + c1.getInstanceCount()); System.out.println("c1.getSharedCount() returns " + c1.getSharedCount()); System.out.println("c2.getInstanceCount() returns " + c2.getInstanceCount()); System.out.println("c2.getSharedCount() returns " + c2.getSharedCount()); System.out.println("MixedCounter.getSharedCount() returns " + MixedCounter.getSharedCount()); } COMPSCI 230: IOOD

Using the MixedCounter class and objects public class MixedCounterProgram { public static void main(String[] args) { MixedCounter c1 = new MixedCounter(); MixedCounter c2 = new MixedCounter(); c1.count(); c2.count(); System.out.println("c1.getInstanceCount() returns " + c1.getInstanceCount()); System.out.println("c1.getSharedCount() returns " + c1.getSharedCount()); System.out.println("c2.getInstanceCount() returns " + c2.getInstanceCount()); System.out.println("c2.getSharedCount() returns " + c2.getSharedCount()); System.out.println("MixedCounter.getSharedCount() returns " + MixedCounter.getSharedCount()); } Invoking a static (class) method on an object works in practice but is considered improper (will produce a warning) Console output: c1.getInstanceCount() returns 3 c1.getSharedCount() returns 5 c2.getInstanceCount() returns 2 c2.getSharedCount() returns 5 MixedCounter.getSharedCount() returns 5 Invoking a static (class) method on the class itself COMPSCI 230: IOOD

UML Unified Modeling Language (UML) When creating complex OO systems, where do we start? When building complex systems, it might be worthwhile to plan things out before you start coding! When building a house, we usually have a set of plans. UML is a language which allows us to graphically model an OO system in a standardised format. This helps us (and others!) understand the system. There are many different UML diagrams, allowing us to model designs from many different viewpoints. Roughly, there are Structure diagrams (documenting the architecture), e.g. class diagrams Behaviour diagrams (documenting the functionality), e.g. use-case diagrams COMPSCI 230: IOOD

Representing classes in UML diagrams public class FancyMixedCounter { private static int sharedCounterValue = 0; private int counterValue = 0; public FancyMixedCounter(int startValue) { counterValue = startValue; } public FancyMixedCounter(int startValue, int sharedStartValue) { sharedCounterValue = sharedStartValue; public int count() { sharedCounterValue++; return ++counterValue; public int getInstanceCount() { return counterValue; public static int getSharedCount() { return sharedCounterValue; Three compartments: name, attribute, and operation Attribute compartment takes class and instance variables Operation compartment takes class and instance methods Types of variables, parameters and return values are specified Constructors are preceded by <<create>> Underlined members are static Can add visibility (public/private) by putting a “+” or a “–” in front of the member (not shown here) COMPSCI 230: IOOD

Technical note: Turning Java classes into UML class diagrams in ArgoUML File -> Import sources… Select .java file or directory (tree) with .java files in it Uncheck the box “Minimise Class icons in diagrams” on the right Hit “Open” On the left hand panel in the main window, click on “untitledModel” A sub-tree will open Click on any of the classes (small yellow icons with three compartments) and the class diagram will appear COMPSCI 230: IOOD

Technical note: Generating Java source code from UML diagrams in ArgoUML In the class diagram, select the classes you want to generate code for (skip this step if you want to generate code for all classes in the diagram) Select Generation -> Generate selected classes … or Generation - > Generate all classes… as required A small window pops open which lets you select in which languages you want to generate the code of each class Select an output directory and click on “Generate” Note: In Java, most primitive data types (int, double, boolean) are also available as classes (Integer, Double, Boolean). When you construct UML class diagrams, ArgoUML will by default offer you UML data types, which translate into the latter class types COMPSCI 230: IOOD

Review The OO approach is based on modeling the real world using interacting objects. OO design is a process of determining what the stakeholders require, designing a set of classes with objects which will meet these requirements, implementing, and delivering. The statements in a class define what its objects remember and what they can do (the messages they can understand), that is, they define Instance variables, class variables, instance methods, and class methods A UML class diagram shows the “bare bones” of an OO system design. It need not show all classes! (A diagram should not have irrelevant information.) Can use tools to generate UML from actual languages such as Java, and vice versa COMPSCI 230: IOOD

Review questions If a method is declared as static, does its return value have to be stored in a class variable? Can an instance method set the value of a class variable in its body? If I have several objects of the same class, how many copies of its class variables exist in memory? If I have several objects of the same class, how many copies of its instance variables exist in memory? Can a class method read the value of an instance variable? How can we invoke a class method? How do UML class diagrams describe the members of a class? What do class variables look like in UML? What about instance variables? What about constructors? COMPSCI 230: IOOD