introduction to objects
Download
Skip this Video
Download Presentation
Introduction to Objects

Loading in 2 Seconds...

play fullscreen
1 / 32

Introduction to Objects - PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on

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). We want to do better. Bank Account Math Calculator Stock

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Objects' - karan


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to objects

Introduction to Objects

A way to create our own types

type and variables
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)
we want to do better
We want to do better
  • Bank Account
  • Math Calculator
  • Stock
  • Car
  • Vending machine
  • Telephone
  • Model “whatever” as software
  • Make a computer act like one
use type to create a variable
Use type to create a variable

intx;

Use class to create an object

Circlemycircle=newCircle();

consider methods
Consider methods
  • Take a known process
  • Package it for reuse

if ((a >= b) && (a>=c))

max = a

else if ((b >= a) && (b>=c))

max = b

else

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;

}

how are object like this
How are object like this?
  • Take a group of methods and data and package those for reuse.

class Testclass

int i

int i

int k

a( )

int k

a( )

b( )

b( )

c( )

c( )

slide7
int i;

int j;

class TestClass{

int i;

int j;

void a()

{…}

void b()

{…}

void c()

{…}

}

void a()

{…}

void b()

{…}

void c()

{…}

how do we decide what goes inside
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
what are some of the attributes of a circle
What are some of the attributes of a circle?
  • Radius (most obvious)
  • Color
  • Border
  • Position
how do we interact with a circle
How do we interact with a circle?
  • Change its size
  • Move it
  • Ask it for its area
  • … depending on the problem’s needs
let s start with a simple circle class
Let’s start with a simple Circle class
  • Just a radius
    • No borders or colors
  • A means of asking it for its area.
  • This will serve as the basis (a type or class) for creating lots of circles
circle
Circle()

class Circle {

double radius;

Circle(double r)

{ radius = r;

}

double Area()

{ double thisarea =

radius*radius*Math.PI;

return thisarea;

}

}

circle1
Circle()

Heading for the class

class Circle {

double radius;

Circle(double r)

{ radius = r;

}

double Area()

{ double this area =

radius*radius*Math.PI;

}

}

circle2
Circle()

class Circle {

double radius;

Circle(double r)

{ radius = r;

}

double Area()

{ double thisarea =

radius*radius*Math.PI;

return thisarea;

}

}

A property of

each circle

circle3
Circle()

class Circle {

double radius;

Circle(double r)

{ radius = r;

}

double Area()

{ double thisarea =

radius*radius*Math.PI;

return thisarea;

}

}

A method named Area

that will calculate the

area of that specific circle

circle4
Circle()

class Circle {

double radius;

Circle(double r)

{ radius = r;

}

double Area()

{ double thisarea =

radius*radius*Math.PI;

return thisarea;

}

}

A constructor

Used to initialize the circle

Let’s see how in the next slide

creating circles
Creating circles

Circle circle1 = new Circle(10);

circle1

Radius:10

creating circles1
Creating circles

Circle circle1 = new Circle(10);

circle1

radius:10

Circle circle2 = new Circle(15);

circle2

radius:15

asking about area
Asking about area

Circle circle1 = new Circle(10);

System.out.println(circle1.Area());

circle1

radius:10

Each circle will return

it’s own area

Circle circle2 = new Circle(15);

System.out.println(circle2.Area());

circle2

radius:15

what s the difference
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.

what s the difference1
What’s the difference?
  • Circle circle1 = new Circle(10);
  • Circle circle1;

Creates the object

and defines the

reference to the object

In this case, circle1

actually refers to a real

Circle.

only a reference no circle
Only a reference.. No circle

Circle circle1;

circle1

a reference with a circle
A reference with a circle

Circle circle1 = new Circle(10);

circle1

Radius:10

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());

}

}

Put it Together!

314.1562..

706.8583..

do i need new can i define a variable and just reference from the main how about this try it
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 cannot

be referenced from a static context

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

only a reference no circle1
Only a reference.. No circle

Circle circle1;

circle1

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());

}

}

Do I need new()?Here there is no Area() or radius defined.Because NO new() has occurred!

Error : nonstatic method can not

be referenced from static method

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());

}

}

This one creates the object.Then a radius and Area() exists to use… no errors.

These are created

when you new()

slide30
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!

slide31
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.

ad