|
Fundamental Programming Concept
Data Types
Variable Declaration
Assignment Statement
Increment/Decrement Operators
Arithmetic Operators
Type Conversion
Constant
Strings
Data Types
Integer
byte : 1 Byte (-128 to +127)
short : 2 Byte (-32,768 to +32,767)
int : 4 Byte (-2,147,483,648 to +2,147,483,647)
long : 8 Byte (-9,000,000,000,000,000,000 to +9,000,000,000,000,000,000 )
Variable Declaration
Examples:
int total;
int count, sum;
// declare and initialize
int total = 0, count = 20;
float unit_price = 57.25;
Java Keywords
abstract, boolean, break, byte, case, catch, char, class, continue, default, do, double, else, extends, false, final,
finally, float, for, if, implements, import, instanceof, int , interface, long, native , new, null, package, private,
protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, true, try, void,
volatile, while
Keywords that are reserved but not used by Java
const, goto
For Variable Declaration
Do not use Integer at the started
Do not use ? or %
Do not use spacebar
Do not use reserved words
Assignment Statements
Examples:
int x, sum = 0;
x = 10;
x = x + 1;
sum = sum + x;
Increment and Decrement Operators (++, --)
- prefix: ++n (increase the value of n before using n)
- postfix: n++ (increase the value of n after using n)
เช่น n = 5; n = 5;
x = n++; x = ++n;
// x = 5 // x = 6
n = 6 n = 6
- (i+j)++; ==> illegal
Arithmetic Operators
+ - * / %
A = 10 + 20 - 5 * 2;
X = 7.0 / 2.0 // 3.5
Y = 7 / 2 // Div => 3
Z = 3 % 5 // Modulo => 3
Type Conversion
double sum = “a lot”; // Error
------------------------------
int baht = 2;
double sum = baht; // OK
baht = (int) sum; // Convert double to int
------------------------------
sum = 13.75
int p = (int) (sum * 100); // => 1375
int p = (int) sum * 100; // => 1300
------------------------------
Constant
final double PI = 3.14159;
final int STUDENTS = 25;
STUDENTS = 30; // cannot change !!!
x = Math.PI; // constant in class Math
Math Class
Methods:
Math.abs( x ): Math.abs(-10) is 10
Math.ceil( x ): Math.ceil(9.2) is 10;
Math.ceil(-9.8) is -9
Math.cos( x ): Math.cos(0.0) is 1
Math.exp( x ): Math.exp(1.0) is 2.71828
Math.floor( x ): Math.floor(9.2) is 9;
Math.floor(-9.8) is -10
Math.log( x ): Math.log(2.718282) is 1
Strings
Strings Declaration
String name = “Jane”;
Assign Strings
name = “John”;
Strings length
int n = name.length();
Not String.length(name)
Empty string is Strings that has String length equals 0
String name = “”;
Substrings
String.substring(begin, end)
String s = “Hello World!”;
String sub = s.substring(0, 4);
The results of sub = “Hell”
The first index of Java Strings is 0
String m = s.substring(6, 11);
The results of m = “World”
String t = s.substring(6); // “World!”
Concatenation
For concatenation we use +
String fname = “Peter”;
String lname = “Pan”;
String name = fname + lname;
// We have got “PeterPan”
OR String name = fname + “ ” + lname;
// We have got “Peter Pan”
Adding Strings with Integer,the result is Strings
String m = “Version”;
int n = 7;
String o = m + n; // “Version7”
Convert Strings-Integer,Double
int age = 20;
String ages = Integer.toString(age); // “20”
double weight = 40.6;
String weights = Double.toString(weight); // “40.6”
String ages = “20”;
int age = Integer.parseInt(ages); // 20
String prices = “$3.80”;
double p = Double.parseDouble(prices.substring(1));
// 3.80
Exercise 1
Write a program that inputs three integer from the keyboard via command line and displays the sum, average, product, maximum, and minimum of the numbers.
Example StringEx1.java
public class StringEx1 {
public static void main(String args[]) {
String name1 = "Chulalongkorn";
String name2 = "University";
System.out.println("Name 1 = " + name1);
System.out.println("Length of Name 1 = " + name1.length());
String name3 = name1 + " " + name2;
System.out.println("Name 3 = " + name3);
String name4 = name1.substring(0, 5);
String name5 = name1.substring(5);
String name6 = name3 + " is the oldest university in Thailand";
System.out.println("Name 4 = " + name4);
System.out.println("Name 5 = " + name5);
System.out.println("Name 6 = " + name6);
String data1 = "30";
String data2 = "45.60";
int n1, n2;
double n3, n4;
n1 = Integer.parseInt(data1);
n2 = n1 + 20;
System.out.println("n2 = " + n2);
n3 = Double.parseDouble(data2);
n4 = n3 + 54.40;
System.out.println("n4 = " + n4);
}
}
|
|