Monday, July 14, 2008

Java, Java and more Java

This blog post is solely for my own references. You don't have to read it. Instead, I don't want you to read it.

You can have a variable in your class that all methods in the class can read, use, initialize, access and change. To do that, declare the variable outside the methods, with the keyword "static". For example, "static int blah = 0;". (I have no creativity with naming. ^_^") In that way, all methods inside that class can use that variable, and yay, no more getting confused with parameters! (I'm such a loser.)

You can also do this with objects, so that you can use the object anywhere in any methods throughout the class. For example, "static Scanner scKey = new Scanner (System.in);". (Sue me, I'm lazy and no longer colouring syntax -.-)

I don't know just how the hell this works to verify if you key in a number or not, but it's sexy.



Yay, no more exceptional handling!! XD

The following are Scanner Class Methods that Check for Valid Input Values.
  • hasNextBoolean()
  • hasNextByte()
  • hasNextDouble()
  • hasNextFloat()
  • hasNextInt()
  • hasNextLong()
  • hasNextShort()
I was going to make a joke here about having to take a Math class to fully appreciate the Math class, or how you'd better stay away from the Math class if you didn't do so well in Math class, or how if you're on the football team, maybe you can get someone to do the Math class for you. But it seemed too easy, so I decided not to. XD

Mathematical Functions Provided by the Math Class
  • Math.abs(argument) - Returns the absolute value of the argument. In laymen's terms, positive numbers remain positive, while negative numbers become positive too.
  • Math.cbrt(argument) - Returns the cube root of the argument. For example, 8 gives you 2, 27 gives you 3, and 64 gives you 4.
  • Math.exp(argument) - Returns the exponential function of the argument. In laymen's terms, the return value will be the mathematical constant e (which is 2.71828182845904523536...) to the power of the argument.
  • Math.hypot(arg1, arg2) - The Pythagoras' theorem states that in any right triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle). This nifty little function allows you to key in the values of the lengths of two sides that meet at the right angle, and return the length of the hypotenuse. In laymen's terms, it returns the square root of the arg1 squared plus arg2 squared.
  • Math.log(argument) - Returns the natural logarithm of the argument. In other words, the return value will be equal to loge(argument), or ln(argument), where e is 2.71828182845904523536...
  • Math.log10(argument) - Returns the base 10 logarithm of the argument. In other words, the return value will be equal to log10(argument).
  • Math.max(arg1, arg2) - Compares the two variables and returns the larger of the two arguments.
  • Math.min(arg1, arg2) - Compares the two variables and returns the larger of the two arguments.
  • Math.pow(arg1, arg2) - Returns the value of the first argument to the power of the second argument. In other words, the value will be equal to arg1arg2.
  • Math.random() - Returns a random number greater than 0 (inclusive) and less than 1 (exclusive). No arguments are needed.
  • Math.signum(argument) - Checks whether the number is positive, negative or zero, and returns a number accordingly. If the number is positive, it will return 1. If the number is negative, it will return -1. If the number is a zero, it will return 0.
  • Math.sqrt(argument) - Returns the square root of the argument. For example, 4 gives you 2, 9 gives you 3, and 16 gives you 4.
This statement can be used to generate a random number within a range, where low is the lowest value in the range, and high is the highest value in the range.

int random = (int) (Math.random() * (high - low + 1) + low;

Rounding Functions Provided by the Math Class
  • Math.ceil(argument) - Rounds up the argument into a whole number and returns it as a double.
  • Math.floor(argument) - Rounds down the argument into a whole number and returns it as a double.
  • Math.rint(argument) - Returns a double value that is a whole number closest to the argument. If two integers are equally close, then return the one that is the even number.
  • Math.round(argument) - Returns a whole number that is closest to the argument. A long will be returned if the argument is a double, and int will be returned if it is a float.

No comments: