1public class Lightsaber {
2 // properties
3 private boolean isOn;
4 private Color color;
5
6 // constructor
7 public Lightsaber(Color color) {
8 this.isOn = false;
9 this.color = color;
10 }
11
12 // getters
13 public Color getColor() {
14 return color;
15 }
16 public boolean getOnStatus() {
17 return isOn;
18 }
19
20 // setters
21 public void turnOn() {
22 isOn = true;
23 }
24 public void turnOff() {
25 isOn = false;
26 }
27}
28
29
30
31// Implementation in main method:
32public class test {
33 public static void main(String[] args) {
34 Lightsaber yoda = new Lightsaber(green);
35 yoda.turnOn();
36 }
37}
38
1// this might help you understand how classes work
2public class MathTest {
3
4 public static void main(String[] args) {
5
6 class MathAdd {
7
8 int num1;
9 int num2;
10
11 public int addNumbers() {
12
13 int addThemUp = num1 + num2;
14 return addThemUp;
15 }
16 }
17
18 MathAdd addition = new MathAdd(); // create a new instance of the class
19
20 // you can access variables from the class
21 addition.num1 = 10;
22 addition.num2 = 20;
23
24 // and use the method from the class to add them up
25 System.out.println(addition.addNumbers());
26
27 }
28}
1
2public class HelloWorld {
3 public static void main(String[] args) {
4
5 // how to use class in java
6 class User{
7
8 int score;
9 }
10
11 User dave = new User();
12
13 dave.score = 20;
14
15 System.out.println(dave.score);
16
17
18 }
19}
20
1Class is a blueprint or template which
2you can create as many objects as you
3like. Object is a member or instance
4of a class.
5Class is declared using class keyword,
6Object is created through
7new keyword mainly. A class is a template
8 for objects. A class defines
9object properties including a valid range
10 of values, and a default value.
11A class also describes object behavior.
1// Example : TestClass (Can be predefined or user-defined)
2public class TestClass {
3 // properties
4 private int id = 111;
5
6 // constructor
7 public TestClass(){
8 super();
9 }
10
11 // method
12 public void test(){
13 // some code here
14 }
15}
1abstract class Pesan {
2 public void success() {
3 System.out.println("Mobil Berhasil Dibeli");
4 }
5
6 public void error() {
7 System.out.println("Uang Anda Tidak Cukup");
8 }
9}
10
11class Car extends Pesan {
12 protected String nama = "toyota supra";
13 protected String warna = "merah";
14 protected int harga = 2000000000;
15 protected String brand = "toyota";
16}
17
18class ShowRoom extends Car {
19 protected String namaShowroom = "Catur Sentosa Raya";
20 protected String alamatShowroom = "Jl.siliwangin kec pancoranmas kota depok 16436";
21}
22
23class Pembeli extends ShowRoom {
24 protected String namaPembeli = "anto jayabaya";
25 protected String alamatPembeli = "jl.swadaya rt.01/rw.04 no.112 kec pancoranmas kota depok";
26 protected int saldoPembeli = 50000000;
27}
28
29class BeliMobil extends Pembeli {
30
31 public BeliMobil(String nama, String warna, int harga, String brand, String nsr, String asr, String np, String ap, int sdp) {
32 super();
33
34 super.nama = nama;
35 super.warna = warna;
36 super.harga = harga;
37 super.brand = brand;
38 super.namaShowroom = nsr;
39 super.alamatShowroom = asr;
40 super.namaPembeli = np;
41 super.alamatPembeli = ap;
42 super.saldoPembeli = sdp;
43 }
44
45void getResult(String nama, String warna, int harga, String brand, String np, String ap) {
46
47 if(super.harga > super.saldoPembeli) {
48 System.out.println("=======================");
49 super.error();
50 System.out.println("=======================");
51 } else {
52 System.out.println("=======================");
53 super.success();
54 System.out.println("=======================");
55 System.out.println("");
56 System.out.println("=======================");
57 System.out.println("Jenis Mobil");
58 System.out.println("=======================");
59 System.out.println("");
60 System.out.println("Nama Mobil:" + nama);
61 System.out.println("Warna Mobil:" + warna);
62 System.out.println("Harga Mobil:" + harga);
63 System.out.println("Brand Mobil:" + brand);
64 System.out.println("");
65 System.out.println("=======================");
66 System.out.println("Nama Pembeli Mobil");
67 System.out.println("=======================");
68 System.out.println("Nama Pembeli:" + np);
69 System.out.println("Nama Pembeli:" + ap);
70 }
71 }
72
73 public static void main(String[] args) {
74 BeliMobil beli = new BeliMobil("avanza", "hitam", 128000000, "toyota", "Jaya Mobil", "Jakarta", "Anton", "Depok", 228000000);
75 beli.getResult(beli.nama, beli.warna, beli.harga, beli.brand, beli.namaPembeli, beli.alamatPembeli);
76 }
77}
78