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...

Wednesday, February 17, 2010

Second Day - OOP

-Struggle and that's how we live-

i'm back and as confused as ever..
first time dealing with java, yet im used to programming with C...

on second thought, maybe im getting the hang of it...
since im writing this in class while doing try and error with java...

lol...thats how human is...
developing as time movin forward...

okay, what i got today :
- given a problem, there is a drugstore selling 3 kinds of drug : generic, bubuk, pill...
generic = 1 times usual price, just take the drug
bubuk = 3 times usual price, cook it with water
pill = 4 times usual price, take it by drinking water, minimum purchase = 10 pills
- making 4 classes consist of drug, generic, bubuk, and pill [in UML and notepad]...
- making one new file [controller.java] to connect the subclass to the superclass
- compile them..
- run them...

here i will show you drug, generic, and controller


Drug.java

public class Drug {

/*plz name this class Drug.java*/
/*DEFAULT String = kosong = string x =""; */

String kode = "";
String bentuk = "";

/*DEFAULT int = kosong = int x=0; */

//this 2 vars will be used by the other java file, since this one is the superclass
int harga=1;
String caraMinum="";

//setter
public void setHarga(int harga){
this.harga = harga;
}

//getter
public int getHarga(){
return harga;
}

public static void main(String[] args){
//do something
}

}



Pill.java


public class Pill extends Drug{
int minimumPembelian = 10;

/* method priceMultiplier used for changing the price to 4 times usual*/

public void setMultiplier(){
harga=harga*4;
}
public void isiCaraMinum(){
caraMinum="pakai air putih";
}
}

**for Generic.java and Bubuk.java, the file is similar to Pill.java, but change every "Pill" to "Generic" or "Bubuk"..


Controller.java

public class Controller{
/* this class is used to call previous class : Drug & Pill*/

public static void main(String[] args){

Pill myPill = new Pill();
myPill.setMultiplier();
myPill.isiCaraMinum();
System.out.println("Harga pil = " + myPill.harga);
System.out.println("cara minum =" + myPill.caraMinum);

Generic myGeneric = new Generic();
myGeneric.setMultiplier();
myGeneric.isiCaraMinum();
System.out.println("Harga generik = " + myGeneric.harga);
System.out.println("cara minum =" + myGeneric.caraMinum);

Bubuk myBubuk = new Bubuk();
myBubuk.setMultiplier();
myBubuk.isiCaraMinum();
System.out.println("Harga bubuk = " + myBubuk.harga);
System.out.println("cara minum = " + myBubuk.caraMinum);
}
}

thats it...
to run it, see my first day...

c u next week...

Wednesday, February 10, 2010

Object Oriented - 1st day

Now is the time to shape your stories! Your fate is in your hands!!
- Auron, Final Fantasy X -

Morning Class - Early Rise - yeah, whatever...
1st OOP class and here's what i've got...
open it, read it, whatever....hope u got what u need....

OOP
11 february 2010

class and objects
class -> gambaran umum akan sesuatu dengan ciri2 tertentu
ex : students, cats
object -> sesuatu yang lebih spesifik berdasarkan ciri2 dari class
ex : edward, garfield

properties and method
properties : ciri2nya, misal u/ cats -> color, weight, race
ada 2 properties :
- variable : berubah-ubah
- constant : ga bisa berubah
method : apa yang bisa dilakukan, misal u/ cats -> eat, jump, walk
ada 2 jenis :
- without argument/value, ex : jump()
- with argument/value, ex : jump(5) -> jump 5 times

inheritance
inheritance : mekanisme OOP dimana ada 2 atau lebih entity yang beda tapi punya banyak kesamaan
- superclass/ancestor : yang bersifat lebih umum
- subclass/descendant : yang bersifat lebih khusus dengan sifat mirip dengan superclass

software engineering
seperti membangun sesuatu, perlu disiplin ilmu, perlu langkah dan desain, langkah/stages nya :
- analysis : cari gambaran umum nya
- design : di bentuk design nya
- coding : programmingnya
- testing : di tes
- operation and maintenance : bagaimana di pakai orang lain dan perbaikan2nya

practice makes perfect
1. open cmd prompt
2. type "notepad HelloWorld.java" -> create new file
3. type :
public class HelloWorld {
public static void main(string[] args){
system.out.println("I Rule !");
system.out.println("The World !");
}
}
4. save
5. compile using "Javac HelloWorld.java"
6. run using "Java HelloWorld"

activating java
go to C:\Program Files\Java\jdk1.6.0_11\bin
go to system properties(right click @ my comp)
choose advanced
choose environment variables
double click path (system variables)
add ";C:\Program Files\Java\jdk1.6.0_11\bin" at the end of variable value
OK to the end

that's it for now...

It's ME !!!!

Life is a story...
And this is My Story...

My Story goes my way - and so does my life...
I dont care about mostly anything - just some in my opinion is important that i do...

Here goes My Story - and it goes MY WAY...