💻
Coding for Creatives Spring 2021
  • Hello
  • Class Exercises
    • Week 2 (Variable) Activities
      • Getting Blobby (Processing)
      • Unity Activity Notes
      • TouchDesigner Activity Notes
      • More Variable Activities
        • Star Wars Name Exercise
        • Mad Libs Exercise
    • Week 3 (If/Else Statements) Activities
      • Rubber Ducky Activity (Unity)
    • Week 4 (Loops) Activities
      • Using For and Foreach Loops in Unity
      • More Loop Activities
        • Movement Using If Statements In Processing
        • Moving Shapes in Processing
    • Week 5 (Arrays & Lists) Activities
      • Moving Squares Activity
    • Week 6 (Functions)
      • Mario Activity
      • Raycasting (Unity)
      • More Functions Activities
        • Snake (Processing)
    • Week 8 (Randomness & Object Interaction)
      • Bouncing Ball (Unity)
    • Week 9 (Creating Custom Objects)
      • Creating Custom Objects (Processing)
      • Cannon Game (Unity)
    • Week 10 (Sensors & TouchDesigner)
      • TouchOSC
      • More Activities
        • Audio-Reactive 3D Shapes
  • Glossary and Terms
    • Blank Space / Canvas Origin
    • Shapes / Primitives
    • Color
    • Movement
    • Control
    • Variables
      • Declaring Variables
      • Assigning a Variable
      • Using a Variable
      • Data Types
        • Floating Point or Float
        • Integer or Int
        • Boolean or Bool
        • String
      • Casting a Variable
    • Arrays
      • Creating Arrays
      • Assigning Values to Arrays
      • Using Array Elements
      • Multi-Dimensional Arrays
      • Common Errors
    • Conditional Statements (If/Else)
    • Operators
      • Relational Operators
      • Mathematical Operators
      • Logical Operators
    • Loops
      • While Loops
      • For Loops
      • Foreach Loops
    • Functions & Methods
      • Creating a Function
      • Calling or Invoking a Function
      • Parameters & Arguments
    • Interaction
      • Mouse Interaction
    • Objects, Classes & Libraries
      • Accessing Objects
    • Node-Based Programming
  • Processing Info
    • Overview
    • Interface
    • Template
    • Printing to the Console
  • Unity Info
    • Overview
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • TouchDesigner Info
    • Overview
    • Popular TOPs
    • Popular CHOPs
    • Popular SOPs
    • Popular DATs
  • Arduino Info
    • Arduinos!
    • The Parts
    • The Setup
      • Using the Serial Connection
      • Output Component Setup Examples
      • Input Component Setup Examples
      • Create a Night Light
  • Templates & Shortcuts
    • Power of Negative 1
    • Modifiers
    • Start Codes
      • Processing Start Code
      • HTML, CSS, JavaScript Start Code
    • WASD Movement Code
      • Processing Example
      • Unity Example (3D)
      • Unity Example (2D Platformer)
    • Parenting Example Script
  • Useful Links
    • Share & Show Off
    • Software & Apps
    • Free & Affordable Game Assets
Powered by GitBook
On this page
  • The Goal
  • Step 1: Create the Variables for the Four Steps
  • Step 2: Assign the Variables
  • Step 3: Use .substring() to Get a Portion of a String Variable
  • Using a function on an existing variable
  • Getting a Substring of a String
  • Step 4: Polish It a Little
  • Python Version

Was this helpful?

  1. Class Exercises
  2. Week 2 (Variable) Activities
  3. More Variable Activities

Star Wars Name Exercise

The steps below walk you through a Processing activity that uses variables and printing to the console. All code should allow you to copy and paste.

The Goal

To find out your "Star Wars Name."

According to the graphic that has been shared all over Facebook, that's:

First Name:

  1. Take the first three letters of your last name.

  2. Add the first two letters of your first name.

Last Name:

  1. Take the first two letters of your mother's last name. parent's first name

  2. Add the first three letters of the city state in which you were born.

I changed the last two since the info (if you used real info) can be used for identity theft.

Step 1: Create the Variables for the Four Steps

These will be strings.

String lastName;
String firstName;
String parentName;
String state;

void setup() {
}

void draw() {
}

Step 2: Assign the Variables

Normally, these would be assigned after prompting the user and storing their response. To pretend that happened, let's assign the variables in the setup() function.

String lastName;
String firstName;
String parentName;
String state;

void setup() {
    lastName = "Moore";
    firstName = "Hope";
    parentName = "Michael";
    state = "Ohio";
}

void draw() {
}

Step 3: Use .substring() to Get a Portion of a String Variable

To get parts of the strings (i.e. "first 3 letters" etc.).

Using a function on an existing variable

The two ways you will see a variable work in or with a function are:

First way: The variable is used within the parentheses as information being used Example:

String greeting = "Hello!";

void setup() {
    print(greeting);
}

Prints:

Hello!

Second Way:

The name of the variable followed by a period and the function name.

We will look further into this idea as we progress through the course. It's like starting with an object (whatever the variable stands for) and adding something to it OR digging deeper to get the object's properties.

Example using the built-in length() function that gets the number of characters in a string:

String mysteryWord;

void setup() {
    mysteryWord = "Booger";
    
    print("The mystery word has " + mysteryWord.length() + " letters.");
}

Prints:

The mystery word has 6 letters.

Getting a Substring of a String

We need to get the substrings of the variables we have.

Let's use the built-in .substring() function:

.substring(index of first letter) starts the string with the index/location of the first letter.

.substring(index of first letter, index of limit letter) starts the string with the index/location of the first letter and ends on the letter before the limit letter.

We'll use the second one since we need the beginning of each string. Remember to add a space between the second and third variables to show a separation between the first and last name.

Remember: The "index" is the position in a list (strings are lists of characters) and it always starts with ZERO.

String lastName;
String firstName;
String parentName;
String state;

void setup() {
  lastName = "Moore";
  firstName = "Hope";
  parentName = "Michael";
  state = "Ohio";
  
  println(lastName.substring(0, 3) + firstName.substring(0, 2) + " " + parentName.substring(0, 2) + state.substring(0, 2));
}

void draw() {
}

Prints:

MooHo MiOh

Step 4: Polish It a Little

You'll notice the names have capital letters in the middle of each. We can make those lowercase by using the .toLowerCase() function. Just attach it to the end of the substring() function code to add the effect.

String lastName;
String firstName;
String parentName;
String state;

void setup() {
  lastName = "Moore";
  firstName = "Hope";
  parentName = "Michael";
  state = "Ohio";
  
  println(lastName.substring(0, 3) + firstName.substring(0, 2).toLowerCase() + " " + parentName.substring(0, 2) + state.substring(0, 2).toLowerCase());
}

void draw() {
}

Prints:

Mooho Mioh

Python Version

Thanks to Nick Montana for providing an example in the discord last semester. This has been modified to be similar to the exercise above.

In this example:

  1. variables are defined at the top and given initial values.

  2. The substrings are put into their own variables.

  3. Variables are created for each name of the Star Wars name.

last = "Moore"
first = "Hope"
pfirst = "Michael"
plast = "Ohio"

# Substrings
swfirst1 = last[:3]
swfirst2 = first[:2]
parent = pfirst[:3]
state = plast[:3]

# Create the names
starwars_first = swfirst1 + swfirst2.lower()
starwars_last = parent + state.lower()

print(starwars_first + " + starwars_last)

What to look at:

In python, substrings are controlled with square brackets [ ] and within them, the beginning letter and the end letter don't need a number to represent them. The "to", as in "from this letter to this letter" is symbolized by a colon ( : )

The function to make the string lowercase is .lower()

PreviousMore Variable ActivitiesNextMad Libs Exercise

Last updated 4 years ago

Was this helpful?