What About Alice? In Alice, we have seen that we have:  Objects (skater, snowman, snowwoman, camera)  Methods (move, turn, roll, move toward)  Properties.

Slides:



Advertisements
Similar presentations
Chapter 10 Introduction to Objects and Classess 1.
Advertisements

Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-1: Classes and Objects reading: self-checks: Ch. 8 #1-9 exercises:
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Object Oriented Software Development
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
1 CSC103: Introduction to Computer and Programming Lecture No 13.
Writing Classes (Chapter 4)
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSC Programming I Lecture 8 September 9, 2002.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Program Organization Sequential Execution: One line done after the other Conditional Execution: If a test is true, one section is done, otherwise another.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
6.2 Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Classes and OOP.
Creating Objects & String Class
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Object-oriented Design in Processing
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes.
More on Classes and Objects
Chapter 8 Lecture 8-1: Classes and Objects
CS2011 Introduction to Programming I Objects and Classes
Object-oriented Design in Processing
Introduction to Object-Oriented Programming
Classes / Objects / Methods
Object-oriented Design in Processing
Object-oriented Design in Processing
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

What About Alice? In Alice, we have seen that we have:  Objects (skater, snowman, snowwoman, camera)  Methods (move, turn, roll, move toward)  Properties (color, opacity) In addition, although not as obvious, Alice has classes.

Classes and Objects Snowman is both a class and an object. As a class, it refers to a generic snowman. The class allows us to create many snowmen (snowman, snowman1, snowman2, etc.). Each snowman may be customized, e.g., the hats of the snowwomen were different colors, and the snowwomen were in different locations. Changing the value of a property of one snowwoman, doesn't change the value for another snowwoman.

Classes (cont'd) The snowman class says what methods and property a snowman can have. It is a blueprint for making actual snowmen.

Objects Objects, in Alice, are created by dragging and dropping the prototype from the Gallery, or from copying an existing object, using the copy button. Although two objects can be made from the same blueprint, they can be customized differently.

Properties The class specifies what properties its objects can have. Each object has its own set of values. For example, color is a property of all snowwomen, but each snowwoman may have a different color.

Methods The class also specifies the methods. We can add new methods, modify existing methods, and call methods from other methods. One object can invoke the method of another object.

Java Classes In Java, a class is a blueprint for objects. A class describes the fields (properties) and methods (methods, functions) that the objects have. Each object has its own values for each field. Fields are also called instance variables. Fields and methods together are called members of the class, and are sometimes called components of the class.

Class Definition A class is created by a class definition. The form is: class Snowman { }

Classes as Types In Java, classes are types. We can create objects of that type and give them names, just as we can create int s and give them names. We can name a object using a variable. The statement: Snowman snowman1; declares the variable snowman1 to be of type Snowman, but it does not create a Snowman object!

Creating Objects To create an object, we must use the new operator. The operator new creates and returns a new object of the given class. The statement: Snowman snowman1 = new Snowman(); declares snowman1 to be an object of type Snowman, creates a new Snowman object, and initializes snowman1 to have this new object as its value.

Example We've seen this statement before: Scanner sc = new Scanner(System.in); declared sc to be a variable of type Scanner, created a new Scanner object (based on reading in from the keyboard) and initialized sc to be this new object.

Instance An object of a class is called an instance of that class, and creating the object is called instantiation.

Example Snowman This might be the start of a Snowman class: public class Snowman { private Color color; private int opacity; private double xPosition; private double yPosition; public void move() {... } public void turn() {... } }

Classes “ A class is basically a structure with member functions as well as member data. Classes are central to the programming methodology known as object-oriented programming.”

Procedural Programming In procedural programming, we break down a program in terms of functions. Top-down design principles start with the problem at hand and finds a solution by breaking down the problem into steps that can be used to solve it. We begin with the major steps, and then break those down into smaller steps. This process is called stepwise-refinement.

Result The result is a program in which functions play the major role. Data is merely passed to and from functions. Data may be structured but is secondary to the flow of control of the program. This is the procedural programming paradigm.

Object-Oriented Design In object-oriented design, instead of thinking in terms of functions, we think in terms of objects. Objects are collections of related data along with the functions that apply to that data. Objects occur naturally in many problems, and so by thinking about what objects are needed to solve a problem, we often can create better solutions.

OOD - Example Let's take a simple example: A clock. What is the data associated with a clock? What operations do we normally perform on that data?

OOD – Another Example A second example is a bank account. Let's take a simple form of a bank account – a checking account with interest. What is the data? balance, APR What are the operations? deposit, withdrawal, check balance, modify APR, check APR

Exercise Write a class declaration for a bank account class. A bank account should have a balance and an APR. In your main method, create three bank accounts.

Object-Oriented Design In object-oriented design the problem is decomposed into different objects that are inherent in the problem. The program relies on the interaction between the objects. The objects can call each others' methods. This is called message passing. A message is the name (and parameters) of the method to be called. Different types of objects may have methods with the same name, so which method is actually run depends on the object and the message.