Syntax Reference Sheet
Syntax Reference Sheet
sc.next(), sc.nextLine() read/return the next token (word) or entire line of input as a string
sc.nextInt(), sc.nextDouble() read/return the next token of input as an int or double
sc.hasNext(), sc.hasNextLine(), ask about whether a next token/line exists, or
sc.hasNextInt(), sc.hasNextDouble() what type it is, without reading it
sc.close() closes the scanner
ConsoleProgram
public class Name extends ConsoleProgram { ... }
readInt("prompt"), Prompts/reprompts for a valid int or double, and returns it
readDouble("prompt")
readLine("prompt"); Prompts/reprompts for a valid String, and returns it
readBoolean("prompt", Prompts/reprompts for either yesString or noString (case-insensitive).
"yesString", "noString"); Returns true if they enter yesString, false if they enter noString.
promptUserForFile("prompt", Prompts for a filename, re-prompting until input is a file that exists in the
"directory"); given directory. Returns the full file path (“directory/filename”).
println("text"); Prints the given text to the console, followed by a newline (‘\n’).
print("text"); Prints the given text to the console.
GraphicsProgram
public class Name extends GraphicsProgram { ... }
add(shape); displays the given graphical shape/object in the window
add(shape, x, y); displays the given graphical shape/object in the window at x, y
getElementAt(x, y) returns graphical object at the given x/y position, if any (else null)
getHeight(), getWidth() the height and width of the graphical window, in pixels
pause(ms); halts for the given # of milliseconds
remove(shape); removes the graphical shape/object from window so it will not be seen
setCanvasSize(w, h); sets canvas’s onscreen size
setBackground(color); sets canvas background color
Colors
rect.setColor(Color.BLUE);
Color.BLACK, BLUE, CYAN, GRAY, GREEN, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW
Color name = new Color(r, g, b); // red, green, blue from 0-255