Say I have the Class Sheep and the 2 parameters 'speed' and 'rotation'. Speed is optional, rotation is not. How do I use Constructor Overloading to avoid nulling? I can't really get it to work.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Sheep extends Actor { private int speed,rot; public Sheep( int rotation) { rot = rotation; speed = 0 //default value } public Sheep( int rotation, int sheepSpeed) { this (rotation); speed = sheepSpeed; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public Sheep() { this ( 0 , 0 ); } public Sheep( int rotation) { this (rotation, 0 ); } public Sheep( int rotation, int sheepSpeed) { rot = rotation; speed = sheepSpeed; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import greenfoot.*; public class Sheep extends Actor { private int speed,rot; public Sheep( int rotation) { rot = rotation; } public Sheep( int rotation, int sheepSpeed) { this (rotation); speed = sheepSpeed; } } |