CM10134 / CM50147 CM10134 / CM50147 Pr Prog ogramming I mming I Lecture Handouts Dr. Marina De Vos 2005-2006
Programming 1 06/10/2005 Course Contents CM10134-CM50147 • Introduction to object-oriented Programming I programming… Basic Programming in Java • …with a strong software engineering foundation… • …aimed at producing and maintaining Marina De Vos large, high-quality software systems. 06/10/2005 Lecture 1a: Introduction 2 design patterns inheritance aggregation Buzzwords Goals reuse responsibility-driven design • Sound knowledge of programming abstraction encapsulation principles iterators overriding • Sound knowledge of object-orientation coupling cohesion • Able to critically assess the quality of a interface (small) software system javadoc mutator methods • Able to implement a small software system collection classes in Java polymorphic method calls 06/10/2005 Lecture 1a: Introduction 3 06/10/2005 Lecture 1a: Introduction 4 Course Text Additional Book Bruce Eckel David J. Barnes & Michael Kölling Thinking in Java, 3 rd Edition Objects First with Java A Practical Introduction using BlueJ Prentice-Hall, 2002 ISBN 031002872 Pearson Education, 2003 ISBN 0-13-044929-6. Free online copy: “http://www.mindview.net/Books/TIJ/” 06/10/2005 Lecture 1a: Introduction 5 06/10/2005 Lecture 1a: Introduction 6 Lecture 1a: Introduction 1
Programming 1 06/10/2005 Webpage Mailing lists • programming1@bath.ac.uk The course webpage is at www.cs.bath.ac.uk/~mdv/courses/prog1.html the general mailing list for this unit. Please check it regularly. It will be used for announcements and • tutors-programming1@cs.bath.ac.uk distribution of material for lectures, to contact the tutors of this unit and to labs, coursework . submit lab sheets in case of illness. 06/10/2005 Lecture 1a: Introduction 7 06/10/2005 Lecture 1a: Introduction 8 Course overview (1) Course overview (2) • Objects and classes • Inheritance • Understanding class definitions • Polymorphism • Object interaction • Extendable, flexible class structures • Grouping objects • Handling errors • More sophisticated behaviour - libraries • Designing applications • Well-behaved objects - testing, maintaining, debugging • Designing classes 06/10/2005 Lecture 1a: Introduction 9 06/10/2005 Lecture 1a: Introduction 10 Passing this Unit • Exam – Answer three questions on theoretical and practical issues of programming. • Coursework – Write four medium-sized programs in an object oriented way using the design methods we discussed. In order to be allowed to sit the exam and to do the coursework 7 exercise sheets have to be satisfactory completed. 06/10/2005 Lecture 1a: Introduction 11 Lecture 1a: Introduction 2
Programming 1 06/10/2005 Fundamental Concepts • object Objects and Classes • class • method • parameter First Programming Concepts • data type 06/10/2005 Lecture 1b: Objects and Classes 2 Objects and Classes Objects and Classes in BlueJ • Objects – represent ‘things’ from the real world, or from some problem domain (example: “the red car down there in the car park”) • Classes – represent all objects of a kind (example: “car”) Objects represent individual instantiations of the class. Object are instantiated . 06/10/2005 Lecture 1b: Objects and Classes 3 06/10/2005 Lecture 1b: Objects and Classes 4 Things we can do with Objects Things we can do with Objects 06/10/2005 Lecture 1b: Objects and Classes 5 06/10/2005 Lecture 1b: Objects and Classes 6 Lecture 1b: Objects and Classes 1
Programming 1 06/10/2005 Methods and Parameters Data Types • Objects/classes have operations which can be • Parameters have types . A type defines what invoked. They are called methods kinds of values a parameter can take. • void moveHorizontal(int distance) is called the • Defining a class defines a type signature of the methods • In Java, everything has a type. • The collection of methods of a class is referred to as the interface of that class • Java is strongly typed language • methods may have parameters to pass additional • Examples of types: int, String, Circle, … information needed to execute • Methods are called or invoked 06/10/2005 Lecture 1b: Objects and Classes 7 06/10/2005 Lecture 1b: Objects and Classes 8 Other Observations State • many instances can be created from a single class • an object has attributes : values stored in fields . • the class defines what fields an object has, but each object stores its own set of values. • These set of values is called the state of the object. 06/10/2005 Lecture 1b: Objects and Classes 9 06/10/2005 Lecture 1b: Objects and Classes 10 Two Circle Objects Object Interaction 06/10/2005 Lecture 1b: Objects and Classes 11 06/10/2005 Lecture 1b: Objects and Classes 12 Lecture 1b: Objects and Classes 2
Programming 1 06/10/2005 Source Code Return Values • Each class has source code (Java code) • Methods may return a result via a return associated with it that defines its details value. (fields and methods). • Example: String getName() • In other words, it determines the structure – This method returns a String. and the behavior of each of its instance. • Example: void changeName() • This source code is compiled and – Void indicates that this method does not return interpreted by Java. anything 06/10/2005 Lecture 1b: Objects and Classes 13 06/10/2005 Lecture 1b: Objects and Classes 14 Developing Java Programs Terms • To learn to develop Java programs, one • Object • Class needs to learn how to write class • Instance • Method definitions, including fields and methods, • State • Return Value and how to put these classes together as • Signature well • Compiler • Parameter • During the rest of this unit we will deal with • Virtual Machine • Type these issues in more detail • Source Code • Method Calling 06/10/2005 Lecture 1b: Objects and Classes 15 06/10/2005 Lecture 1b: Objects and Classes 16 Lecture 1b: Objects and Classes 3
Programming 1 13/10/2005 Main concepts to be covered • fields • constructors Understanding class definitions • methods • parameters Looking inside classes • assignment statements • conditional statements 13/10/2005 Lecture 2: Understanding Class 2 Definitions Ticket machines – an external Resulting Fields view • Exploring the behavior of a typical ticket machine. – Use the naive-ticket-machine project. – Machines supply tickets of a fixed price. • How is that price determined? – How is ‘money’ entered into a machine? – How does a machine keep track of the money that is entered? – How is a ticket provided? 13/10/2005 Lecture 2: Understanding Class 3 13/10/2005 Lecture 2: Understanding Class 4 Definitions Definitions Ticket machines – an internal Resulting Methods view • Interacting with an object gives us clues about its behavior. • Looking inside allows us to determine how that behavior is provided or implemented. – Looking at the source code • All Java classes have a similar-looking internal view. 13/10/2005 Lecture 2: Understanding Class 5 13/10/2005 Lecture 2: Understanding Class 6 Definitions Definitions Lecture 2: Understanding Class Definitions 1
Programming 1 13/10/2005 The Source Code Basic class structure The outer wrapper public class TicketMachine of TicketMachine { Inner part of the class omitted. } public class ClassName { Fields The contents of a Constructors class Methods } 13/10/2005 Lecture 2: Understanding Class 7 13/10/2005 Lecture 2: Understanding Class 8 Definitions Definitions Comments/Documentation Fields • Comments make source code easier to read • Fields store values for public class TicketMachine an object. { for humans. No effect on the functionality. private int price; • They are also known as private int balance; • Three sorts: private int total; instance variables . • Use the Inspect option Constructor and methods omitted. – // comment: single-line comments } to view an object’s – /* comments */: multiple-lines – more detail fields. – /** */: similar to previous, but used when • Fields define the state visibility modifier type variable name documentation software is used. of an object. private int price; 13/10/2005 Lecture 2: Understanding Class 9 13/10/2005 Lecture 2: Understanding Class 10 Definitions Definitions Constructors Passing data via parameters • Constructors initialize public TicketMachine(int ticketCost) an object. { price = ticketCost; • Then assign the balance = 0; necessary memory to total = 0; } the created object • They have the same name as their class. • They store initial values into the fields. • They often receive external parameter values for this. 13/10/2005 Lecture 2: Understanding Class 11 13/10/2005 Lecture 2: Understanding Class 12 Definitions Definitions Lecture 2: Understanding Class Definitions 2
Recommend
More recommend