objects
DESCRIPTION
Objects. Pepper many references from http://journals.ecs.soton.ac.uk/java/tutorial/java/objects/object.html. Objects. Look around you and see objects – desk, people, blackboard, pen, eraser State – hair color, eye color, name, arm length, mouth expression Behavior – move arm; speak; see. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/1.jpg)
Objects
Peppermany references from
http://journals.ecs.soton.ac.uk/java/tutorial/java/objects/object.html
![Page 2: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/2.jpg)
Objects
• Look around you and see objects – desk, people, blackboard, pen, eraser
• State – hair color, eye color, name, arm length, mouth expression
• Behavior – move arm; speak; see
![Page 3: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/3.jpg)
variables and methods
![Page 4: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/4.jpg)
Bicycle object
![Page 5: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/5.jpg)
Objects you use
• Scanner
• Random
ABC System.in
nextInt
nextDouble
next
![Page 6: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/6.jpg)
Class blueprint Instance
• Scanner x = new Scanner(System.in)
• int y = x.nextInt;
nextInt
nextDouble
next
Scanner Class
System.in 1223
nextInt
nextDouble
next
x
Input &Type
![Page 7: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/7.jpg)
People class
say
moveArm
blink
People Class
mary
hair coloreye colorarm length
say
moveArm
blinkbrowngreen11
carrie
say
moveArm
blinkblondblue5
![Page 8: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/8.jpg)
Creating a blueprint
• Add instance variables inside class and outside methods
public class Student{
private String hairColor;private String eyeColor;private int armLength;
/**}
![Page 9: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/9.jpg)
Creating a blueprint
• remove static from methods
public void printPerson (){
System.out.println ("The eye color is "+ eyeColor + " and the hair color is " + hairColor);
}
![Page 10: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/10.jpg)
Use the blueprint
Student mary = new Student();
Student carrie = new Student();
mary.setEyeColor("green");
mary.setHairColor("brown");
carrie.setEyeColor("blue");
carrie.setHairColor("blonde");
![Page 11: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/11.jpg)
Look at the class again - thispublic void setEyeColor(String colorin) { this.eyeColor = colorin; }
mary
say
moveArm
blinkbrowngreen11
carrie
say
moveArm
blinkblondblue5
![Page 12: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/12.jpg)
Exercise – make a point class
• setxy – take in 2 int to set x and y
• getx – return x• gety – return y• getDistance – take in
1 point and return distance to it
Point
gety
getx
setxyxy
getDistance 2122
12 yyxx
![Page 13: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/13.jpg)
Step 1: Class with Variables
Create a class called point
Create 2 class instance variables
int x
int ypublic class Point{ int x; int y;}(Note, you can create int x = 1; if you want 1 to be the default value when the Point is created.
![Page 14: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/14.jpg)
2: Create a method to set x, y
Method name: setxyInput: int for x, int for yOutput: noneRequires class instance: Yes
public void setxy (int xIn, int yIn) { this.x = xIn; this.y = yIn;}
![Page 15: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/15.jpg)
3: Create a method to get the x
Method name: getxInput: noneOutput: int (the x value of the point)Requires class instance: Yes
public int getx() { return this.x;}
public int gety() { return this.y;}
![Page 16: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/16.jpg)
4: Create a method to get Distance
Method name: getDistanceInput: another pointOutput: double representing distanceRequires class instance: YesProcess:
2122
12 yyxx
![Page 17: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/17.jpg)
4: Create a method to get Distance
public double getDistance(Point another) { int x1 = another.getx(); int y1 = another.gety();
return Math.sqrt((this.x-x1) * (this.x-x1) + (this.y-y1) * (this.y-y1)); }
2122
12 yyxx
![Page 18: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/18.jpg)
Make a point public class Point{ int x; int y; public void setxy (int xIn, int yIn) { this.x = xIn; this.y = yIn;} public int getx() { return this.x;} public int gety() { return this.y;} public double getDistance(Point another) { return Math.sqrt((this.x-another.getx()) * (this.x-another.getx()) + (this.y-another.gety()) * (this.y-another.gety())); }}
![Page 19: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/19.jpg)
Use your point class
• create 2 points
• print the distance between them
![Page 20: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/20.jpg)
PointUse
public class PointUse{ public static void main() { Point one = new Point(); Point two = new Point(); one.setxy(1,2); two.setxy(4,6); System.out.println(one.getDistance(two)); }}
![Page 21: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/21.jpg)
Player class
setName
moveMan
getLoc
Player Class
locationnamescore
setLoc reportWin
setName
moveMan
getLoc
mary
30mary100
setLoc reportWin
setName
moveMan
getLoc
carrie
40carrie30
setLoc reportWin
![Page 22: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/22.jpg)
exercise – make a player
• Make a player that knows its own – location - int– name - String– score - int
setName
moveMan
getLoc
Player Class
locationnamescore
setLoc reportWin
![Page 23: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/23.jpg)
Teach that player how to do things
• setName – take a string to change name• setLoc – take an int to change location• getLoc – return its loc• reportWin – print win if > 30• moveMan – take in int - amount to move; int - current loc ; return new loc
setName
moveMan
getLoc
Player Class
locationnamescore
setLoc reportWin
![Page 24: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/24.jpg)
Player classpublic class Player{
int location; String name; int score;
public void setName(String namein){ this.name = namein;}public void setLoc(int locin){ this.location = locin;}public int setLoc(){ return this.location; }public boolean reportWin(){if (this.location > 30) { System.out.println(name + " won."); return true;} else {return false;}} public int moveMan(int move){ this.location = this.location+move; return this.location;}}
![Page 25: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/25.jpg)
Exercise continued - Make a game class
• Main method
• Create 2 players
• Tell players their names
• Tell players to start on 0
• Tell players to move some amounts
• Ask each player if it won
![Page 26: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/26.jpg)
Game classpublic class Game1{ public static void main() { Player player1 = new Player(); Player player2 = new Player(); player1.setLoc(0); player2.setLoc(0); player1.setName("mary"); player2.setName("carrie"); player1.moveMan(15); player2.moveMan(13); player1.moveMan(12); player2.moveMan(20); player1.reportWin(); player2.reportWin(); }}
![Page 27: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/27.jpg)
Summary So Far• Classes as Blueprints vs Instances
– Variable of a class type – hold instance– Create instances with new Class ()
• Behavior – methods– placement: same as other methods, not static– access: instance.method
• State – instance variables – placement: in class; outside method; – access: this.– scope: throughout class
![Page 28: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/28.jpg)
Constructors
• Creates a new instance (a new object)
• Can take in parameters so the new object has information set inside it right away
• Special method– No return– Same name as the class
• Call it with "new" keyword
![Page 29: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/29.jpg)
Point class constructorpublic class Point
{
int x;
int y;
//============ Constructor
public Point(int x, int y)
{ this.x = x;
this.y = y;
}
![Page 30: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/30.jpg)
Use the Point Constructor
Point myPoint = new Point(1,3);
Point yourPoint = new Point(4,5);
myPoint
gety
getx
setxy13
getDistance
yourPoint
gety
getx
setxy45
getDistance
![Page 31: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/31.jpg)
Constructors vs Methods• Differences between methods and constructors.
– There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value.
– There is no return statement in the body of the constructor.
http://www.leepoint.net/notes-java/oop/constructors/constructor.html
![Page 32: Objects](https://reader035.vdocuments.mx/reader035/viewer/2022062723/56813b47550346895da42c18/html5/thumbnails/32.jpg)
coming soon
• Class variables - shared variables among instances
• Class constants - variables that do not change for the class
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html