Coder Profile - Show off your skills, get a coder profile.
 
 
 
source codes Categories
Browse All
Databases (1)
Design & Creativity (1)
Operating Systems (2)
Programming (23)
Security (6)
Web Development (1)
search Search Inside
Business & E-Commerce
7.00
out of 10

Java programming for beginners. Article 2


Hello again. As we finished discussing on how to declare different data types, today I�ll start with how to assign them a value. So consider the following:

Section 1.7
Once a variable has been declared, you can assign it a value (or initialize it while declaring it) using the equal sign.
Examples: age = 16;
int x = 9; //Initialization
//Assigning values
weight = 160;
amount = 11.63;
interestRate = .235
They also can be initialized using one statement:
Example: int age = 16, weight = 160;
double amount = 11.63, interestRate = .235;

section 1.8

Arithmetic Expressions.
Examples of simple arithmetic expressions are:

Addition + 12.62 + 9.8
Subtraction � 18 � 3
Multiplication * .08 * 12.2
Division / 12.6/2.0
Remainder (mod) % 3 % 2

When evaluating simple arithmetic expressions the data type of the result is evaluated by the following rules:
� If you divide an int by an int you get an int and the answer gets truncated
� If you divide a double by a double you get a double
� Double by int or int by double, you get a double

Examples: Try to do the following

1. 15/3 = __________ 2. 15/4 = __________

3. 15 + 2.0 = __________ 4. 15/2.0 = __________

5. 15 % 4 = __________

6. 21 % 8 = __________ 7. 21 % 7 =(find a remainder) __________

Answers

1. 15/3 = ___3_______ 2. 15/4 = ____3______

3. 15 + 2.0 (it might also be 17, depends on a variable that you assign it to)= ____17.0______ 4. 15/2.0 = _____7.5_____

5. 15 % 4 = ____3______

6. 21 % 8 = _____5_____ 7. 21 % 7 = ____0______


Order of Operation:
Expressions are evaluated in precedence order. For all compilers, equal precedence expressions are evaluated left to right.

Operators in precedence of order:
- Parenthesis ()
- Multiply, divide, and remainder *, /, %
- Add and subtract + , �

Example:
1. 35 / 7 % 3 * 4 = _____8_____


Section 2.0
Displaying Output (in order to display something in terminal IO writes the following statement before the name of the class:
import terminalIO;)
Use System.out.println( ) to display output to the screen. It is advisable to enclose arithmetic expressions within parenthesis to ensure that the arithmetic operation is performed correctly.
Examples:
System.out.println(�This is my first program!!�);

Output:
This is my first program!!
System.out.println (�The total of 6 and 15 is � + (6 + 15) );
Output:|
The total of 6 and 5 is 21
System.out.println (�The sum of 12.2 and 15.754 is & \n� + (12.2 + 15.754) );
Output:
The sum of 12.2 and 15.754 is
(That�s a new line)12.954

Note: �\n� automatically returns the cursor to the next line.
�\t� will automatically insert a tab (again C/C++).
Ok. So like I said I'll continue the tutorial the next day

What would you rate this article?
Please login to rate and upload coding articles. To start registering a free account with us, click here..
More Articles By This Author
Recently Posted "Programming" Articles
Recently Rated "Programming" Articles
 
  Part of the MyPingle Network
Copyright � 2007, Scott Thompson, All Rights Reserved
Development Blog :: Contact Me :: Terms & Conditions :: Privacy Policy :: Documents