A.
Objectives
Objectives
of making this report are :
1.
Students are able
to understand what the definition and how to use Constructor in Java.
2.
Students are
able to understan what the definition and how to use Overloading in Java.
B.
Theoretical Basis
Constructor
is a method used to give starting value when object created. This method will
be callled automatically by Java when new
used to create object from a class. Characteristic of constructor :
·
Name of
constructor is same with name of class.
·
Constructor
doesn’t have return value (included it may not present void keyword).
C.
Tools and Material
1.
Laptop
2.
Java Development
Kit (JDK 8)
3.
NetBeans IDE
8.0.2
D.
Work Steps
1.
Experiment 1
a.
Creating project
and Source Packages required below.
·
Project Name : Modul 3
·
Source Packages
Name : Percobaan 1
b. Creating new java
file named “Buku.java”. Typing program code below :
package Percobaan1;
/**
*
* @author Jhonny
*/
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);
}
}
/**
*
* @author Jhonny
*/
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);
}
}
c.
Saving and
compiling the program codes.
d.
Creating a new
java class that has function main() as
like program codes below.
package Percobaan1;
/**
*
* @author Jhonny
*/
public class OverLoadingKonstruktor {
public static void main (String []args){
Buku bukuku = new Buku("John","Panduan Pintar Guitar");
bukuku.infoBuku();
}
}
/**
*
* @author Jhonny
*/
public class OverLoadingKonstruktor {
public static void main (String []args){
Buku bukuku = new Buku("John","Panduan Pintar Guitar");
bukuku.infoBuku();
}
}
e.
Saving as the
class name and compiling the program codes above.
Object creating may not be typed as :
Object creating may not be typed as :
New Buku();
But it should be like
this :
New Buku (Parameter1, Parameter2);
New Buku (Parameter1, Parameter2);
2.
Experiment 2 (Constructor Overloading)
a.
Editing file of “Buku.java”
by added constructor code of overloding.
package Percobaan1;
/**
*
* @author Jhonny
*/
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.pengarang = pengarang;
this.judul = judul;
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+ "\n");
}
}
/**
*
* @author Jhonny
*/
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.pengarang = pengarang;
this.judul = judul;
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+ "\n");
}
}
b.
Compiling the
program codes.
c.
Next, creating a
new class named “OverLoadingKonstruktor.java” then typing program code below.
package Percobaan1;
/**
*
* @author Jhonny
*/
public class OverLoadingKonstruktor {
public static void main (String []args){
Buku bukuku = new Buku("John","Panduan Pintar Guitar");
Buku bukumu = new Buku("John","Panduan Pintar Guitar",2015);
bukuku.infoBuku();
bukumu.infoBuku();
}
}
/**
*
* @author Jhonny
*/
public class OverLoadingKonstruktor {
public static void main (String []args){
Buku bukuku = new Buku("John","Panduan Pintar Guitar");
Buku bukumu = new Buku("John","Panduan Pintar Guitar",2015);
bukuku.infoBuku();
bukumu.infoBuku();
}
}
e.
Comparing
between this result and the result before.
3.
Overloading Method
a.
Creating a new
class named “Matematika.java”, typing program codes below.
package Percobaan1;
/**
*
* @author Jhonny
*/
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;
}
}
/**
*
* @author Jhonny
*/
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;
}
}
b.
Compiling
program codes above.
c.
Then, creating a
new class with function of main() below.
package Percobaan1;
/**
*
* @author Jhonny
*/
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 Sting "+Matematika.kuadrat(20));
}
}
/**
*
* @author Jhonny
*/
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 Sting "+Matematika.kuadrat(20));
}
}
E.
Analysis
Overloading
the constructor means a mechanism of constructor creation that has form more
than one. In this case, distinguishing among one constructor and other
constructors is a number of parameters or type of parameters.
Overloading
usually used on constructor, but overloading also can be used on method of
non-constructor.
F.
Tasks
1.
Editing class of
“Buku.java” and class of “OverLoadingKonstruktor.java”
2.
Program codes :
package Percobaan1;
/**
*
* @author Jhonny
*/
public class OverLoadingKonstruktor {
public static void main (String []args){
Buku bukuku = new Buku("John","Panduan Pintar Guitar");
Buku bukumu = new Buku("John","Panduan Pintar Guitar",2015);
Buku bukuKita = new Buku("Sungha","Null");
Buku bukuKami = new Buku("Jung","Panduan Pintar Komputer",2009);
System.out.println("Bukuku");
bukuku.infoBuku();
System.out.println("Bukumu");
bukumu.infoBuku();
System.out.println("BukuKita");
bukuKita.infoBuku();
System.out.println("BukuKami");
bukuKami.infoBuku();
}
}
/**
*
* @author Jhonny
*/
public class OverLoadingKonstruktor {
public static void main (String []args){
Buku bukuku = new Buku("John","Panduan Pintar Guitar");
Buku bukumu = new Buku("John","Panduan Pintar Guitar",2015);
Buku bukuKita = new Buku("Sungha","Null");
Buku bukuKami = new Buku("Jung","Panduan Pintar Komputer",2009);
System.out.println("Bukuku");
bukuku.infoBuku();
System.out.println("Bukumu");
bukumu.infoBuku();
System.out.println("BukuKita");
bukuKita.infoBuku();
System.out.println("BukuKami");
bukuKami.infoBuku();
}
}
cara agar bisa input manual gimana gan
ReplyDelete