1 CSC 222: Object-Oriented Programming Spring 2013 Objects and classes: a first pass  221 review  software objects, classes, object-oriented design 

Slides:



Advertisements
Similar presentations
1 CSC 221: Introduction to Programming Fall 2011 List comprehensions & objects building strings & lists comprehensions conditional comprehensions example:
Advertisements

1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
ITEC113 Algorithms and Programming Techniques
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Fall 2007ACS-1903 BlueJ Ron McFadyen Lab 1: Using BlueJ at UofW The purpose of this lab is to give you some experience with BlueJ. Over the course of the.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Marlene Galea.  The JDK (Java Development Kit)  An IDE (Integrated Development Environment) ◦ Different IDEs include:  JCreator  NetBeans  BlueJ.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Unified Modeling Language
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
1 CSC 221: Computer Programming I Fall 2004 Objects and classes: a first pass  software objects, classes, object-oriented design  BlueJ IDE, compilation.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Python Programming Fundamentals
High-Level Programming Languages: C++
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
IE 411/511: Visual Programming for Industrial Applications
Introduction to Programming with Java. Overview What are the tools we are using – What is Java? This is the language that you use to write your program.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Introduction to Computers and Programming – Computer Programming.
1 CSC 221: Computer Programming I Spring 2010 Objects and classes: a broad view  Scratch programming review  object-oriented design, software objects.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Java Applets. 2 Introduction to Java Applet Programs  Applications are stand alone programs executed with Java interpreter executed with Java interpreter.
OBJECTS AND CLASSES CITS1001. Concepts for this lecture class; object; instance method; parameter; signature data type multiple instances; state method.
IDEs Department of Information Systems and Computer Science Ateneo de Manila University.
Chapter 1 Object Orientation: Objects and Classes.
Java Applets. 2 Introduction to Java Applet Programs Applications are ___________________ programs –executed with Java interpreter Applet is a small program.
1 CSC 222: Object-Oriented Programming Spring 2013 Course goals:  To know and use basic Java programming constructs for object- oriented problem solving.
1 CSC 221: Computer Programming I Spring 2008 Objects and classes: a broad view  software objects, classes, object-oriented design  BlueJ IDE, compilation.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
Applications Development
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
1 CSC 221: Computer Programming I Fall 2009 Objects and classes: a broad view  Scratch programming review  object-oriented design, software objects 
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Unified Modeling Language (UML)
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
1 CSC 221: Computer Programming I Fall 2005 Understanding class definitions  class structure  fields, constructors, methods  parameters  assignment.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
CSC 222: Object-Oriented Programming Fall 2017
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
Introduction to Programming
CSC 222: Object-Oriented Programming
Java Course Review.
CSC 222: Object-Oriented Programming
CSC 222: Object-Oriented Programming
Java Programming with BlueJ
Java Applets.
Introduction to Programming
Java Programming with BlueJ Objectives
Defining Classes and Methods
Introduction to Programming
CSC 221: Introduction to Programming Fall 2018
Presentation transcript:

1 CSC 222: Object-Oriented Programming Spring 2013 Objects and classes: a first pass  221 review  software objects, classes, object-oriented design  BlueJ IDE, compilation & execution, figures example  method calls, parameters  data types, object state  other examples: Die, SequenceGenerator

