introduction to objects a way to create our own types

32
Introduction to Objects A way to create our own types

Upload: erick-garrison

Post on 05-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Objects A way to create our own types

Introduction to Objects

A way to create our own types

Page 2: Introduction to Objects A way to create our own types

Type and Variables

• Until this point we were only able to create simple types, actually call “primitive” types– integer– double– float– char– String (actually not primitive)

Page 3: Introduction to Objects A way to create our own types

We want to do better

• Bank Account• Math Calculator• Stock • Car• Vending machine• Telephone

• Model “whatever” as software• Make a computer act like one

Page 4: Introduction to Objects A way to create our own types

Use type to create a variable

Use class to create an object

int x;

Circle mycircle = new Circle();

Page 5: Introduction to Objects A way to create our own types

Consider methods• Take a known process

• Package it for reuseif ((a >= b) && (a>=c)) max = aelse if ((b >= a) && (b>=c)) max = belse max = c;

int maxof3(int a, int b, int c){ int max; if ((a >= b) && (a>=c)) max = a else if ((b >= a) && (b>=c)) max = b else max = c; return max;}

Page 6: Introduction to Objects A way to create our own types

How are object like this?

• Take a group of methods and data and package those for reuse.

a( )

b( ) c( )

int i

int k a( )

b( ) c( )

int i

int k

class Testclass

Page 7: Introduction to Objects A way to create our own types

int i;int j;

void a(){…}

void b(){…}

void c(){…}

class TestClass{int i;int j;

void a() {…}

void b() {…}

void c() {…}

}

Page 8: Introduction to Objects A way to create our own types

How do we decide what goes inside?

• The problem will guide us.

• The things we put inside will define – What the object will do– How we can interact with it

• These things will be the “Bank Account”s, “Student”s, etc

Page 9: Introduction to Objects A way to create our own types

Let’s start simple

A circle

Page 10: Introduction to Objects A way to create our own types

What are some of the attributes of a circle?

• Radius (most obvious)

• Color

• Border

• Position

Page 11: Introduction to Objects A way to create our own types

How do we interact with a circle?

• Change it’s size

• Move it

• Ask it for it’s area

• … depending on the problem’s needs

Page 12: Introduction to Objects A way to create our own types

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 13: Introduction to Objects A way to create our own types

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

}

Page 14: Introduction to Objects A way to create our own types

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

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

}

Heading for the class

Page 15: Introduction to Objects A way to create our own types

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

}

A property ofeach circle

Page 16: Introduction to Objects A way to create our own types

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

}

A method named Areathat will calculate thearea of that specific circle

Page 17: Introduction to Objects A way to create our own types

Circle()

class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

}

A constructorUsed to initialize the circleLet’s see how in the next slide

Page 18: Introduction to Objects A way to create our own types

Creating circles

Circle circle1 = new Circle(10);

Radius:10

circle1

Page 19: Introduction to Objects A way to create our own types

Creating circles

Circle circle1 = new Circle(10);

radius:10

circle1

Circle circle2 = new Circle(15);

radius:15

circle2

Page 20: Introduction to Objects A way to create our own types

Asking about area

Circle circle1 = new Circle(10);System.out.println(circle1.Area());

radius:10

circle1

Circle circle2 = new Circle(15);System.out.println(circle2.Area());

radius:15

circle2

Each circle will return it’s own area

Page 21: Introduction to Objects A way to create our own types

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 22: Introduction to Objects A way to create our own types

What’s the difference?

• Circle circle1 = new Circle(10);

• Circle circle1;

Creates the objectand defines the reference to the objectIn this case, circle1 actually refers to a realCircle.

Page 23: Introduction to Objects A way to create our own types

Only a reference.. No circle

Circle circle1;

circle1

Page 24: Introduction to Objects A way to create our own types

A reference with a circle

Circle circle1 = new Circle(10);

Radius:10

circle1

Page 25: Introduction to Objects A way to create our own types

Put it Together!public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { Circle circle1 = new Circle(10); System.out.println(circle1.Area());

Circle circle2 = new Circle(15); System.out.println(circle2.Area()); }

}

314.1562..

706.8583..

Page 26: Introduction to Objects A way to create our own types

Do I need new()?Can I define a variable and just

reference from the main?How about this?

… TRY IT…

public class Circle() {

double radius=5;

public static void main(String args[]) { System.out.println(radius); }

}

Error: non-static variable radius cannotbe referenced from a static context

You never “new()”ed one. No radius exists.

Page 27: Introduction to Objects A way to create our own types

Only a reference.. No circle

Circle circle1;

circle1

Page 28: Introduction to Objects A way to create our own types

Do I need new()?Here there is no Area() or radius defined.

Because NO new() has occurred!

public class Circle() {

double radius=5;

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

public static void main(String args[]) { System.out.println(Area()); }

}

Error : nonstatic method can not be referenced from static method

Page 29: Introduction to Objects A way to create our own types

This one creates the object.Then a radius and Area() exists

to use… no errors.

public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { Circle circle1 = new Circle(10); System.out.println(circle1.Area());

Circle circle2 = new Circle(15); System.out.println(circle2.Area()); }

}

These are created when you new()

Page 30: Introduction to Objects A way to create our own types

public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i);

Circle circle1 = new Circle(10); System.out.println(circle1.Area()); }

}

Why can I declare “i”like this in the main,but not radius in the previous example?

This is legal!

Page 31: Introduction to Objects A way to create our own types

public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i);

Circle circle1 = new Circle(10); System.out.println(circle1.Area()); }

}

Because main is static.Static needs more explanation.

Page 32: Introduction to Objects A way to create our own types

Conclusion:

“new” before using

static is coming!