Code.org Key Learnings
- Use of extends (from lesson 6)
- Creating Objects
- Using Objects
- Lesson 8
- Lesson 9
- Lesson 10
- Lesson 11
- Lesson 12
- Lesson 13
- The main method
- inheritance - an object-oriented programming principle where a subclass inherits the attributes and behaviors of a superclass
- subclass - a class that extends a superclass and inherits its attributes and behaviors
- superclass - a class that can be extended to create subclasses
Club Example
public class ClubOfficer extends ClubMember { //extends tells java that ClubOfficer class should inherit the attributes and behaviors of ClubMember class
public ClubOfficer() { //subclass constructor
super(); //to call the superclass constructor in subclass
}
}
PainterPlus Example
public class PainterPlus extends Painter {
public PainterPlus() {
super();
}
}
Creating PainterPlus
- Instantiate a PainterPlus object called myPainterPlus
- sytax: ClassName objectName = new ClassName();
public class MyNeighborhood {
public static void main(String[] args) {
// Lesson 6 Level 3
// TO DO #1: Instantiate a PainterPlus object.
PainterPlus myPainterPlus = new PainterPlus();
}
}
An object is instantiated using the following syntax:
ClassName objectName = new ClassName();
Example:
public class MyNeighborhood {
public static void main(String[] args) {
// TO DO #1: Instantiate a Painter object called myPainter.
Painter myPainter = new Painter();
myPainter.move();
}
}
We can use methods to nevigate the object.
For example, for the myPainter object, we can use the move() and turnLeft() methods to navigate the myPainter object to the traffic cone.
A method is called using the following syntax:
variableName.methodName();
OR
variableName.methodName(argument);
Example:
myPainterPlus.turnRight();
A method signature for a void method uses the following syntax:
public void methodName() {
// code to execute when the method is called
}
Example:
public void turnRight() {
turnLeft();
turnLeft();
turnLeft();
}
Lesson 9
- control structure - a conditional or iteration statement which affects the flow of a program
- iteration statement (loop) - a control structure that repeatedly executes a block of code
- while loop - a control structure which executes a block of code repeatedly as long as a condition is true
A while loop uses the following syntax:
while (condition) {
// code to execute while the condition is true
}
Example: (in PainterPlus.java)
public void takeAllPaint() {
while (isOnBucket()){
takePaint();
}
}
public void paintLine() {
while (hasPaint()) {
paint("white");
move();
}
}
Lesson 10
- NOT ( ! ) operator - a logical operator that returns true when the operand is false and returns false when the operand is true
- if-else statement (two-way selection statement) - specifies a block of code to execute when the condition is true and a block of code to execute when the condition is false
- logical operator - an operator that returns a Boolean value
An if-else statement uses the following syntax:
if (condition) {
// code to execute if the condition is true
}
else {
// code to execute if the condition is false
}
Problem: can't run the code if there is { after else
public void moveOrTakePaint(){
while (canMove()) {
if (!isOnBucket()) {
move();
}
else {
takeAllPaint();
}
}
code is working if there is no { after else
public void moveOrTakePaint(){
while (canMove()) {
if (!isOnBucket()) {
move();
}
else
takeAllPaint();
}
}
Update:
public void moveSouth() { while (canMove("south")) { if (!isFacingSouth()){ turnRight(); } else { move(); } } }
Lesson 11
- concatenation - joining two strings together
Printing to the console uses the following syntax:
System.out.print();
OR
System.out.println();
- System.out.print() - leaves the cursor at the end of the line that it printed
- System.out.println() - to print a line of text and then move the cursor to the next line
Problem: Inside the printStatus() method, write the code to print information about the state of a PainterPlus object
My code:
public void printStatus() { System.out.println(); }
how to print the state of the object??
public void printStatus() {
System.out.println("Paint: " + getMyPaint());
System.out.println("X Location: " + getX());
System.out.println("Y Location: " + getY());
System.out.println("Direction: " + getDirection());
}
The main method
The main method in Java is arguably the most important method – it is the entry-point into any Java program. The syntax is the same for any Java program. The JVM (Java Virtual Machine) executes Java byte code, and the main method must have specific syntax for the JVM to identify it and execute its contents.
public static void main(String[] args)
public: The method needs to be public for the JVM to identify it.
static: static in this context means that there will be only one type of this method and that it will be shared. When we call the main method, it does not require a new instantiated object – it can be called without the creation of a new object.
void: There is nothing returned from the main method.
main: The general signature of the main method that is identified by the JVM to execute any program from start to finish.
String[] args: The main method's argument (or input parameter) is an array of type String. This allows the method to accept command line arguments, which are stored as Strings in this variable. The name args can be changed.
Examples
This method will be identified by the JVM and will execute accordingly assuming there are no other errors.
public static void main(String [] args) { System.out.println("Main Method"); }
Syntax
public static void main(String[] args)