Monday, March 10, 2014

Java Operators



Java Arithmetic Operators


The Java programming language has includes five simple arithmetic operators like are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo).The following table summarizes the binary arithmetic operators in the Java programming language.

The relation operators in Java are: ==, !=, <, >, <=, and >=. The meanings of these operators are:
Use Returns true if
op1 + op2 ( op1 added to op2)
op1 - op2 (op2 subtracted from op1)
op1 * op2(  op1 multiplied with op2)
op1 / op2 (op1 divided by op2)
op1 % op2 (Computes the remainder of dividing op1 by op2)


The following java program, ArithmeticProg , defines two integers and two double-precision floating-point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses + to concatenate strings.

The arithmetic operations are shown in boldface.

public class ArithmeticProg { 
 public static void main(String[] args) { 
 //a few numbers 
 int i = 10; int j = 20; double x = 10.5; double y = 20.5;
 //adding numbers 
 System.out.println("Adding"); System.out.println(" i + j = " + (i + j));
 System.out.println(" x + y = " + (x + y));
 //subtracting numbers 
 System.out.println("Subtracting"); System.out.println(" i - j = " + (i - j)); 
 System.out.println(" x - y = " + (x - y)); 
 //multiplying numbers
 System.out.println("Multiplying"); 
 System.out.println(" i * j = " + (i * j));
 System.out.println(" x * y = " + (x * y)); 
 //dividing numbers System.out.println("Dividing");
 System.out.println(" i / j = " + (i / j)); 
 System.out.println(" x / y = " + (x / y)); 
 //computing the remainder resulting
 //from dividing numbers
 System.out.println("Modulus");
 System.out.println(" i % j = " + (i % j)); 
 System.out.println(" x % y = " + (x % y)); } } 

source:http://www.freejavaguide.com/corejava2.htm

********************************************************************************

Java Assignment Operators

It's very common to see statement like the following, where you're adding something to a variable. Java Variables are assigned, or given, values using one of the assignment operators. The variable are always on the left-hand side of the assignment operator and the value to be assigned is always on the right-hand side of the assignment operator. The assignment operator is evaluated from right to left, so a = b = c = 0; would assign 0 to c, then c to b then b to a.

i = i + 2;

Here we say that we are assigning i's value to the new value which is i+2.

A shortcut way to write assignments like this is to use the += operator. It's one operator symbol so don't put blanks between the + and =.
i += 2; // Same as "i = i + 2"

The shortcut assignment operator can be used for all Arithmetic Operators i.e. You can use this style with all arithmetic operators (+, -, *, /, and even %).

Here are some examples of assignments:

//assign 1 to
//variable a
int a = 1;

//assign the result
//of 2 + 2 to b
int b = 2 + 2;

//assign the literal
//"Hello" to str
String str = new String("Hello");

//assign b to a, then assign a
//to d; results in d, a, and b being equal
int d = a = b;


***********************************************************************


There are 2 Increment or decrement operators -> ++ and --. These two operators are unique in that they can be written both before the operand they are applied to, called prefix increment/decrement, or after, called postfix increment/decrement. The meaning is different in each case.

Example

x = 1;
y = ++x;
System.out.println(y);

prints 2, but

x = 1;
y = x++;
System.out.println(y);

prints 1

Source Code

//Count to ten

class UptoTen {

public static void main (String args[]) {
int i;
for (i=1; i <=10; i++) {
System.out.println(i);
}
}

}When we write i++ we're using shorthand for i = i + 1. When we say i-- we're using shorthand for i = i - 1. Adding and subtracting one from a number are such common operations that these special increment and decrement operators have been added to the language. T

There's another short hand for the general add and assign operation, +=. We would normally write this as i += 15. Thus if we wanted to count from 0 to 20 by two's we'd write:



Source Code

class CountToTwenty {

public static void main (String args[]) {
int i;
for (i=0; i <=20; i += 2) { //Note Increment Operator by 2
System.out.println(i);
}

} //main ends here

}As you might guess there is a corresponding -= operator. If we wanted to count down from twenty to zero by twos we could write: -=

