Search Java Tutorials

Custom Search

What You Will Want to Complete This Tutorial?

You must have at least 1st and 2nd pre-requirements to start the tute. Other requirements will require for last chapters only. These all are free to use and download!
  1. Java Development Kit Standard Edition (JDK / J2SE) Any Version(www.java.com)
  2. Any Text Editor (Note Pad quite enough or get Notepad++ from (http://notepad-plus.sourceforge.net)
  3. Java Run Time Environment (JRE) Any Version(www.java.com)
  4. Java Enterprise Edition (J2EE) for advanced developing (For last few chapters)(www.java.com)
  5. Java Wireless Toolkit for Mobile Application Development (For Mobile Application Development Chapters) (www.java.com)
  6. Tomcat Web Server to run Servlets. (www.tomcat.apache.org/)
Your operating system may be any version of Windows.
What will you learn (As a summary)?
  1. How to build console and windowed applications using Java
  2. How to build web page elements with Java Applets
  3. How to build Database Programs with Java
  4. How to build enterprise applications with Beans
  5. How to add Email functions to Java programs
  6. How to program smart devices (Phone, PDA etc…) with Java

Using prewritten classes in java in Java

Java contains many prewritten classes which are stored in a package. The package is implicitly imported into every java program is named “java.lang”. The classes it contains are the fundamental classes. Other classes that you may want to import known as “optional classes”.
The class “java.lang.Math” contains constants and methods that can be used to perform common mathematical functions. All of the constants and methods in the Math class are static. Common useful Math class methods include those used for finding an absolute value, finding maximum or minimum value, rounding values etc… To use a prewritten class other than “java.lang” you must use the path with the class name and import the class or import the package that contain the class.

Ex:
import javax.swing.*;
import java.util.*;


Easiest way to import prewritten classes is, importing whole package it contains. We use “*” to indicate we need all classes in the package as in above example.

Math class Methods:

· Max(x,y) Larger of x and y
· Min(x,y) Smaller of x and y
· Pow(x,y) X raised to the y power
· Sqrt(x) Square root of x
· Round(x) Closest integer to x(where x is a float or double, and the return value is an integer or long
· Rint(x) Closest integer to x(x is a double, and the return value is expressed as a double
· Floor(x) Largest integral value not greater than x
· Celi(x) Smallest integral value not greater than x
· Random() Random double number between 0.0 and 1.0
· Abs(x) Absolute value of x
· Sin(x) Sine of x
· Cos(x) Cosine of x
· Tan(x) Tangent of x
· Asin(x) Arcsine of x
· Acos(x) Arccosine of x
· Atan(x) Arctangent of x
· Atan2(x,y) That a component of the polar coordinate
· Exp(x) Exponent, where x is the base of the natural algorithms
· Log(x) Natural algorithm of x


Sample Program:
Case: We will use fundamental class “Math” in “java.lang” to manipulate some numbers. We have used float numbers and while assigning values, the values follow letter”f” to indicate that casting shouldn’t carry out.

public class TestMathClass
{
public static void main(String[] args)
{
float num1 = 4f;
// becouse we're using float data type, it's better to use assigning its values followed by 'f'
float num2 = 5f;
float num3 = 6.3f;
float num4 = 6.8f;

System.out.println("Larger value amoung num1 and num2 is: " + Math.max(num1, num2));
System.out.println("Smaller value amoung num1 and num2 is: " + Math.min(num1, num2));
System.out.println("Power of num1 raised to the num2 power is: " + Math.pow(num1, num2));
System.out.println("Largest integral value not less than num3 is: " + Math.floor(num3));
System.out.println("Closest integer to num2 is: " + Math.rint(num4));
}
}


This is the output:

No comments: