213500 Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES 213500 PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Inheritance Inheritance Reserved word protected Reserved word super
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 Defining Your Own Classes Part 2.
Java Programming Review (Part I) Enterprise Systems Programming.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Classes CS 21a: Introduction to Computing I First Semester,
Utilities (Part 2) Implementing static features 1.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Chapter 3 Objects and Classes. Objects Object – a data type with structure, state, and operations to access and manipulate state - an instance of a class.
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7- Defining Your Own Classes Part 2 : Objectives After you have read and studied this chapter, you should be able to –Describe how objects are.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
Topic: Classes and Objects
More Sophisticated Behavior
JAVA MULTIPLE CHOICE QUESTION.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
About the Presentations
Chapter 3 Introduction to Classes, Objects Methods and Strings
Interface.
Java Programming Language
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Defining Classes and Methods
Applying OO Concepts Using Java
Anatomy of a Java Program
Object Oriented Programming in java
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming1 Programming class diagrams and Java classes, parameters B Module 2: Software Systems 15 November 2013

Week 1 Values, conditions Classes, objects Week 2 Specifications Testing Week 3 Abstraction, inheritance Week 4 Interfaces, abstract classes Arrays Week 5 Collections Generic structures Week 6 Exceptions Stream I/O GUIs Week 7 Concurrency Networking Week 8 Security Week 9/10 Project Software Systems - Programming 2 OVERVIEW PROGRAMMING LINE

Software Systems - Programming 3 PROGRAM DESIGN  A program must be designed before code is implemented Example: Hotel Information System  System to store guests of a hotel, including the name and in which room they stay  (In general and sufficient for now) nouns indicate the relevant concepts, in this example:  Guest  Hotel  Room

Software Systems - Programming 4 PROGRAMMA ONTWERP  Ultimately program consists of objects that represent specific hotels, rooms and guests (instances of these concepts) Examples  ‘Hotel Fawlty Towers’  ‘Room 101’, ‘Room 102’, etc.  ‘Major Gowen’, ‘Miss Tibbs’, etc.  First generalise these objects by defining classes for them. HotelRoomGuest

PROGRAM DESIGN  Define relations (associations) between the concepts (classes)  Hotel has Rooms  Room belongs to a Hotel  Guest occupies a Room  Room has (zero or one) Guest Software Systems - Programming 5 one-to-one one-to-many 1..* HotelRoomGuest *

PROGRAM DESIGN  Define attributes and features of each concept  Java terminology:  Attribute corresponds to field  Feature corresponds to method Software Systems - Programming 6 Hotel String name String address checkin(String) Room int number Guest getGuest() Guest String name Room getRoom() * attributes features

PROGRAM DESIGN  Define attributes and features of each concept Software Systems - Programming 7 Hotel -String name -String address +checkin(String) Room -int number +Guest getGuest() Guest -String name +Room getRoom() * attributes features Visibility: -: private, can only be used by class itself +: public, can be used by every class UML class diagrams: standard notation for classes, their attributes and features and their relations

Software Systems - Programming 8 REMARKS  Program design is no complete system  Parts like the user interface are missing  Concepts, attributes and features are incomplete  Further development in phases  Design is no program  Gives a specifiation and structure  Not executable: details are missing  Next step: implement (in Java)  There may be more than one (good) design!

CLASSES AND INSTANCES  At runtime there are objects in the system  Class is a recipe for creating objects  Objects instances of a class Software Systems - Programming 9 Hotel String name String address checkin(String) Hotel h = new Hotel(“Fawlty Towers”, “Torquay”); Hotel String name String address Calls the constructor of the class. “Fawlty Towers” “Torquay” Attributes Instance Variables

CLASSES AND INSTANCES  Strings are reference values  Variables store reference to String object Software Systems - Programming 10 Hotel name address checkin(String) Hotel String name String address String value int length “Fawlty Towers” 13 String value int length “Torquay” 7

CLASSES AND INSTANCES  Example: Hotel 'Fawlty Towers', rooms 101 and 102 occupied by guest Major Gowen Hotel String name String address Room room1 Room room2 Room int number Guest guest Room int number Guest guest Guest name Room room null “Fawlty Towers” “Torquay” “Majow Gowen” Primitive values are stored in the variables. Reference can be null (point to no object). Software Systems - Programming 11

STATIC ATTRIBUTES  Values of static attributes stored in class variables Software Systems - Programming 12 Hotel String name String address double vat checkin(String) h1:Hotel String name String address Static attributes are underlined in UML. Hotel double vat 0.06 h2:Hotel String name String address Class Attributes Class Variables

VARIABLES Software Systems - Programming 13 class Hotel { static double vat; String name; String address; public double getBill(String guestName) { Room room; room = this.getRoom(guestName); return room.getPrice() * (1 + Hotel.vat); } class variable: once per class instance variables: once per object local variables: once per method execution

VARIABLES Software Systems - Programming 14 class Hotel { static double vat; String name; String address; public double getBill(String guestName) { Room room; room = this.getRoom(guestName); return room.getPrice() * (1 + Hotel.vat); } this: similar to local variable, refers to object on which this method was called formal parameter: similar to local variable, value provided at method call Hotel h =... h.getBill(“Major Gowen”); Calls a method on object h and passes argument “Major Gowen”.

Software Systems - Programming 15 NAMING In general  Identifiers consist of letters, digits and underscore “_”  Must start with letter Conventions  Classes start with upper case letter ( Guest )  Attributes / features start with lower case letter ( name )  New word within identifier starts with upper case ( getName )  Constants use all upper case and underscore to separate words ( MAX_VALUE )  Use meaningful names  no foo, var, i1, etc.  Cannot use keywords as identifiers  class, if, int, etc.

Software Systems - Programming 16 COMMENTS  Text for documenting the code  One-line: starts with // reaches till the end of the line  Multi-line: Between /* and */  Useful for programmer  Increases comprehensibility  Especially when working in a team  Improves maintainability  Is required in this course!

Software Systems - Programming 17 COMMENTS: JAVADOC  Special kind of comments for documenting how to use a class  Multi-line between /** and */  Start with textual description.  Use tags to document specific information  In front of  Class definition  important  Method definition   Field definition

Software Systems - Programming 18 PACKAGES  Group related classes  Consists of identifiers separated by dots (use lower case letters)  Example: ss.week1.hotel  Must match folder hierarchy  Example: ss\week1\hotel\Guest.java  Package declaration in the first line of the file package ss.week1.hotel;  Namespace: avoid name clashes between independently developed classes  Fully qualified class names include the containing package  Example: ss.week1.hotel.Guest  Details: section 2.8 in the book

Software Systems - Programming 19 JAVA FILES package pname; import ; /** JavaDoc documentation of the class. */ public class CName { /** JavaDoc documentation of the attribute. */ /** JavaDoc documentation of the feature. */ }  Details of import : section 2.9 of the book For example Java packages java.util.Date; java.util.*;

Software Systems - Programming 20 DEFINITION OF A CLASS package ss.week1.hotel; /** * Manages a hotel. John Cleese 2.0 */ public class Hotel {... } Javadoc-comment namespace Javadoc-tag for author of class Visibility (for now only public for classes) Javadoc-tag for class version general description

Software Systems - Programming 21 DEFINITION OF AN ATTRIBUTE /** * Stored the hotel’s name. */ private String name; /** * Constant representing a 1 star rating. */ public static final String RATING_ONE_STAR = “*”; Visibility (for now only public and private for attributes and features) Field’s type Class variable instead of instance variable Value will not change during execution

Software Systems - Programming 22 DEFINITION OF A QUERY /** * Returns the number of the room * occupied by the specified guest. name the guest’s name the room number */ public int getRoomNumber(String name ) { Room room = getRoom(name); return room.getNumber(); } Javadoc-tag for documenting the parameter “name” formal parameter list, may be empty result type statement for returning the query result Javadoc-tag for documenting the result value

Software Systems - Programming 23 DEFINITION OF A COMMAND /** * Finds a free room and * occupies it with the guest. name the guest’s name */ public void checkin(String name) {... return; } No result value, therefore the result type is “void”. Terminates the command without returning a value.

Software Systems - Programming 24 DEFINITION OF A CONSTRUCTOR /** * Creates a new Hotel. theName name of the hotel to create theAddress address of the hotel to create */ public Hotel(String theName, String theAddress ) { this.name = theName; this.address = theAddress; } Remarks  Constructor creates instances of the class and initialises the attributes  Name of the constructor is the same as the class name  Constructor has no result type

 Classes represent concepts  Objects are instances of classes  Class diagrams can specify their attributes, features and relations  Class variables, instance variables, local variables, formal parameters  JavaDoc to document classes, attributes and features  Packages to group classes Software Systems - Programming 25 MAIN POINTS