Home

Test 2 Solutions

Write a keyPressed function that sets x and y to 0 whenver any key except w/a/s/d is pressed.

void keyPressed()
{
  if(key != 'w' && key != 'a' && key != 's' && key != 'w')
  {
    x = 0;
    y = 0;
  }
}

Write a part of the draw function that draws a circle centered on the mouse (diameter 10) whenever the mouse button is pressed (held down). Note that an "if" is required.

if(mousePressed)
{
  ellipse(mouseX, mouseY, 10, 10);
}

What is the first and last position of this array: int[] xyz = new int[37]; Answer: 0, 36.

Write code to define an array containing 1000 copies of the phrase "cse1211".

String[] phrases = new String[1000];
for(int i = 0; i < 1000; i++)
{
  phrases[i] = "cse1211";
}

Print out each value (using the println function) of an integer array called xs. Do not create the array; assume it already exists. Use a for loop. Recall that you can figure out the size of the array by writing xs.length

for(int i = 0; i < xs.length; i++)
{
  println(xs[i]);
}

Fill in the following blanks so that the for loop sets an integer k to the values 100, 99, ..., -99, -100.

for(int k = 100; k >= -100; k--)

Assume we have:

What type of values are the following?

Write a for loop that computes the produce 500495490...10*5. Save the product into a variable called "prod".

int prod = 1;
for(int x = 500; x > 0; x -= 5)
{
  prod *= x;
}

// this also works
int prod = 1;
for(int x = 500; x > 0; x--)
{
  prod *= x;
  x = x - 4;
}

Finish this code so that it finds the largest value in the array.

int[] xs = new int[200];
int largest = xs[0];
for(int i = 0; i < xs.length; i++)
{
  if(xs[i] > largest)
  {
    largest = xs[i];
  }
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.