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

Advertisements


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.