Introduction to Objects What is a constructor? Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Slides:



Advertisements
Similar presentations
Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming.
Advertisements

Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double.
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Tutorial 6 February 4th/5th, 2015
Const Parameters & In Line Functions 04/15/11. Next Time  Quiz, Monday, 04/18/11  Over 5.2 and 5.3 void functions pass-by-reference  Read 7.1 about.
More on Objects CS 102 More, more, more on Objects.
Liang Chapter 6 Variable scope and the keyword this.
Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
CIRCUMFERENCE OF A CIRCLE LEARNING TARGET 4: I CAN SOLVE PROBLEMS USING AREA AND CIRCUMFERENCE OF A CIRCLE.
Mental Math Computation. Multiply Mentally 84 × 25 What strategy did you use? Why did we choose 84 × 25 instead of 85 × 25?
Standard Form for the Equation of the Circle
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
Classes / Objects An introduction to object-oriented programming.
Class & Object 蔡柏灃.
Introduction to Objects A way to create our own types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Circles Introduction Circle equation Circle properties Practice qs Points of intersection.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
CSC 212 – Data Structures Lecture 11: More Inheritance & Generic Types.
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Objects and Classes Mostafa Abdallah
Inequalities and their Graphs Objective: To write and graph simple inequalities with one variable.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Warm-Up What is the distance between the two trees? If you wanted to meet a friend halfway, where would you meet.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
The Circle. Examples (1) – (5) Determine the center and radius of the circle having the given equation. Identify four points of the circle.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Classes and Objects (contd.) Course Lecture Slides 19 May 2010.
Data Structures in Scheme Building data structures: Java class Circle { private double center_x, center_y, radius; Circle(double x, double y, double r)
1 Introduction to Object Oriented Programming Chapter 10.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
A First Book of C++ Chapter 12 Extending Your Classes.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Phil Tayco Slide version 1.0 Created Sep 10, 2017
Inequalities and their Graphs
Inequalities and their Graphs
Inequalities and their Graphs
Inequalities and their Graphs
Chapter 8 Objects and Classes
Objects and Classes Creating Objects and Object Reference Variables
Introduction to Objects & Classes
Chapter 6 Objects and Classes
Object-Oriented Programming and class Design
Chapter 8 Objects and Classes
NAME 436.
Object-Oriented Programming and class Design
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

Introduction to Objects What is a constructor?

Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();

Let’s start simple A circle

What are some of the attributes of a circle? Radius (most obvious) Color Border Position

Let’s start with a simple Circle class Just a radius –No borders or colors A means of asking it for it’s area. This will serve as the basis (a type or class) for creating lots of circles

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; }

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Heading for the class

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A property of each circle

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A method named Area that will calculate the area of that specific circle

Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A constructor Used to initialize the circle Let’s see how in the next slide -Name same as the class -No type

Creating circles Circle circle1 = new Circle(10); Radius:10 circle1

Creating circles Circle circle1 = new Circle(10); radius:10 circle1 Circle circle2 = new Circle(15); radius:15 circle2

What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates a REFERENCE Like having a telephone number for a friend.. a means to find them. But this one is a contact without a number.

Creation requires a new new uses the constructor

Circle() a default constructor class Circle { double radius; Circle() { radius = 1.0; } Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Another constructor This constructor chooses a radius (1) for us.

Creating circles Circle circle1 = new Circle(); radius:1 circle1 Circle circle2 = new Circle(15); radius:15 circle2 default constructor

Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } }

Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } } Circle c1 = new Circle();

Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } } Circle c2 = new Circle(10.0);

Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } } Circle c3 = new Circle(10.0,1.0,2.0);

As many constructors as you want!