Skip to main content

Object, Class, Message and Method JAVA

Object, Class, Message and Method JAVA

Object, Class, Message and Method

In object-oriented programming, a class is a template for structure definition and object creation. So the object of an instance of a class, that class is a collection of plans that specify how to build an object.

Object and Class

Information-related information and describes an object called object attributes (object attribute). This attribute allows an object has a value attribute that stands alone. Besides object attributes also have an action called by the method. So the method is a set of operations on an object in the form of code instructions.

General syntax format-making class is as follows:
class classname {
// declare instance variables var1 type;
var2 type;
// ... type varN;
// declare methods
type method1 (parameters) {
// body of method
}
type method2 (parameters) {
// body of method
}
// ...
type methodN (parameters) {
// body of method
}
}

Defining Class

Consider the example of a class definition of vehicles consisting of three instance variables and the making of objects of that class, such as vehicle class pembuaan below:
1public class Vehicle {
2int passenger; // number of people in the vehicle
3int capacity_bb; // fuel capacity
4int mpg; // consumption of miles per gallon of fuel
5}

Creating Objects

Objects created by the pre-defined class that is a class above the previous vehicle. 
so coding for the object as follows:
01public class VehicleObjects {
02public static void main (String [] args) {
03Vehicle minivan = new Vehicle ();
04Vehicle sportscar = new Vehicle ();
05int range1, range2;
06minivan.passenger = 7;
07minivan.capacity_bb = 16;
08minivan.mpg = 21;
09sportscar.passenger = 2;
10sportscar.capacity_bb = 14;
11sportscar.mpg = 12;
12range1 = minivan. capacity_bb * minivan.mpg;
13range2 = sportscar. capacity_bb * sportscar.mpg;
14System.out.println ("Minivan can carry "
15+ minivan. passenger + " person with range " + range1);
16System.out.println ("Sportscar can bring "
17+ sportscar. passenger + " person with range " + range2);
18}
19}

Creating Objects Java
coding results

Adding Method into Class

Class that has been made over there just an instance variable, now add some operation (method) into the class.

vehicle class with the method:
01public class Vehicle {
02int passenger; // number of people in the vehicle
03int capacity_bb; // fuel capacity
04int mpg; // consumption of miles per gallon of fuel
05String name = new String ();
06int getRange () {
07return mpg*capacity_bb;
08}
09double getNumBB (int miles) {// amount of fuel needs
10return (double) miles / mpg;
11}
12}

objekvehicle class with the method:
01public class VehicleObjects {
02public static void main (String [] args) {
03Vehicle minivan = new Vehicle ();
04Vehicle sportscar = new Vehicle ();
05double gallon;
06int distance = 259;
07minivan.name = "Minivan";
08minivan.passenger = 7;
09minivan.capacity_bb = 16;
10minivan.mpg = 21;
11sportscar.name = "Sportscar";
12sportscar.passenger = 2;
13sportscar.capacity_bb = 14;
14sportscar.mpg = 12;
15gallon = minivan.getNumBB (distance);
16System.out.println (minivan.name + " requires " + gallon +
17" gallons to travel distance " + distance + " miles.");
18gallon = sportscar.getNumBB (distance);
19System.out.println (sportscar.name + " requires " + gallon +
20" gallons to travel distance " + distance + " miles.");
21}
22}
Adding Method into Class Java
coding results

constructor

A constructor is an object initialization when it is created.  Constructor name equal to the name of its class. 

vehicle class with a constructor:
01public class Vehicle {
02int passenger; // number of people in the vehicle
03int capacity_bb; // fuel capacity
04int mpg; // consumption of miles per gallon of fuel
05String name = new String ();
06Vehicle (String name, int passenger, int capacity, int mpg) {
07this.name = name;
08this.passenger = passenger;
09this.capacity_bb = capacity;
10this.mpg = mpg;
11}
12int getRange () {
13return mpg * capacity_bb;
14}
15double getNumBB (int miles) {// amount of fuel needs
16return (double) miles / mpg;
17}
18}

