0% found this document useful (0 votes)
31 views

Koding-Koding Yang Dipakai Pada Chapter 4 Object Oriented Programming (Class, Inheritance, Encapsulation Dan Interface) Project 1

The document discusses two projects using object-oriented programming concepts in C#. The first project defines a termometer class with methods to change the type and temperature and return temperatures in Celsius or Fahrenheit. The second project defines an interface for a glass and a cangkir (cup) class that implements it, with methods to clean, fill, and use the cup and a status method. The main method demonstrates creating a cangkir object and calling its methods.

Uploaded by

Tadonny Vani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Koding-Koding Yang Dipakai Pada Chapter 4 Object Oriented Programming (Class, Inheritance, Encapsulation Dan Interface) Project 1

The document discusses two projects using object-oriented programming concepts in C#. The first project defines a termometer class with methods to change the type and temperature and return temperatures in Celsius or Fahrenheit. The second project defines an interface for a glass and a cangkir (cup) class that implements it, with methods to clean, fill, and use the cup and a status method. The main method demonstrates creating a cangkir object and calling its methods.

Uploaded by

Tadonny Vani
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Koding-koding yang dipakai pada chapter 4

Object Oriented Programming


(Class, Inheritance, Encapsulation dan Interface)

Project 1
public class termometer
{
private string jenis;
private double suhu;
public void ubahJenis(string jenis)
{
this.jenis = jenis;
}
public void ubahSuhu(double suhu)
{
this.suhu = suhu;
}
public double suhuCel()
{
if (this.jenis == "C") return this.suhu;
else if (this.jenis == "F") return 5*(this.suhu - 32) / 9;
else return 0;
}
public double suhuFahr()
{
if (this.jenis == "C") return 9*this.suhu/5 +32;
else if (this.jenis == "F") return this.suhu;
else return 0;
}
}

class Program
{
static void Main(string[] args)
{
termometer termo = new termometer();
termo.ubahJenis("C");
termo.ubahSuhu(37);
Console.WriteLine("suhu celcius : "+ termo.suhuCel());
Console.WriteLine("suhu fahrenheit : "+ termo.suhuFahr());
Console.ReadKey();
}
}
Project 2 - Contoh penggunaan interface:
using System;
using System.Collections.Generic;
using System.Text;

public interface iGelas


{
void diBersihkan();
void diIsi(string minuman);
void dipakai();
}

public class cangkir : iGelas


{
private string lokasi;
private string kondisi;
private string isi;
public cangkir()
{
this.lokasi = "rak piring";
this.kondisi = "bersih";
this.isi = "";
}
public void diBersihkan(){
this.kondisi = "bersih";
this.lokasi = "rak piring";
this.isi = "";
}
public void diIsi(string minuman) {
this.isi = minuman;
this.lokasi = "meja";
this.kondisi = "siap minum";
}
public void dipakai() {
this.lokasi = "tempat cuci piring";
this.kondisi = "kotor";
this.isi= "";
}
public string status() {
return "\nlokasi: " + this.lokasi + "\nkondisi: " + this.kondisi +
"\nisi : " + this.isi;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n*** buat class cangkir baru");
cangkir biru = new cangkir();
Console.WriteLine(biru.status());
Console.WriteLine("\n*** isi dengan susu");
biru.diIsi("susu");
Console.WriteLine(biru.status());
Console.WriteLine("\n*** diminum");
biru.dipakai();
Console.WriteLine(biru.status());
Console.WriteLine("\n*** cangkir dicuci");
biru.diBersihkan();
Console.WriteLine(biru.status());
Console.ReadKey();
}
}

You might also like