Lucky Cat Tutoring Services

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

–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!!

–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

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

Free Programming Reference Cards
October 8, 2009, 3:50 pm
Filed under: Java | Tags: , , , ,

Programmer Ref Cards from Dzone. Free Sparknotes style cheat sheet references for programmers and students. Registration to the site is free too!

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);
   //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
   System.out.println(“number ” +i);
      // Always end your program with finch.quit()

How about a fun nose  color change too? Continue reading

CIT 111 Java 101 week 2
August 25, 2009, 4:36 pm
Filed under: Class Notes, Java | Tags: , , ,

Time for more notes from D Smith’s CIT 111 class at CCAC.

A quick review:
–Classs can stack
–You can only have 1 main class
–You can have as many “Methods” in your class as you want
–All you need to get a program to run at all is a basic skeleton:

/*This is where comments go
public class nameOfProgram
        public static void main(String[] args)



Lets make a little program to draw a star pattern using last week’s lesson:

public class StarProgram
 { public static void main(String []args)
   System.out.println(”    *    “);
   System.out.println(“*       *”);
   System.out.println(”    *   “);
   System.out.println(”    *    “);
   System.out.println(”   ***   “);
   System.out.println(”  *****  “);
   System.out.println(” ******* “);
   System.out.println(”  *****  “);
   System.out.println(”   ***   “);
   System.out.println(”    *    “);
 } // ends the class and program

This displays a little star pattern like old school “Hi I’m drawing with emoticons” postings from the dark ages of the internet.  Don’t like how it looks? No need to delete a line of code:

Just turn whatever line that bothers you into a comment by dropping a “//” in front; great for debugging.


Learning about initializing variables

–if you plan to use a variable you need to declare it

–Variable can just be declared: ex int hours
–Assign set amount: ex int hours = 40;

int (INTEGERS) ex 7, 27, whole numbers
—Byte: 1 byte of memory
—Short: 2 bytes
—Long: 8 bytes. All LONGS must have the suffix “L” at the end or it will be output as an integer and not a long
double (FLOATING POINT) ex 7.1, 27.3 decimals
public class IntegerVariables
public static void main(String[] args)
int checking; // Declare an int variable named checking.
byte miles; // Declare a byte variable named miles.
short minutes; // Declare a short variable named minutes.
long days; // Declare a long variable named days.

//assignments to the data types, not redeclared
checking = -20;
miles = 105;
minutes = 120;
days = 185000L; //make sure it stays long
// int years = days / 365; mixed data types don’t work
int years = (int)days / 365; //years must be long; can’t fit 8 bytes of stuff in 4 byte box. Use explicite casting
Continue reading