CompSci 230 Software Construction

Similar documents
Today s Agenda UML. CompSci 280 S Introduction to Software Development. 1.Introduction UML Diagrams. Topics: Reading:

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Chapter 1 GUI Applications

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lecture 02, Fall 2018 Friday September 7

UCLA PIC 20A Java Programming

Object Oriented Programming (OOP) is a style of programming that incorporates these 3 features: Encapsulation Polymorphism Class Interaction

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

1.00 Lecture 8. Using An Existing Class, cont.

Introduction to Java

CPSC 324 Topics in Java Programming

ITI Introduction to Computing II

Lecture 6 Introduction to Objects and Classes

CS1004: Intro to CS in Java, Spring 2005

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Introduction to Computer Science I

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

GUI Forms and Events, Part II

Lecture 1 - Introduction (Class Notes)

Lecture 06: Classes and Objects

Java Classes & Primitive Types

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

Java Classes & Primitive Types

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Week 9 Implementation

CompSci 125 Lecture 02

Tutorial 2 Today we will have a quick introduction to ArgoUML and how to use it, as well as look at some examples of class diagrams.

Slide 1 CS 170 Java Programming 1

1.00 Lecture 4. Promotion

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Project #1 Computer Science 2334 Fall 2008

Lecture 7 Objects and Classes

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

Exercise 1: Intro to Java & Eclipse

Express Yourself. Writing Your Own Classes

CSE 401 Final Exam. December 16, 2010

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Java Magistère BFA

CS 231 Data Structures and Algorithms, Fall 2016

CHAPTER 7 OBJECTS AND CLASSES

CompSci 125 Lecture 07. Chapter 4: Object-Oriented Development

CHAPTER 7 OBJECTS AND CLASSES

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Chapter 4 Defining Classes I

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

CSE 143 Au04 Midterm 1 Page 1 of 9

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CS1004: Intro to CS in Java, Spring 2005

CSCU9T4: Managing Information

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Example: Fibonacci Numbers

CS 170 Java Programming 1. Week 12: Creating Your Own Types

Inheritance and Interfaces

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Swing from A to Z Some Simple Components. Preface

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 9: Writing Java Applets. Introduction

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Create a Java project named week9

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

JAVA Programming Language Homework I - OO concept

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Object-Orientation. Classes Lecture 5. Classes. State and Behaviour. Instance Variables. Object vs. Classes

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 1006/1406: Assignment # 3 Due Monday July 27, 2015, 12:00 Noon Total Marks: 60

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

Inf1-OP. Classes with Stuff in Common. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein.

CSSE2002/7023 The University of Queensland

Introduction to Computation and Problem Solving

Object-Oriented Concepts

BM214E Object Oriented Programming Lecture 8

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Week 3 Classes and Objects

Chapter 6 Lab Classes and Objects

CMSC131. Library Classes

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Inf1-OP. Inheritance. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. March 12, School of Informatics

Encapsulation. Inf1-OOP. Getters and Setters. Encapsulation Again. Inheritance Encapsulation and Inheritance. The Object Superclass

COMP110 Jump Around. Go ahead and get today s code in Eclipse as shown on next few slides. Kris Jordan

Chapter 6 Lab Classes and Objects

Lesson 10: Quiz #1 and Getting User Input (W03D2)

G52PGP. Lecture oo3 Java (A real object oriented language)

Lecture 3. Lecture

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Introduction to Software Engineering: Tools and Environments. Session 9. Oded Lachish

Crash Course Review Only. Please use online Jasmit Singh 2

Transcription:

CompSci 230 Software Construction Lecture Slides #3: Introduction to OOD S1 2015 Version 1.1 of 2015-03-12: added return to code on slides 10, 13

Topics: Agenda Software Design (vs. hacking) Object-Oriented Design (vs. other approaches to SW design) Classes & Objects Introduction to UML class diagrams Object diagrams may be helpful for visualizing instantiations Variables & Methods 2

