Lucky Cat Tutoring Services


Arrays 01
November 3, 2009, 4:44 pm
Filed under: Class Notes, Java | Tags: , , , , , , ,

ARRAYS ARE:
–Primitive variables are designs to hold only one value at a time
–allows to create collection of like indexed values
–can store any type of data but only one type at a time
–List of data elements

Creating Arrays
–An array is an object so it needs to be referenced. Then create array and assign address

int[]nameofarray=new int [INTEGER VALUE]

Integer value can be a variable, a constant etc but always must be INT.
–Whatever data type is stored in the array determines how it is initialized. If it’s created as an INT all values start at 0.
–may be of any type (floats, car, long, double etc)

ARRAY SIZE: must be a non negative number. It may be a literal, a constant, or avariable
ex: final int ARRAY_SIZE=6
int[] numbers=new int[ARRAY_SIZE]
–Once created the size is fixed and cannot change!!

USING INDEX VALUES
–The first element is ALWAYS INDEX 0. The last element in the array is always the size or length -1

In an array with 6 indexes. The first one is index0, the last one is index5

Calling an index:
arrayname[indexnumber]= new value //assigning variables to be held in the index
arrayname[0]=17

An array is accessed by:
–The reference name
–the subscript [] which declares what to access

Can be treated like any other variable of the same type.
Continue reading



Return Method Example
October 20, 2009, 4:08 pm
Filed under: Uncategorized | Tags: , , , , , ,

Here’s an example of a method that returns the smallest number out of a set of three entered by the user:
Continue reading



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? Continue reading



CIT 111: While Loops
September 17, 2009, 3:59 pm
Filed under: Uncategorized | Tags: , , , ,

While loops mean “while this statement is true, do this. When it’s false do something else. A loop is a part of a program that repeats until a certain need is met
Every “WHILE” loop that you write has certain parts. If you forget these, you can end up with an infinite loop and you don’t want this. Infinite loop goes on forever (the dreaded hourglass in windows, the spinning pinwheel of death in mac…) and THIS IS BAD! Don’t be like Microsoft, loop well.

With all loops:
Initialize
Test
Update

Make sure not to close your loop prematurely with a “;”
Ex:

while (number MAX_INT) // NO “;” IN A WHILE LOOP; && cannot work for this, instead use OR which is || {
JOptionPane.showMessageDialog(null,”You must not be able to read. Try again, Ceiling Cat is watching! “);
inData = JOptionPane.showInputDialog(” Please enter a number from 1 to 10″);
number =Integer.parseInt(inData);//make string a number datatype
}

Lets look at this little program:

/**
This program demonstrates the while loop.
*/

public class WhileLoop
//while is a KEY WORD and has special meaning. “While” is followed by a boolean expression inside a {}
//while “this bit of bode is doing its thing and this condition is true, then do this command. when it’s FALSE stop doing.

{
public static void main(String [] args)
{
int number = 1; //this INITIALIZES

while (number <= 5) //this TESTS
{
System.out.println("Hello");
number++;//this UPDATES the loop
}

System.out.println("That's all!");
}
}

Keep in mind that {}s are your friends. They link bits of code together like putting beads in different boxes in a kit. Lets do a tracethrough of this little program: Continue reading