Wednesday, February 24, 2010

3rd day

- headache , bad mood , dnt want to write anything at all -
- yet i have to -

what i got :
- review, the simplest code
public class Simplest{
public static void(String[] args){
//do nothing(cant be ran without method)
}
}

- today's menu
vars
arithmethic expressions
standard I/0
math class
gregorianCalendar Class

- public void doPaint(int colour);
void means there is no returned value, so the code above is called a procedure

- starting out, basic coding
public class Calculate{
public static void main(String[] args){
//set the value

//calculate

//print out the result
}
}

- vars
- types
- byte
- short
- int
- long
- float
- double
- boolean
- char
- declaration
syntax =
ex = boolean a;
int x,y;
char c;
- initialization
syntax = =
ex = a=true;
x=10;
c="you";

- code example
public class Calculate{
public static void main(String[] args){
//set the value
int income = 500000, expense = 270000;
float base = 2.7f;
float height;
int saldo;
float area;

height = 3.1f;

//calculate
saldo = income - expense;
area = (float)1/2 * base * height;

//print out the result
System.out.println("My money now = " + saldo);
System.out.println("Triangle area = " + area);
}
}

- the output should be like this
My money now = 230000
Triangle area = 4.185


- every vars should have a type

- the six numeric data types differ in the precisions of values they can store in memory
byte , short , int , long , float , double
smallest < < < < < < < < < largest

- number manipulating, based on precedence rules from the highest to lowest
- subexpression ; ( )
- unary operator ; +,- ; plus and minuses of the numbers
- multiplicative operator ; *,/
- additive operator ; +,- ; addition and substraction

- type casting
if x is float, and y is int, x + y is float
its called mixed expression, which will take the highest expression which is float

- explicit type casting
syntax - ()
example- (float) x / 5
(int) (x / y * 5.0)

- implicit type casting
example- double y = 5 + 7;
its adding 5 with 7 first, then promote it to double type variable
remember that it is a promotion, and a demotion is not allowed

- constants
means a variable with an unchanged value, use uppercase
syntax - final =
example- final PI = 3,1472;

- type mismatch
when a variable is an int, and when the value will be used for a string variable,
it will be conduct a type mismacth

- type conversion - wrapper classes
used to convert numeric into string and vice versa

- code example
public class CrossType{
public static void main(String[] args){
int age;
age = Integer.parseInt(args[0]);
age += 5;
System.out.println("Your age 5 years later : " + age);
}
}

- other type conversion, to :
- integer parseInt
- long parseLong
- float parseFloat
- double parseDouble
remember to make the value converted is conversible

- overloaded operator +
strOutput = "test" + 1 + 2;
the output will be "test12"
strOutput = 1 + 2 + "test";
the output will be "3test"

- standard Input/Output, code example
import java.util.Scanner;
public class MyScanner{
public static void main(String[] args){
System.out.print("Your age is now = ");
Scanner myScanner = new Scanner(System.in);
int age = myScanner.nextInt();
age += 5;
System.out.println("In the 5 years, your age will be " + age);
}
}

- math class
math class in java.lang package contains method for math functions

- code example
import java.util.Scanner;
public class MyMath{
public static void main(String[] args){
double num, x, y;

. . .

num = Math.sqrt(Math.max(x,y) + 3.14);
}
}

- gregorian calendar class

- code example
import java.util.GregorianCalendar;
public class MyCalendar{
public static void main(String[] args){
GregorianCalendar now = new GregorianCalendar();
System.out.println(" today is " + now.get(now.DATE) +
"/" + now.get(now.MONTH) +
"/" + now.get(now.YEAR));
}
}

thats all...

No comments:

Post a Comment