24su ver.
Note: this is for the Summer 2024 iteration of CSE 121. Looking for a different quarter? Please visit https://courses.cs.washington.edu/courses/cse121/.
Structure of a Java program¶
public class Name {
public static void main(String[] args) {
statement;
// more statements here...
statement;
}
}
Printing¶
(displays output in the console window)
Code | Description |
---|---|
System.out.print("text"); | prints text |
System.out.println("text"); | prints text and moves to a new line |
System.out.println(); | prints a blank line |
Comments¶
(notes written in the program for the programmer to read)
/* text
* (multiple lines)
*/
// text (one line)
Example
/* CSE 121, Stan Smith
* This program prints the steps of my favorite recipe.
*/
// This code displays the preamble of the US Constitution.
Turtle Graphics¶
(allows you to draw graphics on a window)
Turtle donatello = new Turtle();
Example
Turtle donatello = new Turtle();
donatello.forward(100);
Turtle Methods¶
Method | Description |
---|---|
donatello.forward(x); | moves turtle forward by a distance of x pixels |
donatello.left(deg); | turns turtle left by deg degrees |
donatello.right(deg); | turns turtle right by deg degrees |
donatello.up(); | lifts pen up and stops drawing |
donatello.down(); | puts pen back down and starts drawing |
donatello.penColor(color); | sets pen color (String or Java Color) options: black, blue, cyan, gray, green, magenta, orange, pink, red, yellow |
Example
Turtle donatello = new Turtle();
donatello.forward(100);