***
CS 106A MIDTERM SYNTAX REFERENCE ***
This document lists some of the common methods and syntax that you will use on the exam. For more, consult your textbook. (v1.3.2)
Karel the Robot (Karel reader Ch. 1-6)
public class Name extends SuperKarel { ... }
turnLeft(); turnRight(); turnAround(); rotates Karel 90º counter-clockwise, clockwise, or 180º
move(); moves Karel forward in current direction by one
square
pickBeeper(); picks up a beeper if present on Karel's corner; else
error
putBeeper(); places a beeper, if present in beeper bag; else error
frontIsClear(), frontIsBlocked() Is there a wall in front of Karel?
leftIsClear(), leftIsBlocked() Is there a wall to Karel's left (counter-clockwise)?
rightIsClear(), rightIsBlocked() Is there a wall to Karel's right (clockwise)?
beepersPresent(), noBeepersPresent() Are there any beepers on Karel's current corner?
beepersInBag(), noBeepersInBag() Are there any beepers in Karel's beeper bag?
facingNorth(), notFacingNorth(), Is Karel facing north, south, east, or west?
facingEast(), notFacingEast(),
facingSouth(), notFacingSouth(),
facingWest(), notFacingWest()
Math (A&S 5.1)
double d = [Link](2, 5); // 32.0
[Link](n), [Link](n), [Link](n), [Link](n), Math.log10(n),
[Link](a, b),[Link](a, b), [Link](b, e), [Link](n), [Link](n),
[Link](r), [Link](r), [Link](r), [Link](r), [Link](d)
RandomGenerator (A&S 6.1)
RandomGenerator rg = [Link]();
[Link]() returns a random true/false result;
[Link](probability) pass an optional probability from 0.0 - 1.0, or default to 0.5
[Link]() a randomly chosen Color object
[Link](min, max) returns a random real number between min and max, inclusive
[Link](min, max) returns a random integer between min and max, inclusive
String (A&S Ch. 8)
String s = "hello";
[Link](i) the character in this String at a given index
[Link](str) true if this String contains the other's characters inside it
[Link](str) true if this String ends with the other's characters
[Link](str) true if this String is the same as str
[Link](str) true if this String is the same as str, ignoring capitalization
[Link](str) first index in this String where given String begins (-1 if not found)
[Link](str) last index in this String where given String begins (-1 if not found)
[Link]() number of characters in this String
[Link](s1, s2) a new string with all occurrences of s1 changed to s2
[Link](str) true if this String begins with the other's characters
[Link](i, j) characters in this String from index i (inclusive) to j (exclusive)
[Link]() a new String with all lowercase or uppercase letters
[Link]()
Character/char (A&S Ch. 8)
char c = [Link]([Link](i));
[Link](ch), .isLetter(ch), methods that accept a char and return boolean values of
.isLowerCase(ch), .isUpperCase(ch), true or false to indicate whether the character is of the
.isWhitespace(ch) given type
[Link](ch), accepts a character and returns lower/uppercase version of
.toUpperCase(ch) it
Scanner
Scanner input = new Scanner(new File("filename")); // scan an input file
Scanner tokens = new Scanner(string); // scan a string
[Link](), [Link]() read/return the next token (word) or entire line of input as a string
[Link](), [Link]() read/return the next token of input as an int or double
[Link](), [Link](), ask about whether a next token/line exists, or
[Link](), [Link]() what type it is, without reading it
[Link]() 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
Graphical Objects (A&S Ch. 9)
GRect rect = new GRect(10, 20, 50, 70);
new GImage("filename", x, y) image from the given file, drawn at (x, y)
new GLabel("text", x, y) text with bottom-left at (x, y)
new GLine(x1, y1, x2, y2) line between points (x1, y1), (x2, y2)
new GOval(x, y, w, h) largest oval that fits in a box of#size w * h with top-left at (x, y)
new GRect(x, y, w, h) rectangle of size w * h with top-left at (x, y)
[Link](), [Link]() returns the color used to color the shape outline or interior
[Link](), [Link](), returns the left x, top y coordinates, width, and height of the shape
[Link](), [Link]()
[Link](dx, dy); adjusts location by the given amount
[Link](Color); sets overall window's background color
[Link](boolean); whether to fill the shape with color
[Link](Color); what color to fill the shape with
[Link](Color); what color to outline the shape with
[Link](x, y); change the object's x/y position
[Link](w, h); change the objects width*height size
Colors
[Link]([Link]);
[Link], BLUE, CYAN, GRAY, GREEN, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW
Color name = new Color(r, g, b); // red, green, blue from 0-255
Mouse Events (A&S Ch. 10)
public void eventMethodName(MouseEvent event) { ...
events: mouseMoved, mouseDragged, mousePressed, mouseReleased, mouseClicked, mouseEntered, mouseExited
[Link](), [Link]() the x or y-coordinate of mouse cursor in the window