Posted by : Octo The Azura
Selasa, 07 April 2015
THIRD MEETING : CONSTRUCTOR AND
OVERLOADING
|
|
NAMA
|
RAHMATUL MAHDALINA
|
STUDENT NUMBER
|
L200134004
|
CLASS
|
X
|
A. Objective
1. Students are understand about constructor and overloading
2. Students are understand about difference between constructor and overloading
B. Basic
Teory
Constructor is
method that use for give beginning value when the object created. This method
will calling automatically by java when new used for create object from a
class.
Characteristic
C. Tools
and Materials
· Laptop
· Practice Object Oriented Programming
Modul
· Software NetBeans IDE 8.0.2
· Java Development Kit (JDK) 1.8.0_31
D. Work
Step
EXPERIMENT 1
|
Steps
of experiment:
1.
Create java class with name “Buku.java”, write the program
code:
package Konstruktordanoverloading;
/**
*
* @author
Octopus_girl
*/
public class Buku {
String
pengarang;
String
judul;
int
tahunTerbit;
//membuat
konstruktor
public
Buku(String pengarang, String judul){
this.judul = judul;
this.pengarang = pengarang;
}//akhir
konstruktor
//membuat
method
public void
infoBuku(){
System.out.println("Judul Buku
: "+judul);
System.out.println("Pengarang
: "+pengarang);
System.out.println("Tahun Terbit : "+tahunTerbit);
}
}
2.
Compile code
3.
Create new class that having main
function()
package
Konstruktordanoverloading;
/**
*
* @author
Octopus_girl
*/
public class OverloadingKonstruktor {
public
static void main(String[]args){
Buku
bukuku = new Buku("Dedi Gunawan","Panduan Pintar
Komputer");
bukuku.infoBuku();
}
}
4. Save a new class with class name and compile
that program code. In that program object creation can not use:
new Buku();
melainkan harus dalam
bentuk
new Buku(Parameter
1, Parameter 2);
EXPERIMENT 2
|
Steps
of experiment:
1.
Create java class with name “Buku.java”, write the program
code:
package Konstruktordanoverloading;
/**
*
* @author
Octopus_girl
*/
public class Buku {
String
pengarang;
String
judul;
int tahunTerbit;
//membuat
konstruktor
public
Buku(String pengarang, String judul){
this.judul = judul;
this.pengarang = pengarang;
}//akhir
konstruktor
//membuat
overloading konstruktor
public
Buku(String pengarang, String judul, int tahunTerbit){
this.judul = judul;
this.pengarang = pengarang;
this.tahunTerbit = tahunTerbit;
}
//membuat
method
public void
infoBuku(){
System.out.println("Judul Buku
: "+judul);
System.out.println("Pengarang : "+pengarang);
System.out.println("Tahun Terbit : "+tahunTerbit);
}
}
2.
Compile code
3.
Create new class with name “OverLoadingKonstruktor.java”,
write like picture
package Konstruktordanoverloading;
/**
*
* @author
Octopus_girl
*/
public class OverloadingKonstruktor {
public
static void main(String[]args){
Buku
bukuku = new Buku("Dedi Gunawan","Panduan Pintar
Komputer");
Buku
bukumu = new Buku("Dedi Gunawan","Panduan Pintar
Komputer",2009);
bukuku.infoBuku();
bukumu.infoBuku();
}
}
4.
Compile and save
5.
Compare the result
EXPERIMENT 3
|
Steps
of experiment:
1. Create java class with name “Matematika.java”,
write the program code:
package Konstruktordanoverloading;
/**
*
* @author
Octopus_girl
*/
public class Matematika {
static public
double kuadrat(double nilai){
return
nilai*nilai;
}
static public
int kuadrat(int nilai){
return
nilai*nilai;
}
static public
double kuadrat(String nilai){
double
bilangan;
bilangan=
Double.valueOf(nilai).doubleValue();
return
bilangan*bilangan;
}
}
2.
Compile code
3. Create new class that having main function()
package Konstruktordanoverloading;
/**
*
*
@author Octopus_girl
*/
public class OverloadingMethod {
public static void main(String []args){
System.out.println("Nilai Kuadrat dari tipe data
double"+Matematika.kuadrat(20));
System.out.println("Nilai Kuadrat dari tipe data
integer"+Matematika.kuadrat(20));
System.out.println("Nilai Kuadrat dari tipe data
Strig"+Matematika.kuadrat(20));
}
}
4.
Save file suite to class name and compile
E. Analysis
The constructor
just can access by the same data type like String or same parameter, but overloading
constructor can be access by different data type or some parameter. Then
overloading can be access in constructor or non-constructor.
F. Assignment
package Konstruktordanoverloading;
/**
*
* @author Octopus_girl
*/
public class Buku {
String jenis;
String pengarang;
String judul;
int tahunTerbit;
//membuat konstruktor
public Buku(String
jenis,String pengarang, String judul){
this.jenis = jenis;
this.judul = judul;
this.pengarang =
pengarang;
}//akhir konstruktor
//membuat overloading
konstruktor
public Buku(String
jenis,String pengarang, String judul, int tahunTerbit){
this.jenis = jenis;
this.judul = judul;
this.pengarang =
pengarang;
this.tahunTerbit =
tahunTerbit;
}
public Buku(String
jenis,String pengarang){
this.jenis = jenis;
this.pengarang =
pengarang;
}
public Buku(String
jenis,String judul, int tahunTerbit){
this.jenis = jenis;
this.judul = judul;
this.tahunTerbit =
tahunTerbit;
}
//membuat method
public void infoBuku(){
System.out.println(""+jenis);
System.out.println("Judul Buku
: "+judul);
System.out.println("Pengarang
: "+pengarang);
System.out.println("Tahun Terbit : "+tahunTerbit);
System.out.println("");
}
}
package Konstruktordanoverloading;
/**
*
* @author Octopus_girl
*/
public class OverloadingKonstruktor {
public static void main(String[]args){
Buku bukuku = new Buku("Buku
Ku","Lina azure","Semua Tentang Kita");
Buku bukumu = new Buku("Buku
Mu","Rio de ro","Rain",2010);
Buku bukukita = new Buku("Buku
Kita","Alice");
Buku bukukami = new Buku("Buku
Kami","Greenfoot",2011);
bukuku.infoBuku();
bukumu.infoBuku();
bukukita.infoBuku();
bukukami.infoBuku();
}
}