39
© A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

Embed Size (px)

Citation preview

Page 1: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 2: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 3: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public Triangle(){ setSides(0,0,0);}

Constructors are similar to methods. Constructors set the properties of an object to an initial state.

Page 4: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public Triangle(int a, int b, int c){ setSides(a,b,c);}

Constructors are similar to methods. Constructors set the properties of an object to an initial state.

Page 5: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public void setSides(int a, int b, int c){ setSideA(a); //more of the same }

Modifier methods are methods that change the properties of an object.

Page 6: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public void setSideA(int a){ sideA=a;}

Modifier methods are methods that change the properties of an object.

Page 7: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public int getSideA(){ return sideA;}

Accessor methods are methodsthat retrieve or grant access tothe properties of an object, butdo not make any changes.

Page 8: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public String toString(){ return "" + getSideA() + //more get calls }

Accessor methods are methodsthat retrieve or grant access tothe properties of an object, butdo not make any changes.

Page 9: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

All data members should haveprivate access. The publicconstructors, accessor methods,and modifier methods should beused to manipulate the data. All data is tucked away nicelyinside the class.

Page 10: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

The public methods give you access to an object’s privatedata / properties.

getIt( )

setIt( )

toString( )

Class/Object

private data /instance variables /properties

Page 11: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 12: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 13: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

In Java, all classes are sub classes of class Object. This adds greater flexibility when writing programs in Java.

Object

String Date

Page 14: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

public class Monster extends Object{ public void print( ) { out.println("Monster"); }}

Page 15: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Because all classes are subclasses of Object, all classesstart with the same methods.

.equals( )

.toString( )

. . . . . and more

Page 16: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 17: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 18: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

All members with publicaccess can be accessedinside and outside of the class where they are defined.

Page 19: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

All members with privateaccess can only be accessedinside of the class wherethey are defined.

Page 20: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 21: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 22: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

If you do not provide any constructors, Java will provide a default constructor.

Page 23: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 24: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 25: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

The equals() method is used to seeif two objects have the same contents.

String one = "comp";String two = "sci";out.println(one.equals(two));

Page 26: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

class Monster{ private int height;

//methods

public boolean equals(Object obj){ Monster other = (Monster)obj; if(getHeight()==other.getHeight()) return true; return false; }

//methods}

//test code in the mainMonster one = new Monster(33);Monster two = new Monster(12);out.println(one.equals(two));

OUTPUTfalse

Page 27: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 28: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Overloading occurs when you have morethan one method or constructor with thesame name. Each method or constructormust have a different parameter list.

# of parameters && data types matter

Page 29: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

class Monster{ private int height; //default assinged to 0 private double weight; //default assinged to 0

public Monster(){ height=0; weight=0.0; }

public Monster(int ht){ height=ht; weight=0.0; }

public Monster(double wt){ height=0; weight=wt; }

public Monster(int ht, double wt){ height=ht; weight=wt; }}

Page 30: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 31: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Page 32: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

getX( ) - accessors

setX( ) - modifiers

Monster() - constructors

Monster

toString( ) - accessor

Page 33: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

class Monster{ //instance vars / data fields

public Monster(){ //code }

public void setX( params ){ //code }

public int getX(){ //code }

public String toString() { //code }}

Page 34: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

class Monster{ // instance variables

public Monster(){ code } public Monster( int ht ) { code } public Monster(int ht, int wt) { code } public Monster(int ht, int wt, int age) { code } //more methods}

Page 35: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Monster m = new Monster();

MONSTERProperties – height – 0 weight - 0 age - 0methods

m

m is a reference variable that refersto a Monster object.

Page 36: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Monster m = new Monster(23);

m is a reference variable that refersto a Monster object.

MONSTERProperties – height – 23 weight – 0 age - 0methods

m

0x234

0x234

Page 37: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Monster m = new Monster(23, 45);

m is a reference variable that refersto a Monster object.

MONSTERProperties – height – 23 weight – 45 age - 0methods

m0x239

0x239

Page 38: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com

Monster m = new Monster(23, 45, 11);

m is a reference variable that refersto a Monster object.

MONSTERProperties – height – 23 weight – 45 age - 11methods

m0x2B3

0x2B3

Page 39: © A+ Computer Science - . public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties

© A+ Computer Science - www.apluscompsci.com