/*Contoh interface*/
public class Person{
private String name;
private String address;
private String city;
private int age;
private char gender;
/* Konstruktor */
/**
* Inisialisasi objek Person dengan nilai awal string kosong dan 0 untuk age
*/
protected Person() {
name = "";
address = "";
city = "";
age = 0;
gender = 'F';
}
/**
* Inisialisasi objek Person dengan nilai yang diberikan
*/
protected Person(String varName, String varAddress, String varCity, int varAge, char varGender) {
name = varName;
address = varAddress;
city = varCity;
age = varAge;
gender = varGender;
}
//setter
/**
* Mengubah nama
* @param varName nilai baru untuk nama
*/
protected void setName (String varName){
name = varName;
}
/**
* Mengubah Alamat
* @param varAddress nilai baru untuk alamat
*/
protected void setAddress (String varAddress) {
address = varAddress;
}
/**
* Mengubah Kota
* @param varCity nilai baru untuk kota
*/
protected void setCity (String varCity) {
city = varCity;
}
/**
* Mengubah Umur
* @param varAge nilai baru untuk umur
*/
protected void setAge (int varAge) {
age = varAge;
}
/**
* Mengubah jenis kelamin
* @param varGender nilai baru untuk jenis kelamin
*/
protected void setGender (char varGender) {
gender = varGender;
}
//getter
/**
* Mengambil nilai nama
* @return name nama
*/
protected String getName (){
return name;
}
/**
* Mengambil nilai alamat
* @return address address
*/
protected String getAddress (){
return address;
}
/**
* Mengambil nilai kota
* @return city city
*/
protected String getCity (){
return city;
}
/**
* Mengambil nilai umur
* @return name umur
*/
protected int getAge (){
return age;
}
/**
* Mengambil nilai jenis kelamin
* @return gender jenis kelamin
*/
protected int getGender (){
return gender;
}
/**
* Mengambil nilai Person
* @return str gabungan seluruh nilai atribut
*/
public String toString(){
String str =
"name : "+ name + "\n"+
"address : "+ address + "\n"+
"city : "+city + "\n"+
"age : "+age + "\n";
return str;
}
}
/**
* @author Frans Tuani Ryerson SIburian
*/
public class Instructor extends Person {
private String subject;
private String title;
private int salary;
/**
* Inisialisasi objek Instructor dengan nilai awal string kosong dan 0 untuk age dan salary
* Konstruktor secara default memanggil super()
*/
public Instructor() {
subject = "";
title = "";
salary = 0;
}
/**
* Inisialisasi objek Instructor dengan nilai yang diberikan
* Pemanggilan konstruktor berparameter dari kelas parent harus ditulis
* Jika tidak akan memanggil super()
*/
public Instructor (String varName, String varAddress,
String varCity, int varAge, char varGender,
String varSubject, String varTitle, int varSalary) {
super(varName, varAddress, varCity, varAge, varGender);
subject = varSubject;
title = varTitle;
salary = varSalary;
}
/**
* Mengubah mata kuliah
* @param varSubject nilai baru untuk mata kuliah
*/
public void setSubject (String varSubject) {
subject = varSubject;
}
/**
* Mengubah gelar
* @param varTitle nilai baru untuk gelar
*/
public void setTitle (String varTitle) {
title= varTitle;
}
/**
* Mengubah gaji
* @param varSalary nilai baru untuk gaji
*/
public void setSalary (int varSalary) {
salary = varSalary;
}
/**
* Mengambil nilai nama
* @return name nama
*/
public String getSubject () {
return subject;
}
/**
* Mengambil nilai gelar
* @return title gelar
*/
public String getTitle () {
return title;
}
/**
* Mengambil nilai gaji
* @return salary gaji
*/
public int getSalary () {
return salary;
}
/**
* Mengambil nilai Instructor
* @return str gabungan seluruh nilai atribut
*/
public String toString(){
String str =
super.toString()+"\n"+
"Subject : "+ subject + "\n"+
"Title : "+ title + "\n"+
"Salary : "+ salary;
return str;
}
}
public class DemoInheritance
{
public static void main(String []args)
{
Instructor instructor1 = new Instructor ("Umar Bakri", "Jl. Lurus No. 1", "Bandung", 30, 'L', "Strategi Algoritmik", "ST", 10000000);
System.out.println(instructor1.getName());
System.out.println(instructor1.getAge());
System.out.println(instructor1.getCity());
System.out.println(instructor1.getAge());
System.out.println(instructor1.getSubject());
System.out.println(instructor1.getTitle());
System.out.println(instructor1.getSalary());
System.out.println(instructor1);
instructor1.setName("Umar Bakri Tobing");
System.out.println(instructor1.toString());
}
}
Minggu, 07 November 2010
Sabtu, 06 November 2010
Tutorial Java part -3
class Point{
private int x;
private int y;
Point (int a, int b){
x = a;
y = b;}
Point(){
x = 0;
y = 0;}
public Point(Point P){
x = P.x;
y = P.y;}
public void setAbsis (int a){
x = a;}
public void setOrdinat (int b){
y = b;}
int getAbsis(){
return x;}
int getOrdinat(){
return y;}
public void tulisPoint(){
System.out.println("("+ x + "," + y +")");}
public Point plus(Point P1, Point P2){
this.x = P1.x + P2.x;
this.y = P1.y + P2.y;
return this;
}
public Point minus(Point P1, Point P2){
this.x = P1.x - P2.x;
this.y = P1.y - P2.y;
return this;
}
public Point dotProduct(Point P1, Point P2){
this.x = P1.x * P2.x;
this.y = P1.y * P2.y;
return this;
}
public Point crossProduct(Point P1, Point P2){
this.x = P1.x * P2.y;
this.y = P1.y * P2.x;
return this;
}
boolean EQ (Point P1, Point P2){
return((P1.x == P2.x) && (P1.y == P2.y));
}
boolean NEQ (Point P1, Point P2){
return((P1.x != P2.x) &&(P1.y != P2.y));}
boolean LT (Point P1, Point P2){
return((P1.x < P2.x) &&(P1.y < P2.y));}
boolean GT (Point P1, Point P2){
return((P1.x > P2.x) && (P1.y > P2.y));
}
boolean isOrigin (Point P){
return((P.x == 0) && (P.y == 0));}
boolean isOnSbX (Point P){
return(P.y == 0);}
boolean isOnSbY (Point P){
return(P.x == 0);}
int kuadran (Point P){
if (!isOrigin(P) && (!isOnSbX(P)) && (!isOnSbY(P))){
if((P.x > 0) && (P.y > 0)){return 1;}
else if((P.x > 0) && (P.y < 0)) {return 2;}
else if((P.x < 0) && (P.y < 0)){return 3;}
else {return 4;}
} else {return -999;}
}
public Point nextX (Point P){
this.x = P.x + 1;
this.y = P.y;
return this;}
Point nextY (Point P){
this.x = P.x;
this.y = P.y + 1;
return this;}
Point plusDelta (int deltaX, int DeltaY){
this.x = x + deltaX;
this.y = y + DeltaY;
return this;}
Point mirrorOf (Point P, boolean SbX, boolean SbY){
Point temp = new Point();
if(SbX && !SbY){
temp.x = -1 * P.x;
temp.y = P.y;}
else if(!SbX && SbY){
temp.x = P.x;
temp.y = -1 * P.y;}
else if(SbX && SbY){
temp.x = -1 * P.x;
temp.y = -1 * P.y;}
else{
temp.x = P.x;
temp.y = P.y;}
return temp;
}
float jarak0 (Point P){
int pangkat2x,pangkat2y,result;
pangkat2x = (P.x) * (P.x);
pangkat2y = (P.y) * (P.y);
result = pangkat2x + pangkat2y;
float a = (float)Math.sqrt(result);
return a;}
Point hslGeser (Point P, int Deltax, int deltaY){
Point temp = new Point();
temp.x = P.x + Deltax;
temp.y = P.y +deltaY;
return temp;}
void geserKeSbX (Point P){
P.x = 0;}
void geserKeSbY (Point P){
P.y = 0;}}
/*class driver*/
class PointTestDrive{
public static void main (String[] args){
Point A = new Point();
Point B = new Point (1,3);
Point C = new Point (7,9);
A.tulisPoint();
A.plus(B,C);
A.tulisPoint();
A.minus(B,C);
A.tulisPoint();
A.dotProduct(B,C);
A.tulisPoint();
A.crossProduct(B,C);
A.tulisPoint();
System.out.println("Apakah B dan C sama" + " " + A.EQ(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.NEQ(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.LT(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.GT(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.isOrigin(B) );
System.out.println("Apakah B dan C sama" + " " + A.isOnSbX(C) );
System.out.println("Apakah B dan C sama" + " " + A.isOnSbY(B) );
System.out.println("Apakah B dan C sama" + " " + A.kuadran(C) );
A.nextX(C);
A.tulisPoint();
A.nextY(B);
A.tulisPoint();
B.plusDelta(2,8);
B.tulisPoint();
B=A.mirrorOf(B,true,false);
B.tulisPoint();
float a = A.jarak0(B);
System.out.println("ini nilai dari a" +" " +a);
A.hslGeser(B,3,4);
A.tulisPoint();
A.geserKeSbX(B);
A.tulisPoint();
A.geserKeSbY(B);
A.tulisPoint();
}}
private int x;
private int y;
Point (int a, int b){
x = a;
y = b;}
Point(){
x = 0;
y = 0;}
public Point(Point P){
x = P.x;
y = P.y;}
public void setAbsis (int a){
x = a;}
public void setOrdinat (int b){
y = b;}
int getAbsis(){
return x;}
int getOrdinat(){
return y;}
public void tulisPoint(){
System.out.println("("+ x + "," + y +")");}
public Point plus(Point P1, Point P2){
this.x = P1.x + P2.x;
this.y = P1.y + P2.y;
return this;
}
public Point minus(Point P1, Point P2){
this.x = P1.x - P2.x;
this.y = P1.y - P2.y;
return this;
}
public Point dotProduct(Point P1, Point P2){
this.x = P1.x * P2.x;
this.y = P1.y * P2.y;
return this;
}
public Point crossProduct(Point P1, Point P2){
this.x = P1.x * P2.y;
this.y = P1.y * P2.x;
return this;
}
boolean EQ (Point P1, Point P2){
return((P1.x == P2.x) && (P1.y == P2.y));
}
boolean NEQ (Point P1, Point P2){
return((P1.x != P2.x) &&(P1.y != P2.y));}
boolean LT (Point P1, Point P2){
return((P1.x < P2.x) &&(P1.y < P2.y));}
boolean GT (Point P1, Point P2){
return((P1.x > P2.x) && (P1.y > P2.y));
}
boolean isOrigin (Point P){
return((P.x == 0) && (P.y == 0));}
boolean isOnSbX (Point P){
return(P.y == 0);}
boolean isOnSbY (Point P){
return(P.x == 0);}
int kuadran (Point P){
if (!isOrigin(P) && (!isOnSbX(P)) && (!isOnSbY(P))){
if((P.x > 0) && (P.y > 0)){return 1;}
else if((P.x > 0) && (P.y < 0)) {return 2;}
else if((P.x < 0) && (P.y < 0)){return 3;}
else {return 4;}
} else {return -999;}
}
public Point nextX (Point P){
this.x = P.x + 1;
this.y = P.y;
return this;}
Point nextY (Point P){
this.x = P.x;
this.y = P.y + 1;
return this;}
Point plusDelta (int deltaX, int DeltaY){
this.x = x + deltaX;
this.y = y + DeltaY;
return this;}
Point mirrorOf (Point P, boolean SbX, boolean SbY){
Point temp = new Point();
if(SbX && !SbY){
temp.x = -1 * P.x;
temp.y = P.y;}
else if(!SbX && SbY){
temp.x = P.x;
temp.y = -1 * P.y;}
else if(SbX && SbY){
temp.x = -1 * P.x;
temp.y = -1 * P.y;}
else{
temp.x = P.x;
temp.y = P.y;}
return temp;
}
float jarak0 (Point P){
int pangkat2x,pangkat2y,result;
pangkat2x = (P.x) * (P.x);
pangkat2y = (P.y) * (P.y);
result = pangkat2x + pangkat2y;
float a = (float)Math.sqrt(result);
return a;}
Point hslGeser (Point P, int Deltax, int deltaY){
Point temp = new Point();
temp.x = P.x + Deltax;
temp.y = P.y +deltaY;
return temp;}
void geserKeSbX (Point P){
P.x = 0;}
void geserKeSbY (Point P){
P.y = 0;}}
/*class driver*/
class PointTestDrive{
public static void main (String[] args){
Point A = new Point();
Point B = new Point (1,3);
Point C = new Point (7,9);
A.tulisPoint();
A.plus(B,C);
A.tulisPoint();
A.minus(B,C);
A.tulisPoint();
A.dotProduct(B,C);
A.tulisPoint();
A.crossProduct(B,C);
A.tulisPoint();
System.out.println("Apakah B dan C sama" + " " + A.EQ(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.NEQ(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.LT(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.GT(B,C) );
System.out.println("Apakah B dan C sama" + " " + A.isOrigin(B) );
System.out.println("Apakah B dan C sama" + " " + A.isOnSbX(C) );
System.out.println("Apakah B dan C sama" + " " + A.isOnSbY(B) );
System.out.println("Apakah B dan C sama" + " " + A.kuadran(C) );
A.nextX(C);
A.tulisPoint();
A.nextY(B);
A.tulisPoint();
B.plusDelta(2,8);
B.tulisPoint();
B=A.mirrorOf(B,true,false);
B.tulisPoint();
float a = A.jarak0(B);
System.out.println("ini nilai dari a" +" " +a);
A.hslGeser(B,3,4);
A.tulisPoint();
A.geserKeSbX(B);
A.tulisPoint();
A.geserKeSbY(B);
A.tulisPoint();
}}
Tutorial Java part - 2
public class Computer{private String monitorSpec;
private String mouseSpec;
private String keyboardSpec;
public void setMonitorSpec(String monitor){
monitorSpec = monitor;
}
public void setMouseSpec(String mouse){
mouseSpec = mouse;
}
public void setKeyboardSpec(String keyboard){
keyboardSpec = keyboard;}
public String getMonitorSpec(){
return monitorSpec;
}
public String getMouseSpec(){
return mouseSpec;
}
public String getKeyboardSpec(){
return keyboardSpec;}
public String toString(){
return "merek monitor anda" + " " + getMonitorSpec() + " " + "Merek mouse anda adalah" + " " + getMouseSpec() + " " + "Merek keyboard anda adalah " + getKeyboardSpec() ;
}
public String start(){
return "Computer is turn on… " ;}
public String shutDown(){
return "Computer is turn off";}
}
/*untuk kelas drivernya*/
class ComputerTestDrive{
public static void main(String[] args){
Computer P = new Computer();
P.setMonitorSpec("Dell");
P.setMouseSpec("optikal mouse");
P.setKeyboardSpec("sony");
System.out.println("\n" + P.toString());;
System.out.println( P.start());
System.out.println( P.shutDown());
}
}
private String mouseSpec;
private String keyboardSpec;
public void setMonitorSpec(String monitor){
monitorSpec = monitor;
}
public void setMouseSpec(String mouse){
mouseSpec = mouse;
}
public void setKeyboardSpec(String keyboard){
keyboardSpec = keyboard;}
public String getMonitorSpec(){
return monitorSpec;
}
public String getMouseSpec(){
return mouseSpec;
}
public String getKeyboardSpec(){
return keyboardSpec;}
public String toString(){
return "merek monitor anda" + " " + getMonitorSpec() + " " + "Merek mouse anda adalah" + " " + getMouseSpec() + " " + "Merek keyboard anda adalah " + getKeyboardSpec() ;
}
public String start(){
return "Computer is turn on… " ;}
public String shutDown(){
return "Computer is turn off";}
}
/*untuk kelas drivernya*/
class ComputerTestDrive{
public static void main(String[] args){
Computer P = new Computer();
P.setMonitorSpec("Dell");
P.setMouseSpec("optikal mouse");
P.setKeyboardSpec("sony");
System.out.println("\n" + P.toString());;
System.out.println( P.start());
System.out.println( P.shutDown());
}
}
Tutorial Java part-1.2
public class BeerSong{
public static void main (String[] args){
int beerNum = 99;
int temp = 0;
String word = "bottles";
while (beerNum > 0) {
System.out.println(beerNum + " " + word + " of beer on the wall" + "," + beerNum + word +" of beer");
beerNum = beerNum - 1;
System.out.println("Take one Down and pass it arround, " + " " + beerNum + " " + word + " of beer on the wall \n");
temp = temp + 1;
if (beerNum == 1) { System.out.println(beerNum + " " + word + " of beer on the wall" + "," + beerNum + word +" of beer");
beerNum = beerNum - 1;
System.out.println("Take one Down and pass it arround, " + " " + "no More " + word + " of beer on the wall \n");
temp = temp + 1;
if (beerNum == 0){
System.out.println("No more" +word + "of beer on the wall, no more " +word + " of beer.");
System.out.println("Go to the store and buy some more " + temp + " " + word + " of beer."); }
}
}
}
}
public static void main (String[] args){
int beerNum = 99;
int temp = 0;
String word = "bottles";
while (beerNum > 0) {
System.out.println(beerNum + " " + word + " of beer on the wall" + "," + beerNum + word +" of beer");
beerNum = beerNum - 1;
System.out.println("Take one Down and pass it arround, " + " " + beerNum + " " + word + " of beer on the wall \n");
temp = temp + 1;
if (beerNum == 1) { System.out.println(beerNum + " " + word + " of beer on the wall" + "," + beerNum + word +" of beer");
beerNum = beerNum - 1;
System.out.println("Take one Down and pass it arround, " + " " + "no More " + word + " of beer on the wall \n");
temp = temp + 1;
if (beerNum == 0){
System.out.println("No more" +word + "of beer on the wall, no more " +word + " of beer.");
System.out.println("Go to the store and buy some more " + temp + " " + word + " of beer."); }
}
}
}
}
Tutorial Java part-1
public class AnakAyam{
public static void main (String[] args){
int ayam = 100;
String akhir = "induknya";
while (ayam > 0) {
System.out.println("anak ayam turun " + ayam);
ayam = ayam - 1;
if (ayam == 0){
System.out.println("anak ayam mati satu tinggal " + akhir);
break;}
System.out.println("Mati satu tinggal " + " " + ayam + "\n");
}
}
}
public static void main (String[] args){
int ayam = 100;
String akhir = "induknya";
while (ayam > 0) {
System.out.println("anak ayam turun " + ayam);
ayam = ayam - 1;
if (ayam == 0){
System.out.println("anak ayam mati satu tinggal " + akhir);
break;}
System.out.println("Mati satu tinggal " + " " + ayam + "\n");
}
}
}
Langganan:
Postingan (Atom)