Lucky Cat Tutoring Services


Making a Finch Count With ‘For’ Loop
October 6, 2009, 3:01 pm
Filed under: Finches, Java | Tags: , , , , ,

Here’s some code to make the CMU Finch count down from 10 and sing.

/**
 * Created by:
 * Date:
 * make baby Finch count and change nose color to a random tint with each number
 */

import finch.*;
import javax.swing.JOptionPane;
import java.util.Random;

public class finchCount
{

   public static void main(final String[] args)
   {
   // Instantiating the Finch object
      Finch baby = new Finch();
      Random spin = new Random();
      int count, redNum, greenNum, blueNum;
   String input; //name for what you’re asking for
  
   
  input = JOptionPane.showInputDialog(null, “What number do you want to count to?”);
  count = Integer.parseInt(input);
  
  for(int i = 1; i <= count; i++)//variable I; while I is lessthan/equal to COUNT, keep adding to I
          //never a ; here
  {
   baby.saySomething(“”+ i);
   baby.sleep(1000);
   //turn nose to random color
   redNum= spin.nextInt(256);//random num between 0 and 255
   greenNum= spin.nextInt(256);//random num between 0 and 255
   blueNum= spin.nextInt(256);//random num between 0 and 255
   baby.setLED(redNum,greenNum,blueNum);
   baby.sleep(1000);
   System.out.println(“number ” +i);
   
   
  }
      // Always end your program with finch.quit()
      baby.quit();
      System.exit(0);
    }
}

How about a fun nose  color change too?

This next program has the finch count to ten, say the number, and have it’s nose change color in relation to its counting.  The LED will change color in a gradient from dark to brighter blue

/**
 * Created by:
 * Date: 0910.06
 * in class looping problem with a finch
 */

import finch.*;

public class finchLoop01
{

   public static void main(final String[] args)
   {
   // Instantiating the Finch object
      Finch baby = new Finch();

 for(int i =1; i<= 10; i++) //while I is lessthan or equal to 10…to this, each round add 1
 {
  baby.saySomething(“The number is ” +i);
  System.out.println(“The number is “+i);
  baby.setLED(i*5,i*10,i*20); //makes a light gradient
  baby.sleep(1500);
  
 }
      // Always end your program with finch.quit()
      baby.quit();
      System.exit(0);
    }
}

Advertisements

Leave a Comment so far
Leave a comment



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: