Construction and Overloading: A. The Purpose

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

MODULE 3

CONSTRUCTION AND OVERLOADING

A. THE PURPOSE
Introduce the constraction, overloading constructor and overloading methode
in java term which is capable to gives initial values whe the object is being
created. Gives over view how the constructior works and called.

B. THEORETICAL BASIS
Java will call this constructor methode automaticaly when new is
used to create objects of a class.
Constructor type:

The name same as the class


Have no return (including there can be no void keyword)

The constructor overloading is a constructor creation mechanism


which has more than one shape. In this term the differentiator
between a constructor to other constructor is the parameter amount or
parameter type.
Overloading usually is used in constructor, but overlading also can be
used in non-construction methode.
C. TOOLS
1. Computer.
2. NetBeans application
D. EXPERIMENT
1. EXPERIMENT 1.CONSTRUCTOR
WORK STEPS

1.1.Create

new

java

file

named "Buku.java",

write program

code

as below.
public class Buku {
String pengarang;
String judul;
int tahunTerbit;
//constructor
public Buku(String pengarang, String judul){
this.judul=judul;
this.pengarang=pengarang;
}
//methode
public void infoBuku(){
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit);
}
}
1.2.Save and compile the codes.
1.3.Next create new class having main function as below.
public class Overloadingkonstruksi {
public static void main(String[]args){
Buku bukuku=new Buku("Dedi Gunawan","panduan pintar
kumputer",);
bukuku.infoBuku();
1.4.Save same as the name of the class and compile the code.

2. EXPERIMENT 2. OVERLOADING CONSTRUCTOR


WORK STEPS
2.1.Edit Buku.java file with some addition of overloading constructor
code as below.

public class Buku {


String pengarang;
String judul;
int tahunTerbit;
//constructor
public Buku(String pengarang,String pengarang, String judul){
pemilik=pengarang;
this.judul=judul;
this.pengarang=pengarang;
}

//constructor overloading
public Buku (String pengarang,String judul,int tahunTerbit){
this.pengarang=pengarang;
this.judul=judul;
this.tahunTerbit=tahunTerbit;
}
//methode
public void infoBuku(){
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit);
}
}
2.2.Compile the program code.
2.3.Next create new class named

"OverLoadingKonstruktor.java", and

write the following code


public class Overloadingkonstruksi {
public static void main(String[]args){
Buku bukuku=new Buku(Dedi Gunawan","panduan pintar
kumputer");
Buku bukuku1=new Buku(Dedi Gunawan","panduan pintar
kumputer",2009);
bukuku.infoBuku();
bukuku1.infoBuku();
}
}

2.4.Save and compile the program code.


2.5.Compare with previous experiment.

3. EXPERIMENT 3.OVERLOADING METHODE


3.1.Create new class name Matematika.java, write the following code.
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;}
}

3.2.Compile the program code.


3.3.Next create class with main function, as follow.
public class OverLoadingMethode {
public static void main(String []args){
System.out.println("Nilai kuadrat dari tipe data double "
+Matematika.kuadrat(20));
System.out.println("Nilai kuadrat dati tipe data integer "
+Matematika.kuadrat(20));
System.out.println("Nilai kuadrat dati tipe data Stirng "
+Matematika.kuadrat(20));
3.4.Save file appropirate to class name and compile.
E. ANALYSIS
For the result from first experiment, in the program the creation of object can
not be done anymore with following form

new Buku();
other wise in other form.
New Buku(parameter 1, parameter 2);
A cunstructor creation mechanism which has more than one shape is differed
by the amount of the parameters. Overloading can be conduct for nonconstructor methode.

F. TASK
Edit Buku.java class and Overloadingkonstruksi.java. and the required
result as following picture (modification is allowed for judul buku, pengarang
dan tahun terbit).

Solution:
public class Buku {
String pengarang;
String judul;
int tahunTerbit;
String pemilik;
public Buku(String x,String pengarang, String judul){
pemilik=x;
this.judul=judul;
this.pengarang=pengarang;
}
public Buku (String pemilik,String pengarang,String judul,int
tahunTerbit){
this.pemilik=pemilik;
this.pengarang=pengarang;
this.judul=judul;
this.tahunTerbit=tahunTerbit;

}
public Buku (String pemilik,String pengarang){
this.pemilik=pemilik;
this.pengarang=pengarang;
}
public Buku (String pemilik,String judul,int tahunTerbit){
this.pemilik=pemilik;
this.judul=judul;
this.tahunTerbit=tahunTerbit;
}
public void infoBuku(){
System.out.println("pemilik "+pemilik);
System.out.println("Judul Buku "+judul);
System.out.println("pengarang "+pengarang);
System.out.println("tahun terbit "+tahunTerbit+"\n");
}
public class Overloadingkonstruksi {
public static void main(String[]args){
Buku bukuku=new Buku("ku","panduan pintar kumputer","Dedi
Gunawan");
Buku bukuku1=new Buku("mu","panduan pintar kumputer","Dedi
Gunawan",2009);
Buku bukuku2=new Buku("kita","Dedi Gunawan");
Buku bukuku3=new Buku("kami","panduan pintar
kumputer",2009);
bukuku.infoBuku();
bukuku1.infoBuku();
bukuku2.infoBuku();
bukuku3.infoBuku();
}
}

You might also like