HW02 - james-bern/CS136 GitHub Wiki

image

README

In this challenging homework, we will learn how to use arrays and for loops by implementing a text input field.

Background

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 (or CONTROL + 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?

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[]).

Java

cast

  • 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)

char's are integers

  • char's are integers (but they are NOT int's)
  • We can use a cast to find a char's "integer equivalent"
    • (int)('A') is 65
    • (int)('0') is 48
  • NOTE: The char's are "in order"
    • (int)('A') is 65
    • (int)('B') is 66
    • (int)('C') is 67
    • ...
    • (int)('a') is 97
    • (int)('b') is 98
    • (int)('c') is 99
    • ...
  • We can add char's and subtract char's
    int 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'

Data

  • char[] buffer; stores the user's message as an array of characters
    • buffer can store a message of length at most buffer.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
  • int cursor; is the current cursor position
    • 0 means all the way to the left
    • 1 means in between buffer[0] and buffer[1]

Geometry

  • 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}$
  • example
    • here is the message COW written in a monospaced font
      • cursor is 3
      • buffer.length is 4
      #####   #####   #   # |
      #       #   #   #   # |
      #       #   #   # # # |
      #####   #####    # #  |
    0       1       2       3       4   -> x
    
  • the bottom of the canvas is $y = -1.0$ and the top is $y = 2.0$

User Interface

  • All features must match the behavior of a typical text input field
    • For example, pressing BACKSPACE should turn CO|W (length 3) into C|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

TODO's

  • A-

    • User can press A-Z (any of the keys A, 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 ' '
    • User can press LEFT_ARROW
    • User can press RIGHT_ARROW
    • User can press BACKSPACE
  • A

    • Make the cursor bar blink
      • NOTE: you will want to introduce a frame or time variable to keep track of the current frame / time
    • Click the mouse to move cursor to the closest int
    • If the user presses ENTER while the buffer contains Passw0rd, replace the contents of the buffer with accepted
      • HINT: some repetition here is OK
  • 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
    • Double-click the mouse to select all characters
    • Press COMMAND + A or CONTROL + A to select all characters
      • NOTE: we want our app to be "cross platform" (work on both Mac and Windows)
  • A++

    • Build a full raw text editor like TextEdit/Notepad

NOTE's

  • 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 for c == 'A', c == 'B', c == 'C', ...)
  • You may NOT have any out of bounds errors

Starter Code

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);
        }
    }
}

Hints

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'
⚠️ **GitHub.com Fallback** ⚠️