objekvehicle class with a constructor:
01public class VehicleObjects {
02public static void main (String [] args) {
03Vehicle minivan = new Vehicle ("Minivan"7,16,21);
04Vehicle sportscar = new Vehicle ("Sportscar"2,14,12);
05double gallon;
06int distance = 259;
07gallon = minivan.getNumBB (distance);
08System.out.println (minivan.name + " requires " + gallon +
09" gallons to travel distance " + distance + " miles.");
10gallon = sportscar.getNumBB (distance);
11System.out.println (sportscar.name + " requires " + gallon +
12" gallons to travel distance " + distance + " miles.");
13}
14}
constructor JAVA
coding results

Comments

Popular posts from this blog

Menambahkan Peraturan Komentar Pada Blog

Menambahkan Peraturan Komentar Pada Blog : Ngblog Menulis sebuah artikel dan dikomentari dengan link aktif/promosi produk dan kata kata spam lainnya memang tidak mengenakan anda mungkin akan sangat merasa kecewa sekali, anda mungkin seperti menulis sebuah artikel namun tidak dihargai karyanya oleh orang lain. Mungkin membuat aturan berkomentar di blog adalah salah 1 cara menetralisirnya, mungkin jika ada aturan tersebut orang berpikir 2 kali untuk meninggalkan komentar Spam, lalu bagaimana cara membuat aturan komentar tersebut? langsung saja kita praktekan Ikuti langkah berikut : Login ke bloger,masuk ke blog yang anda inginkan, Klik Setelan -> POS-KOMENTAR   Scroll kebawah dan cari menu "pesan formulir komentar" klik tambahkan , dan isi kolom sesuai keinginan kalian atau peraturan yang kalian inginkan.  Setelah selesai silakan klik simpan setelan dipojok kanan atas. Lihat hasilnya di blog kalian ,akan seperti ini : Jika ada pertanyaan...

Traha Infinity a lancé une campagne événementielle de pré-inscription

Moai Games a officiellement annoncé la sortie d’ un  nouveau  MMORPG  intitulé  Traha Infinity , dans la première moitié de 2022. Ce jeu mobile devrait se dérouler 200 ans avant les événements des précédents jeux  Traha , il a  des  graphismes  Full 3D  et optimisés performances haut de gamme pour smartphones. Traha a  été un énorme succès en Corée avec 4,2 millions de précommandes avant sa sortie officielle en 2019, le jeu mobile n’a jamais eu de sortie mondiale. Ce jeu a été développé par Moai Games et publié par  Nexon , et est devenu l’un des jeux les plus vendus de l’époque. Et maintenant,  Traha Infinity  continuera le même monde comme une suite, plutôt une préquelle. Le nouveau jeu présentera la même vision du monde et la même topographie que le jeu précédent, mais présentera un nouveau scénario. Ils se vantent d’ une qualité graphique  Full 3D haut  de gamme ainsi que d’un système optimisé pour les smartphones. Les joueurs pourront explorer l...

Contoh Program C++ : Konversi Bilangan Hexadesimal, Oktal , Biner.

Hai Pembaca FajarYusuf.Com yang pintar dan berwawasan tinggi, kali ini FajarYusuf.Com akan post Contoh Program C++ : Konversi Bilangan Hexadesimal, Oktal , Biner . Mari kita membuat program konversi bilangan yang sangat mudah di buat dengan menggunakan bahasa pemrograman C++. Yaitu dengan menggunakan type data long, untuk Hexadesimal dengan rumus : <<hex<<desimal (merubah dari bilangan Desimal ke Hexadesimal) , untuk Oktal dengan rumus : <<oct<<desimal (merubah dari bilangan Desimal ke Oktal). Dan untuk biner dilakukan pembagian 2. Contoh Coding Program C++ Konversi Bilangan: #include<iostream> using namespace std; int main() { long desimal, pembagi=1073741824, bit; cout<<"Nilai Desimal : "; cin>>desimal; cout<<"Hexadesimal = "<<hex<<desimal<<endl; cout<<"Oktal = "<<oct<<desimal<<endl; ...