Contoh Java
Encapsulation.java
package encapsulation; import java.util.Scanner; public class Encapsulation { public static void main(String[] args) { Scanner Scanner; Scanner scn = new Scanner (System.in); a n = new a(); System.out.print("masukkan nilai a ="); int a=scn.nextInt(); n.setnilaiA(a); System.out.print("masukkan nilai b= "); int b=scn.nextInt(); n.setnilaiB(b); System.out.println("hasil penjumlahan +"+n.hasil_tambah()); System.out.println("hasil pengurangan +"+n.hasil_kurang()); } }
a.java
package encapsulation; public class a { private int a; private int b; public void setnilaiA(int angka1) { a=angka1; } public int getnilaiA() { return a; } public void setnilaiB(int angka2) { b=angka2; } public int getnilaiB() { return b; } public int hasil_tambah() { return getnilaiA()+getnilaiB(); } public int hasil_kurang() { return getnilaiA()-getnilaiB(); } }Contoh C#
encapsulation.cpp
#includeContoh C#using namespace std; class hitung{ private : int a; private : int b; public : void setnilaiA(int angka1){ a=angka1; } int getnilaiA(){ return a; } void setNilaiB(int angka2){ b=angka2; } int getNilaiB(){ return b; } int hasil_tambah() { return getnilaiA()+getNilaiB(); } int hasil_kurang() { return getnilaiA()-getNilaiB(); } }; int main() { hitung x; int a,b; cout << " Masukkan nilai a = "; cin >> a; x.setnilaiA(a); cout << " Masukkan nilai b = "; cin >> b; x.setNilaiB(b); cout << " Hasil tambah = " << x.hasil_tambah() << endl; cout << " Hasil pengurangan " << x.hasil_kurang() << endl; return 0; }
proses.cs
using System; namespace encapsulation { class proses { private int a=0; private int b=0; public int niliaA{ set{ a=value; } get{ return a; } } public int nilaiB{ set{ b=value; } get{ return b; } } public int hasiltambah(){ return a+b; } public int hasilkurang(){ return a-b; } }//end class }//end namespaceProgam.cs
using System; namespace encapsulation { class Program { public static void Main(string[] args) { proses a = new proses(); Console.Write(" Masukkan nilai a = "); a.niliaA = Convert.ToInt32(Console.ReadLine()); Console.Write(" Masukkan nilai b = "); a.nilaiB = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); Console.WriteLine(" Hasil Penjumlahan = "+a.hasiltambah()); Console.WriteLine(" Hasil Pengurangan = "+a.hasilkurang()); Console.ReadKey(true); } } }Semoga bermanfaat. . .
0 komentar:
Posting Komentar