Code.org Completion
I have completed Code.org unit 1! Here I will share some of what I have learned.
public class PainterPlus extends Painter{
public PainterPlus() {
super();
}
public void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
public void move(int blocks) {
for(int i = 0; i < blocks; i++) {
move();
paint("white");
}
}
}
In this example, 'PainterPlus' is a subclass of the superclass 'Painter'. PainterPlus extends Painter, meaning PainterPlus inherits the characteristics of the preset class Painter. With this subclass, I can create constructors and methods to alternate the actions of the Painter.
Lines 2 and 3 represent the constructor signature. Constructors are a special type of method invoked to create objects from a class.
'turnRight' and 'move' represent methods, or blocks of codes that only run when called upon.
public class MyNeighborhood {
public static void main(String[] args) {
// Lesson 6 Level 3
// TO DO #1: Instantiate a PainterPlus object.
PainterPlus myPainterPlus = new PainterPlus();
myPainterPlus.move(3);
myPainterPlus.turnRight();
myPainterPlus.move(2);
// Lesson 7 Level 2
// TO DO #1: Navigate the PainterPlus object
// to the traffic cone.
}
}
Here, I have instantiated an object called "myPainterPlus", which has a behavior displayed upon by the objects. The object follows the given commands written below it.