introduction to objects what is a constructor? use type to create a variable use class to create an...
TRANSCRIPT
![Page 1: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/1.jpg)
Introduction to Objects
What is a constructor?
![Page 2: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/2.jpg)
Use type to create a variable
Use class to create an object
int x;
Circle mycircle = new Circle();
![Page 3: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/3.jpg)
Let’s start simple
A circle
![Page 4: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/4.jpg)
What are some of the attributes of a circle?
• Radius (most obvious)
• Color
• Border
• Position
![Page 5: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/5.jpg)
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
![Page 6: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/6.jpg)
Circle()
class Circle {
double radius;
Circle(double r) { radius = r; }
double Area() { double this area = radius*radius*Math.PI; }
}
![Page 7: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/7.jpg)
Circle()
class Circle {
double radius;
Circle(double r) { radius = r; }
double Area() { double this area = radius*radius*Math.PI; }
}
Heading for the class
![Page 8: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/8.jpg)
Circle()
class Circle {
double radius;
Circle(double r) { radius = r; }
double Area() { double this area = radius*radius*Math.PI; }
}
A property ofeach circle
![Page 9: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/9.jpg)
Circle()
class Circle {
double radius;
Circle(double r) { radius = r; }
double Area() { double this area = radius*radius*Math.PI; }
}
A method named Areathat will calculate thearea of that specific circle
![Page 10: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/10.jpg)
Circle()
class Circle {
double radius;
Circle(double r) { radius = r; }
double Area() { double this area = radius*radius*Math.PI; }
}
A constructorUsed to initialize the circleLet’s see how in the next slide
-Name same as the class-No type
![Page 11: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/11.jpg)
Creating circles
Circle circle1 = new Circle(10);
Radius:10
circle1
![Page 12: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/12.jpg)
Creating circles
Circle circle1 = new Circle(10);
radius:10
circle1
Circle circle2 = new Circle(15);
radius:15
circle2
![Page 13: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/13.jpg)
What’s the difference?
• Circle circle1 = new Circle(10);
• Circle circle1; Creates a REFERENCELike having a telephone number for a friend.. a means to find them. Butthis one is a contact without a number.
![Page 14: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/14.jpg)
Creation requires a new
new uses the constructor
![Page 15: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/15.jpg)
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 constructorThis constructor chooses a radius (1) for us.
![Page 16: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/16.jpg)
Creating circles
Circle circle1 = new Circle();
radius:1
circle1
Circle circle2 = new Circle(15);
radius:15
circle2
default constructor
![Page 17: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/17.jpg)
Circle() : more constructorsclass 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; } }
![Page 18: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/18.jpg)
Circle() : more constructorsclass 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();
![Page 19: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/19.jpg)
Circle() : more constructorsclass 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);
![Page 20: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/20.jpg)
Circle() : more constructorsclass 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);
![Page 21: 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 ();](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697c00a1a28abf838cc7b5c/html5/thumbnails/21.jpg)
As many constructors as you want!