Software Design Communication: identify stakeholders, find out what they want and need. Planning: list tasks, identify risks, obtain resources, define milestones, estimate schedule. Modeling develop structure diagrams and use cases, maybe some other UML artifacts. Different approaches: OO, procedural, data. Construction: implement the software, with assured quality. Deployment: deliver the software, then get feedback for possible revision. To learn more: R. Pressman, Software Engineering: A Practitioner s Approach, 7 th Ed., 2010, pp. 14-15. 3

4 What is Object-Oriented Design? In OO design, a system is a collection of interacting objects. Each object should have simple attributes and behaviours. Each object should have simple relations to other objects. In procedural design, a system is a collection of basic blocks. Each basic block should have a simple effect on local and global variables. Basic blocks are linked by controlflow arcs: if/then/else, call/return, while/loop, for/loop, case, goto, In data architecture, a system is a collection of data structures, with access and update methods. Each data structure should have simple relations to other data structures. object 3 object 2 Program object 4 object 1

What is an Object? A building block for OO development Like objects in the world around us Objects have state and behaviour Examples: Dog State/field/attribute: name, colour, ishungry, Behaviour: bark(), fetch(), eat(), Bicycle State: gear, cadence, colour, Behaviour: brake(), turn(), changegear(), VCR State: brand, colour, ison Behaviour: play(), stop(), rewind(), turnon(), 5

Class Classes & Objects A set of objects with shared behaviour and individual state Individual state: Data is stored with each instance, as an instance variable. Shared behaviour: Code is stored with the class object, as a method. Shared state may be stored with the class object, as a class variable. Object Objects are created from classes at runtime by instantiation usually with New. There may be zero, one, or many objects (instances) of a class. Instantiated objects are garbage-collected if no other user-defined object can reference them. 6

Object Imagine a world of communicating objects An object remembers things (i.e. it has a memory): its state. An object responds to messages it gets from other objects. It performs the method with the given parameters, then sends a response. An object that receives a strange message may throw an exception. Be careful! An object s method may ask for help from other objects. It sends a message to an object, and waits for a response. A method may send a message to itself! This is called recursion. Be careful. Messages between objects Usually: method calls and method returns, sometimes exceptions. 7

Information Hiding The implementation details of a method should be of no concern to the sender of the message. If a JavaKid tells a JavaDog to fetch(), the dog might run across a busy street during its fetch(). Parameterised methods allow the senders to have more control over object behaviour. For example, a JavaDog might have a parameterised fetch() method: ball = dog.fetch(safely); Note: in these lecture slides, the word should indicates an element of style. You should write Java code that is understandable to other Java programmers. 8

