CSC 222: Object-Oriented Programming

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.
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.
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.
1 CSC 221: Computer Programming I Fall 2004 Objects and classes: a first pass  software objects, classes, object-oriented design  BlueJ IDE, compilation.
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
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.
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.
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.
Applications Development
1 CSC 222: Object-Oriented Programming Spring 2013 Objects and classes: a first pass  221 review  software objects, classes, object-oriented design 
Chapter 4 Introduction to Classes, Objects, Methods and strings
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)
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
Chapter 1 – Introduction
CSC 222: Object-Oriented Programming Fall 2017
CSC 222: Object-Oriented Programming
Appendix A Barb Ericson Georgia Institute of Technology May 2006
CSC 222: Object-Oriented Programming
Chapter 2: The Visual Studio .NET Development Environment
Programming what is C++
Topic: Python’s building blocks -> Variables, Values, and Types
Programming Logic and Design Seventh Edition
Introduction to Programming
Unified Modeling Language
Objects and Classes CITS1001 week 1.
Chapter 1: An Introduction to Visual Basic 2015
CSC 222: Object-Oriented Programming
Classes and OOP.
Key Ideas from day 1 slides
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Java Course Review.
Eclipse Navigation & Usage.
CSC 222: Object-Oriented Programming
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
CompSci 230 Software Construction
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Procedural Abstraction Object-Oriented Code
Unit E Step-by-Step: Programming with Python
Java Programming with BlueJ
Board: Objects, Arrays and Pedagogy
Fundamentals of Programming
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Java Applets.
Using JDeveloper.
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Java Programming with BlueJ Objectives
Defining Classes and Methods
Defining Classes and Methods
Chapter 17 JavaScript Arrays
Introduction to Programming
Computer Programming-1 CSC 111
CSC 221: Introduction to Programming Fall 2018
Workshop for Programming And Systems Management Teachers
An Introduction to the Windows Operating System
Presentation transcript:

CSC 222: Object-Oriented Programming Fall 2015 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 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

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)/100 50.5

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'

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

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 REAL WORLD CLASS: automobiles REAL WORLD OBJECTS: my 2011 Subaru Outback, the batmobile, … 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

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

Skip-3 solitaire example DeckOfCards properties? behaviors? 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)

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:

BlueJ and software 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 BlueJ includes an editor, debugger, visualizer, documentation viewer, … we will start with a visual example in BlueJ: drawing shapes

Starting up BlueJ to start up the BlueJ IDE, double-click on the BlueJ desktop icon 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 e.g., could create a project for the entire semester, or HW by HW 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

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

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, …)

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

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

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., 3.14159) 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

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

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

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

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"