MODUL 1
CLASS
A. Objective
To Understand what is class and make
student can create class it self.
B. Basic
Teory
Class is blueprint from object.
Some object can created from project that called class. Basic on this
definition, then a sufficient class made just
one part of the class and can be created / unloaded several objects (tens or
even hundreds) that have the same properties
C. Tools
and Materials
·
Laptop
·
Software
NetBeans
·
Java Development
Kit (JDK)
D. Work
Step
This is some implementation of
bicycle class :
public class
bicycle {
int candance;
int speed;
int gear;
void changeCandance (int newValue){
candance
= newValue;
}
void changeGear (int newValue){
gear
= newValue;
}
void speedUp (int increment){
speed =
speed + increment;
}
void applyBreak (int decrement){
speed =
speed - decrement;
}
void printStates(){
System.out.println("Speed = "+speed+", "+"Gear
= "+gear+", "+"Candance =
"+candance);
}
}
Class Bicycle above do not have a main function (main method) because this class is not
an application program but merely a blueprint to create. To run an application program,
it must be given a
class that has
a primary function.
public class
BicycleDemo {
public static void main(String
[]args){
bicycle bike1 = new bicycle();
bicycle bike2 = new bicycle();
bike1.changeCandance(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCandance(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCandance(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
Execute :
run:
Speed = 10, Gear = 2, Candance = 50
Speed = 20, Gear = 3, Candance = 40
BUILD SUCCESSFUL
(total time: 2 seconds)
E. Analysis
To make class we must have
blueprint and main method to run some java program.
Ern4t
F. Assignment
1.
Create a class that can present the properties of the object cat. This
object has the
form {umur, warna bulu} and function of the form {meow, ulang tahun}.
Code
:
public class cat {
String warnabulu;
int umur;
void changeWarnabulu
(String colour){
warnabulu
= colour;
}
void
changeUmur (int newValue){
umur
= newValue;
}
void printState(){
System.out.println("WarnaBulu = "+warnabulu+", "+"meong,
"+"Ulang tahun ke = "+umur);
}
}
public class catDemo {
public static
void main(String []args){
cat cat1 = new cat();
cat cat2 = new cat();
cat1.changeWarnabulu("White");
cat1.changeUmur(2);
cat1.printState();
cat2.changeWarnabulu("Black");
cat2.changeUmur(1);
cat2.printState();
}
}
Execute
:
run:
WarnaBulu
= White, meong, Ulang tahun ke = 2
WarnaBulu
= Black, meong, Ulang tahun ke = 1
BUILD SUCCESSFUL (total time: 2 seconds)
2.
One
of the application PBO is a very common form of financial applications.
Bank Account is one of the things that can be used
as an object in
the PBO.
i.
Create a class that can present the account object.
variables of this object is
{saldo, no rek, nama} and function of the form
{cek saldo, menabung, menarik, and transfer}.
ii.
Create a class that has a main function that is used to demonstrate the
making of the object.
Code :
public
class ObjectRekening
{
int saldo;
int noRek;
int sisa;
int transfer;
int menabung;
int checksaldo;
String nama;
void
changesaldo (int newValue){
saldo
= newValue;
System.out.println("Saldo Anda = "+saldo);
}
void
changenorek (int newValue){
noRek
= newValue;
System.out.println("Nomer Rekening Anda = "+noRek);
}
void
changenama (String name){
nama
= name;
System.out.println("Nama Anda = "+nama);
}
void
changetransfer (int newValue){
transfer
= saldo - newValue;
checksaldo
= transfer;
System.out.println("Dana yang Di Transfer = "+newValue);
}
void changemenabung (int newValue){
menabung
= saldo + newValue;
checksaldo
= menabung;
System.out.println("Anda Menabung = "+newValue);
}
void checksaldo (){
saldo
= checksaldo;
System.out.println("Saldo Anda = "+saldo);
}
}
public
class ObjectRekeningDemo
{
public static
void main(String []args){
ObjectRekening Rekening1 = new ObjectRekening();
Rekening1.changenama("Fauzan");
Rekening1.changesaldo(10000);
Rekening1.changenorek(123456789);
Rekening1.changemenabung(25000);
Rekening1.checksaldo();
Rekening1.changetransfer(15000);
Rekening1.checksaldo();
Rekening1.changemenabung(5000);
Rekening1.checksaldo();
Rekening1.changetransfer(2000);
Rekening1.checksaldo();
}
}
Execute :
run:
Nama Anda = Fauzan
Saldo Anda = 10000
Nomer Rekening Anda =
123456789
Anda Menabung = 25000
Saldo Anda = 35000
Dana yang Di Transfer =
15000
Saldo Anda = 20000
Anda Menabung = 5000
Saldo Anda = 25000
Dana yang Di Transfer =
2000
Saldo Anda = 23000
BUILD
SUCCESSFUL (total time: 0 seconds)
3.
Note the string class is in the java library.
specify a list of variables
and functions held by the string class.
Homework
:
1. Create a class Kalimat
is an
application program to count the number of letters
of a sentence
and also can convert
the sentence into a sentence with a capital letter.
Code :
public class
Kalimat {
String w;
void changeword
(String newword){
w =
newword;
System.out.println("Your words = "+w);
}
void
counting (){
System.out.println("The Amount Character = "+w.length());
}
void
enlarge (){
System.out.println(w.toUpperCase());
}
}
public class DemoKalimat {
public static
void main(String []args){
Kalimat kalimat1 = new Kalimat();
kalimat1.changeword("Teknik
Informatika UMS");
kalimat1.counting();
kalimat1.enlarge();
}
}
Execute
:
run:
Your
words = Teknik Informatika UMS
The
Amount Character = 22
TEKNIK
INFORMATIKA UMS
BUILD SUCCESSFUL (total time: 0 seconds)
Tidak ada komentar:
Posting Komentar