Download presentation
Presentation is loading. Please wait.
Published byShawn Rice Modified over 9 years ago
1
Review Creating Objects Pepper many references from http://journals.ecs.soton.ac.uk/java/tuto rial/java/objects/object.html http://journals.ecs.soton.ac.uk/java/tuto rial/java/objects/object.html
2
Objects you use Scanner Random ABC System.in nextInt nextDouble next
3
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
4
People class say moveArm blink People Class mary hair color eye color arm length say moveArm blink brown green 11 carrie say moveArm blink blond blue 5
5
Creating a blueprint Add instance variables – inside class and outside methods public class Student { private String hairColor; private String eyeColor; private int armLength; /** }
6
Creating a blueprint remove static from methods public void printPerson () { System.out.println ("The eye color is "+ eyeColor + " and the hair color is " + hairColor); }
7
Use the blueprint Student mary = new Student(); Student carrie = new Student(); mary.setEyeColor("green"); mary.setHairColor("brown"); carrie.setEyeColor("blue"); carrie.setHairColor("blonde");
8
Look at the class again - this public void setEyeColor(String colorin) { this.eyeColor = colorin; } mary say moveArm blink brown green 11 carrie say moveArm blink blond blue 5
9
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 setxy xyxy getDistance
10
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())); }}
11
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)); }}
12
Player class setName moveMan getLoc Player Class location name score setLoc reportWin setName moveMan getLoc mary 30 mary 100 setLoc reportWin setName moveMan getLoc carrie 40 carrie 30 setLoc reportWin
13
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 location name score setLoc reportWin
14
Player class public 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;} }
15
Game class public 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(); }}
16
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
17
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
18
Point class constructor public class Point { int x; int y; //============ Constructor public Point(int x, int y) { this.x = x; this.y = y; }
19
Use the Point Constructor Point myPoint = new Point(1,3); Point yourPoint = new Point(4,5); myPoint gety getx setxy 1313 getDistance yourPoint gety getx setxy 4545 getDistance
20
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
21
What is wrong with this code public class usePoint { public static void main(){ Point myPoint1; Point myPoint2; System.out.println(myPoint1.getX()); System.out.println(myPoint1.getDistance(my Point2)); }}
22
Summary So Far2 Classes as Blueprints vs Instances –Variable of a class type – hold instance –Create instances with new Class () – default –Create instances with new Class (cons parms) Behavior – methods –placement: same as other methods, not static –access: instance.method –Special method: constructor State – instance variables –placement: in class; outside method; –access: this. –scope: throughout class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.