Lucky Cat Tutoring Services


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



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



CS111 Decision Structures part 1
September 17, 2009, 3:20 pm
Filed under: Uncategorized

Even the most basic programs have to make choices. We do this with “if-then”, “if-else”, “if-else-if”, “Switch” and other decision structures.

Switch Control Structure: acts like an if else statment; lets user pick an option and ‘switches’

Start with SWITCH which is a keyword and tells the program to evaluate that variable (in example is “number”). Switch can only compare INT or CHAR variables. BREAK after each case pops out of the control structure.

import java.util.Scanner; // Needed for Scanner class

/**
This program demonstrates the switch statement.
*/

public class SwitchDemo
{
public static void main(String[] args)
{
int number; // A number entered by the user

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get one of the numbers 1, 2, or 3 from the user.
System.out.print(“Enter 1, 2, or 3: “);
number = keyboard.nextInt();

// Determine the number entered.
switch (number)
{
case 1:
System.out.println(“You entered 1.”);
break;
case 2:
System.out.println(“You entered 2.”);
break;
case 3:
System.out.println(“You entered 3.”);
break;
default:
System.out.println(“That’s not 1, 2, or 3!”);
}
}
}

Switch control structures are easier to read than other forms



CIT111: Lets Add and Subtract
September 17, 2009, 3:17 pm
Filed under: Uncategorized | Tags: , , , ,

With incrementing and decrementing, things can be in many different orders. You can prefix, or postfix and write it in different ways.

Example: “++number” is the same as “number++” or “number +=1”.

What happens with prefixing:
Say you have a number 4 and ++number. When it runs the program it will starting 5, not 4. If you put it after, then it will start with 4. Here’s a small example program.

/**
This program demonstrates the ++ and — operators.
*/

public class IncrementDecrement
{
public static void main(String[] args)
{
int number = 4; // number starts out with 4

// Display the value in number.
System.out.println(“number is ” + number);
System.out.println(“I will increment number.”);

// Increment number.
number++; //number=number + 1; number += 1 Can put ++ or — infront of variable

// Display the value in number again.
System.out.println(“Now, number is ” + number);
System.out.println(“I will decrement number.”);

// Decrement number.
number–; //number=number-1; number -=1

// Display the value in number once more.
System.out.println(“Now, number is ” + number);
}
}

The result of this program shows up as:
number is 4
I will increment number.
Now, number is 5
I will decrement number.
Now, number is 4

Process completed.

If PREfixed, the program spits out
number is 5
I will increment number.
Now, number is 6
I will decrement number.
Now, number is 5

Process completed.



CIT 111: String Classes and Methods
September 1, 2009, 4:41 pm
Filed under: Uncategorized

String classes and Methods
Each string class has methods to manipulate them. Any input from GUI windows come in as a string datatype and must be changed into the correct classes. String classes are constant and their value can’t be changed after they are created.
A string class is a complex data type, sort of like an ‘array’ but it’s more than just that. For example, in strings, the characters are indexed by number. Index = “length -1”
A list of every string method can be found under strings at Java 6 API Docs. This is handy if you don’t know what the method is or just want to know more.



CIT 111: Booleans & Other Primitive Data Types
September 1, 2009, 3:31 pm
Filed under: Uncategorized | Tags: , ,

Booleans: Can hold true or false, can be assigned any way that we want them, as long as the expression evaluates to “true or false”.

Example:

// A program for demonstrating boolean variables

public class TrueFalse
{
public static void main(String[] args)
{
boolean bool;
int a=18, b=12;
bool = true;
System.out.println(bool);
bool = false;
System.out.println(bool);
bool = a > b;
if (bool)
{System.out.println(“‘a’ is larger than ‘b'”);
}
}
}



Should come out to say:

true
false
‘a’ is larger than ‘b’

Process completed.

CHARS: are letter based only; can only hold a single character.

// This program demonstrates the char data type.

public class Letters
{
public static void main(String[] args)
{
char letter;

letter = ‘A’;
System.out.println(letter);
letter = ‘B’;
System.out.println(letter);
}
}

COMMON MISTAKES WITH CHARS:
-Errors occur when more than one character is used. Char can only use ONE.

-Double quotes “” around the single character makes it the wrong data type. Anything with “” is a STRING not a CHAR!! Single quotes must always be used


Chars and Integer relationships:
Letters can be assigned using numeric value for a letter in Java 6.