-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...
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment