Introduction To Programming: First Program, Types of Data, Expression
Introduction To Programming: First Program, Types of Data, Expression
6/4/2012
Keywords/Reserved Words
abstract float protected generic case static inner instanceof continue do try extends operator finally private break rest if catch switch synchronized interface native else void package boolean future byvalue short import class const throw transient null final while for public goto cast super implements int long double var outer default byte return char this throws new
volatile
/** This is my second program Date: May 31, 2012 Author: */ class MySecondProgram { public static void main(String[] args) { int x = 5; int y = 10; //Compute for the sum System.out.println (Value of first variable is + x); System.out.println (Value of second variable is + y); System.out.println (Sum of two variables is + x+y); } }
4 6/4/2012
Types of Data
Literals Variables
a primitive type
Integers
Whole number Real numbers true or false Enclosed in single quotes Examples: a, 5, S, Special Characters
Floating-Point Numbers
No Comma No Space
(between unary + or and digits)
Boolean
Characters
Literals: String
null
Literals: Summary
Numeric
Non-Numeric
Integers
Characters
Whole number
Floating-Point Numbers
Real numbers
Strings
No Comma No Space
(between unary + or and digits)
Enclosed in double quotes Series of zero or more characters Hello, Alph@Num3r1c (empty/null string)
char boolean
2 bytes 1 bit
Again, letters, digits and underscore only Cannot begin with a digit Cannot be keywords Case-sensitive
Variables names that have values that may be modified Constants names with values that cannot be modified
Used to name
Introduction to Programming
Expressions
Expressions
Arithmetic operators
Addition Subtraction
Multiplication
Division Modulo computes for the remainder
Assignment Operator
Increment/Decrement Operators
++
Increment by 1 Decrement by 1
More Operators
+=
x += 5
is the same as
x = x + 5;
x -= 5
is the same as
x = x - 5;
*= /= %=
Relational Operators
<
Less than Less than or equal Greater than Greater than or equal equal Not equal
<=
>
>=
==
!=
Logical Operators
Not or Negation
&&
||
Evaluating Expressions
Operator Precedence
Determines the priority/order of operators Determines the direction of evaluation Example: a/b/c
Associativity
OPERATOR
ASSOCIATIVITY right to left left to right left to right left to right left to right left to right
||
=, +=, and all combined assignment
left to right
right to left
binary
Binary
Programming Exercises
Create a Java program that computes for the perimeter and area of a square and triangle. The perimeter for both square and triangle is computed as getting the sum of all sides. The area, on the other hand, is computed differently. Area of a square = s2, where s is a side; and triangle is bh, where b is the base and h is the height. Supply in your program the values necessary for both square and triangle Create a Java program which displays the individual digits of 3-digit number separately,, and computes for the sum of all digits.
End of Discussion