Attributes Example 1: Ball Represent the internal state of an instance of this class. Constructor Creates the object Methods 9 Implement the processing performed by or to an object, often updating its state. If there are read and write methods for an attribute x, these should be called getx() and setx(). You should learn Java s conventions for capitalisation and naming. Example: Ball.java public class Ball { public final static int SIZE = 20; private int xpos; private int ypos; private Color color; public Ball(int x, int y, Color c) { xpos = x; ypos = y; color = c; public void move(int deltax, int deltay) { xpos += deltax; ypos += deltay; public void paint(graphics g) { g.setcolor(color); g.filloval(xpos,ypos,size,size);

10 Object Instantiation When a constructor method is called, a new instance is created. Ball b = new Ball( 10, 20, Color.Red ); Ball c = new Ball( 0, 10, Color.Blue ); If a class definition doesn t include a constructor method, the Java compiler inserts a default constructor with default initialisations. public class Class1 { private int x; // Note no explicit constructor public int increment() { return ++x; // is this good code? b: Ball xpos = 10 ypos = 20 Color = Red x = 0 d: Class1 c: Ball xpos = 0 ypos = 10 Color = Blue Class1 d = new Class1(); Blecch!

Message Passing In a method call, a message is passed to a receiver object. The receiver s response to the message is determined by its class. Ball b = new Ball(10, 20, Color.Red); b.move(50, 100); message receiver arguments b: Ball xpos =10 = 60 ypos = 20120 Color = Red public class Ball {... public void move(int deltax, int deltay) { xpos += deltax; ypos += deltay; 11

Instance & 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. 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. Test your understanding: List the names of all class variables in Ball. List the names of all instance variables in Ball. b1: Ball xpos=10 ypos = 20 Color = Red : Class name = Ball size = 10 b2: Ball xpos =10 ypos = 10 Color = Blue 12

Instance & Class Methods Instance methods operate on this object's instance variables. They also have read & write access to class variables. E.g. Class methods are static. public class Class1 { private int x; public int increment() { return ++x; // or x++? Class methods cannot access instance 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.) 13

Class1App public class Class1App { public static void main( String[] args ) { Class1 x = new Class1(); System.out.println( "Without initialisation, ++x = " + x.increment() ); System.out.println( "After another incrementation, ++x = " + x.increment() ); 14

15 BallApp import java.awt.*; import java.awt.event.*; public class BallApp extends Frame{ Ball b = new Ball( 20, 30, Color.blue ); public BallApp() { addwindowlistener( new WindowAdapter() { public void windowclosing( WindowEvent e ) { System.exit( 0 ); ); setsize( 300, 200 ); setvisible( true ); public void paint(graphics g) { b.paint( g ); public static void main( String[] args ) { new BallApp();

public class SharedCounter { private static int count; private int value; public SharedCounter(int value) { this.value = value; count++; public int getvalue() { return value; public static int getcount() { return count; public String tostring() { return "value=" + value + " count=" + count; public static void main(string[] args) { SharedCounter c1 = new SharedCounter(10); SharedCounter c2 = new SharedCounter(100); SharedCounter c3 = new SharedCounter(200); System.out.println(c1 + " " + c2 + " " + c3); 16 : : Class name = = SharedCounter count count = = 0 01 12 23 c1: SharedCounter value = 10 c2: SharedCounter value = 100 c3: SharedCounter value = 200

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 17

Object Diagrams in UML In this lecture, I have drawn some object diagrams of instance models (using coloured boxes). An object diagram is a graphic representation of an instance model, showing the state of a system after some objects have been instantiated, and after some variables of these objects have been updated. Object diagrams are very helpful in tuition, but are not commonly used outside the classroom. Please focus on the basics. Understand the distinction between static variables and instance variables. Develop a working understanding of instantiation this is a crucial concept! Learn how to draw UML-standard class diagrams. Honours-level students might want to learn more about object diagrams. I recommend Modelling instances of classifiers using UML object diagrams, online Help resource for the IBM Rational Software Modeler, available 4 March 2014. 18

19 Tool Support: Eclipse & ArgoUML? You will need a Java development environment. I strongly recommend Eclipse. The de-facto industry standard for Java developers. It s FOSS: free and open-source software. Its codebase is robust and is under active development. Your tutors will help you learn Eclipse. Alternatively, you may use javac and a text editor (e.g. emacs) with Java support I reckon every Java developer should know how to run javac from a console, but I won t attempt to teach this! You will draw some class diagrams and use-case diagrams. Options: ArgoUML Supports forward- and reverse-engineering. Class diagrams Java skeletons. Java classes class diagrams. FOSS, works ok but missing some features such as an undo button save your versions carefully! No longer under active development: v0.34 is dated 15 December 2011. Not on lab image you ll have to download and unzip the binary distribution in your echome directory (or on your USB pendrive) then double-click on argouml.jar (this is an executable jarfile ). See http://argouml-stats.tigris.org/documentation/quickguide-0.32/ch02s02.html. Any general-purpose drawing package (e.g. Visio) Warning: you ll have trouble with the fancy arrowheads in UML! Maybe Softwarestencils.com/uml/visio? By hand: This is your only option during exams and tests You ll have to scan your drawings into your assignments (which are submitted online)

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 The hardest concept in this set of lecture slides: instantiation. Very important! 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.) 20