Recall from 221… programming is the process of designing/implementing/debugging algorithms in a format that computers can understand & execute  high-level languages (e.g., Python, Java, C++, C#, PHP, JavaScript) enable the programmer to use abstract constructs (e.g., variables, while loop)  a compiler and/or interpreter translates the high-level code into executable machine code 2 variables  are names that correspond to values, which can be set/updated via assignment statements >>> name = 'Guido'  languages provide built-in operators for manipulating/combining values, expressions can appear on the right-hand side of assignments >>> average = (num1 + num2 + num3)/3.0

Recall from 221… control statements provide for conditional execution and repetition  in Python: if, if-else, if-elif-else, while, for if average >= 60: print('Nice job, you passed.') else: print('You failed. Get to work!') num = 10 while num > 0: print(num) num = num – 1 sum = 0 for i in range(1, 11): sum = sum + i print sum 3

Recall from 221… functions enable the programmer to group statements together under a single name  a function is a unit of "computational abstraction"  parameters make the function general, can be called on different values  return statements make it possible to use the result in an expression def sumToN(N): sum = 0 for i in range(1, N+1): sum = sum + i return sum >>> sumToN(10) 55 >>> sumToN(100) 5050 >>> sumToN(100)/

Recall from 221… strings and lists are simple, indexable data structures  a string is a sequence of characters, with built-in operations (e.g., len, [], upper)  a list is a sequence of arbitrary items, with built-in operations (e.g., len, [], reverse) def acronym(phrase): words = phrase.split() str = '' for w in words: str = str + w[0] return str.upper() >>> acronym("What you see is what you get") 'WYSIWYG' >>> acronym("Fouled up beyond all recognition") 'FUBAR' 5

Recall from 221… 221 covered other language features & programming techniques  list comprehensions  library/module use  I/O & file processing  simple OO HW7: Skip-3 Solitaire game –DeckOfCards class captured properties/behavior of a deck of cards –RowOfCards class captures properties/behaviors of a row of cards –Skip3 function could create objects to model these parts of the game, then focus on the game interface 6

7 Object-oriented programming the object-oriented approach to programming:  solve problems by modeling real-world objects e.g., if designing a banking system, model clients, accounts, deposits, …  a program is a collection of interacting objects  in software, objects are created from classes the class describes the kind of object (its properties and behaviors) the objects represent individual instantiations of the class  the class encompasses all automobiles they all have common properties: color, seats, wheels, engine, brakes, … they all have common behaviors: can sit in them, start them, accelerate, steer, …  each car object has its own specific characteristics and ways of producing behaviors my car is white & seats 5; the batmobile is black & seats 2 accelerating with V-4 is different than accelerating with jet engine REAL WORLD CLASS: automobiles REAL WORLD OBJECTS: my 2011 Subaru Outback, the batmobile, …

Die example Die properties?behaviors? 8 # sides __init__ (to initialize) # of rolls roll numberOfSides numberOfRolls >>> d6 = Die(6) >>> d8 = Die(8) >>> d6.roll() 3 >>> d6.roll() 6 >>> d6.numberOfRolls() 2 >>> d8.numberOfRolls() 0

Skip-3 solitaire example DeckOfCards properties?behaviors? 9 list of cards (e.g., "QH") __init__ (to initialize) shuffle dealCard addCard numCards __str__ (convert to string) RowOfCards properties?behaviors? list of cards (front == left) __init__ (to initialize) addAtEnd moveCard numCards __str__ (convert to string)

10 Shape classes and objects a slightly more abstract example involves shapes  class: circles what properties do all circles share? what behaviors do all circles exhibit?  objects: similarly, could define classes and object instances for other shapes  squares:  triangles:

11 BlueJ and software shapes we will start with a visual example in BlueJ: drawing shapes the BlueJ interactive development environment (IDE) is a tool for developing, visualizing, and debugging Java programs  BlueJ was developed by researchers at Deakin University (Australia), Maersk Institute (Denmark), and University of Kent (UK)  supported by Oracle (previously Sun Microsystems), the developers of Java  note that BlueJ does NOT include a Java compiler/interpreter must install Oracle’s JDK (Java Development Kit); BlueJ connects to it BlueJ includes an editor, debugger, visualizer, documentation viewer, …

12 Starting up BlueJ to start up the BlueJ IDE, double-click on the BlueJ desktop icon to create a new BlueJ project  click on the Project heading at the top left & select New Project  enter the project name and location to open an existing BlueJ project  click on the Project heading at the top left & select Open Project  browse to locate and select the project this opens the BlueJ main window  in order to create and execute a program, must first create or load a project  a project groups together all the files needed to produce a working program

13 Loading the figures project download figures.zip from the class Code directory  save it on the Desktop and double-click to unzip  in BlueJ, select Open Project  browse to select figures when a project loads, its classes are shown in a diagram  here, there are 5 classes  Canvas represents a painting area  Circle, Square, Triangle, and Person represent shapes  the arrows show that the shapes depend upon the Canvas class

14 Editing and compiling classes you can view/edit a class definition by double-clicking on its box  this opens the associated file in the BlueJ editor before anything can be executed, the classes must be compiled  recall, the Java compiler translates Java source code into Java byte code  to compile all classes in a project, click on the Compile button (note: non-compiled classes are shaded, compiled classes are not) IMPORTANT: classes don’t act, objects do!  you can’t drive the class of all automobiles  but you can drive a particular instance of an automobile in order to draw a circle, must create a circle object  then, can specify properties of that instance (radius, color, position, …)

15 Example: creating a circle right-click on a class to see all the actions that can be applied  select new Circle() to create a new object  you will be prompted to specify a name for that object (circle1 by default) the new Circle object appears as a box at the bottom of the screen  note: classes and objects look different EXERCISE: create 2 circles, a square, and a triangle

16 Applying object methods can cause objects to act by right-clicking on the object box, then selecting the action  the actions that objects can perform are called methods (same as in Python: a method is a function that belongs to an object.)  here, void makeVisible() opens a Canvas in which the shape is displayed EXERCISE: make the other shapes visible EXERCISE: select other methods to change the color and size of objects EXERCISE: play

17 Methods and parameters sometimes an action (i.e., method) requires information to do its job  the changeColor method requires a color (“red”, “green”, “black”, …)  the moveHorizontal method requires a number (# of pixels to move)  data values provided to a method are called parameters Java provides for different types of values  String is a sequence of characters, enclosed in double-quotes (e.g., “red”)  int is an integer value (e.g., 40)  double is a real value (e.g., )  char is a character value (e.g., ‘A’)  the parameter to changeColor is a String representing the new color  the parameter to moveHorizontal is an int representing the # of pixels to move

18 Objects and state recall that each object has properties and methods associated with it  when you create a Circle, it has an initial size, color, position, …  those values are stored internally as part of the object  as methods are called, the values may change  at any given point, the property values of an object define its state BlueJ enables you to inspect state of an object  right-click on the object  select Inspect to see the values of object properties note: objects of the same class have the same properties, but may have different values

19 IN-CLASS EXERCISE create objects and call the appropriate methods to produce a picture like this

20 Another example: Die class can define a Die class to model different (numeric) dice  properties shared by all dice: number of sides, number of times rolled  behaviors/methods shared by all dice: roll it, get # of sides, get # of rolls  the roll method generates a random roll and returns it the return value is displayed by BlueJ in a Method Result window

21 Another example: SequenceGenerator there are two options for creating a SequenceGenerator object  can specify an alphabet to choose from (e.g., "etaoinshrd")  if nothing specified, will assume "abcdefghijklmnopqrstuvwxyz"