Code.org learnings
- Java is case-sensitive
- Create an object (instance of a class) with 'ClassName newObject = new ClassName();' format
- Can invoke methods on objects with 'objectName.methodName();' format
- Methods are like functions/algorithms, you can store them in separate files and then call them in main file
- When doing so remember to extend the class
- if/while loop parameter format is 'if/when (objectName.condition())'
- Pay attention to indentation and formatting. Organization is key not only for compiling but also for human readability
import org.code.neighborhood.Painter;
public class MyNeighborhood {
public static void main(String[] args) {
// constructing an instance (object)
Painter myPainter = new Painter();
// how to use a method with an object
myPainter.move();
myPainter.takePaint();
myPainter.turnLeft();
myPainter.turnLeft();
myPainter.turnLeft();
myPainter.move();
}
}
import org.code.neighborhood.Painter;
// separate methods in PainterPlus file
public class PainterPlus extends Painter {
public PainterPlus() {
super();
}
// turnRight is actually rotating left 3 times
public void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
// using if and while loops, parameter format is 'objectName.givenCondition()'
public void takeAllPaint() {
if (myPainterPlus.isOnBucket()) {
while (myPainterplus.isOnBucket()) {
takePaint();
}
}
}
// mind indentation/formatting for code in method
public void paintLine(String color) {
while (myPainterPlus.canMoveForward()) {
paintColor();
myPainterPlus.moveForward();
}
}
}