class CountToZero {

public static void main (String args[]) {
int i;
for (i=20; i >= 0; i -= 2) { //Note Decrement Operator by 2
System.out.println(i);
}
}

}

source:http://www.freejavaguide.com/increment_decrement_operators.htm
*******************************************************************************



Java Relational Operators

A relational operator compares two values and determines the relationship between them. For example, != returns true if its two operands are unequal. Relational operators are used to test whether two values are equal, whether one value is greater than another, and so forth. The relation operators in Java are: ==, !=, <, >, <=, and>=. The meanings of these operators are:









Variables only exist within the structure in which they are defined. For example, if a variable is created within a method, it cannot be accessed outside the method. In addition, a different method can create a variable of the same name which will not conflict with the other variable. A java variable can be thought of

The main use for the above relational operators are in CONDITIONAL phrases The following java program is an example, RelationalProg, that defines three integer numbers and uses the relational operators to compare them.

 public class RelationalProg {

 public static void main(String[] args) {
 //a few numbers int i = 37; int j = 42; int k = 42;
 //greater than 
 System.out.println("Greater than...");
 System.out.println(" i > j = " + (i > j));//false
 System.out.println(" j > i = " + (j > i)); //true
 System.out.println(" k > j = " + (k > j)); //false
 //(they are equal) 

//greater than or equal to
  System.out.println("Greater than or equal to..."); 
System.out.println(" i >= j = " + (i >= j)); //false
 System.out.println(" j >= i = " + (j >= i)); //true
 System.out.println(" k >= j = " + (k >= j)); //true

 //less than
 System.out.println("Less than...");
 System.out.println(" i < j = " + (i < j)); //true 
System.out.println(" j < i = " + (j < i)); //false
 System.out.println(" k < j = " + (k < j)); //false

 //less than or equal to
 System.out.println("Less than or equal to...");
 System.out.println(" i <= j = " + (i <= j)); //true 
System.out.println(" j <= i = " + (j <= i)); //false 
System.out.println(" k <= j = " + (k <= j)); //true 
 
//equal to
 System.out.println("Equal to...");
 System.out.println(" i == j = " + (i == j)); //false
 System.out.println(" k == j = " + (k == j)); //true

 //not equal to
  System.out.println("Not equal to..."); 
 System.out.println(" i != j = " + (i != j)); //true
 System.out.println(" k != j = " + (k != j)); //false } 
}

Java tutorial



Java Hello World Program


Our first application will be extremely simple - the obligatory "Hello World". The following is the Hello World Application as written in Java. Type it into a text file or copy it out of your web browser, and save it as a file named HelloWorld.java. This program demonstrates the text output function of the Java programming language by displaying the message "Hello world!". Java compilers expect the filename to match the class name.

A java program is defined by a public class that takes the form: public class program-name { optional variable declarations and methods public static void main(String[] args) { statements } optional variable declarations and methods }

Source Code


In your favorite editor, create a file called HelloWorld.java with the following contents:


/** Comment

* Displays "Hello World!" to the standard output.

*/

class HelloWorld {

public static void main (String args[]) {

System.out.println("Hello World!"); //Displays the enclosed String on the Screen Console

}

}

To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is:
javac HelloWorld.java
For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code.

You're ready to run your first Java application. To run the program, you just run it with the java command:

java HelloWorld

Sample Run

Hello world!

The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial.


HelloWorld.java

Note: It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).

You've just written your first Java program! Congratulations!!


source:http://www.freejavaguide.com/corejava1.htm

Entity Relationship Model








Download Pass year UTHM (Muat turun soalan yang lepas UTHM)

Adakah anda memerlukan soalan-soalan yang lepas.Anda boleh lah muat turun  di link yang terdapat di bawah ini

http://ent.uthm.edu.my/client/main/search/




Cara:1.masukkan nama subjek .contohnya spt gambar di bawah.

2.Click Search boleh click link "UW,,," untuk muat turun