HW02 - james-bern/CS136 GitHub Wiki
In this challenging homework, we will learn how to use arrays and for loops by implementing a text input field.
A text input field allows a user to type a message inside of a computer program. You've used these things your whole life, but probably haven't given them much thought.
Let's change that. 🙂👍
- Try playing around with one of the search bars on this web page.
- What happens when you...
- press the
B
key on your keyboard? - press
SHIFT + B
? - press the
LEFT_ARROW_KEY
? - press the
RIGHT_ARROW_KEY
? - press
BACKSPACE
? - press
COMMAND + A
(orCONTROL + A
on Windows)? - press the left mouse button?
- hold the mouse and drag?
- press
B
while some text is highlighted? - press
BACKSPACE
while some text is highlighted? - double-click?
- press the
- What happens when you...
We need to store a sequence of characters that we'll be changing a lot.
We will use an array, specifically an array of characters (char[]
).
- We can cast variables from one type to another
- The syntax
(Bar)(foo)
"cast foo to type Bar" - NOTE: Casting is a "special tactic" in modern Java; we will only ever use it on this lab (and even then, probably only once)
- The syntax
-
char
's are integers (but they are NOTint
's) - We can use a cast to find a
char
's "integer equivalent"-
(int)('A')
is65
-
(int)('0')
is48
-
- NOTE: The
char
's are "in order"-
(int)('A')
is65
-
(int)('B')
is66
-
(int)('C')
is67
- ...
-
(int)('a')
is97
-
(int)('b')
is98
-
(int)('c')
is99
- ...
-
- We can add
char
's and subtractchar
'sint i = ('B' - 'A'); // 1 // NOTE: We needed to "cast to a char" because, in Java, a char plus an int is an int char c = (char) ('c' + i); // 'd' ++c; // 'e'
-
char[] buffer;
stores the user's message as an array of characters-
buffer
can store a message of length at mostbuffer.length
-
-
int length;
is the length of the current message stored in buffer- if
(length == 0)
then message is empty - if
(length == buffer.length)
then the message completely fills the buffer
- if
-
int cursor;
is the current cursor position-
0
means all the way to the left -
1
means in betweenbuffer[0]
andbuffer[1]
-
- the left side of the canvas is
$x = 0.0$ - the right side of the canvas is
$x = \texttt{buffer.length}$ - we are using a monospaced font; this means each character is the same width;
- for simplicity, we have each character be
$1.0$ unit wide -
buffer[0]
is drawn from$x = 0.0$ to$x = 1.0$ -
cursor
should be drawn at$x = \texttt{cursor}$
- for simplicity, we have each character be
-
example
- here is the message
COW
written in a monospaced font-
cursor
is3
-
buffer.length
is4
-
##### ##### # # | # # # # # | # # # # # # | ##### ##### # # | 0 1 2 3 4 -> x
- here is the message
- the bottom of the canvas is
$y = -1.0$ and the top is$y = 2.0$
- All features must match the behavior of a typical text input field
- For example, pressing
BACKSPACE
should turnCO|W
(length 3) intoC|W
(length 2); NOTE: the|
is the cursor - You are responsible for discovering what the correct behavior of all commands is by experimenting with a real-world text input field
- For example, pressing
-
A-
- User can press
A-Z
(any of the keysA
,B
, ...) - User can press
0-9
- User can press
SHIFT + A
,SHIFT + B
... - Draw the cursor bar (location of
cursor
as a vertical line) - User can press the space bar
- HINT: this is the character
' '
- HINT: this is the character
- User can press
LEFT_ARROW
- User can press
RIGHT_ARROW
- User can press
BACKSPACE
- User can press
-
A
- Make the cursor bar blink
- NOTE: you will want to introduce a
frame
ortime
variable to keep track of the current frame / time
- NOTE: you will want to introduce a
- Click the mouse to move
cursor
to the closestint
- If the user presses
ENTER
while the buffer containsPassw0rd
, replace the contents of the buffer withaccepted
- HINT: some repetition here is OK
- Make the cursor bar blink
-
A+
- Click and drag the mouse to select/highlight a sequence of characters
- HINT:
int select = 0;
- NOTE: pressing
BACKSPACE
while characters are selected must do the right thing - NOTE: pressing
A
,B
, ... should also do the right thing
- HINT:
- Double-click the mouse to select all characters
- Press
COMMAND + A
orCONTROL + A
to select all characters- NOTE: we want our app to be "cross platform" (work on both Mac and Windows)
- Click and drag the mouse to select/highlight a sequence of characters
-
A++
- Build a full raw text editor like TextEdit/Notepad
- You may NOT use any fancy Java standard library functions; stick to basic for loops, if statements, and the functions inside Cow.java
- You may NOT use the Java
String
class anywhere. - You may NOT create any new arrays (just modify the contents of
buffer
) - You may NOT use excessive repetition (for example, don't use separate
if
statements forc == 'A'
,c == 'B'
,c == 'C'
, ...) - You may NOT have any out of bounds errors
class HW02 extends Cow {
public static void main(String[] arguments) {
char[] buffer = new char[8];
int length = 0;
int cursor = 0;
canvasConfig(0.0, -1.0, buffer.length, 2.0, WHITE, 512);
while (beginFrame()) {
// TODO
HW02_drawText(buffer, length, BLACK);
}
}
}
Iterate over the characters A-Z
for (char c = 'A'; c <= 'Z'; ++c) { ... }
Convert char
from uppercase to lowercase
(char)('a' + (c - 'A'))
-
Example:
('D' - 'A') -> 3
and(char) ('a' + 3) -> 'd'