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

Advertisements


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



October 13, 2009, 4:07 pm
Filed under: Uncategorized | Tags: , , , ,

Methods:Stand alone units, or blocks of code that do a specific task.
–EXAMPLE: public static void main(String []args) <—a main method
–the "public" means it's public, "void" returns nothing, "(String[]args)" returns the string

Step-line Refinement: Creating routines and methods to break the code down into more manageable and efficent parts.

MAKING A METHOD
1) Call the method inside the main method: any method that is called inside the main must also be STATIC!

Example: public static void printStar()
{
}
KEY POINT:METHODS CAN NEVER BE INSIDE ANOTHER METHOD! It’s inside the class, but outside any other method!

2) next put the required method code inside the {} to make the method, making sure that the block of code stands alone.

3)INVOKING THE METHOD: put the method in the code by putting the method name and ()

Continue reading