0% found this document useful (0 votes)
6 views3 pages

Koding Object-Oriented Programming

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Koding Object-Oriented Programming

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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)
{
[Link] = jenis;
}
public void ubahSuhu(double suhu)
{
[Link] = suhu;
}
public double suhuCel()
{
if ([Link] == "C") return [Link];
else if ([Link] == "F") return 5*([Link] - 32) / 9;
else return 0;
}
public double suhuFahr()
{
if ([Link] == "C") return 9*[Link]/5 +32;
else if ([Link] == "F") return [Link];
else return 0;
}
}

class Program
{
static void Main(string[] args)
{
termometer termo = new termometer();
[Link]("C");
[Link](37);
[Link]("suhu celcius : "+ [Link]());
[Link]("suhu fahrenheit : "+ [Link]());
[Link]();
}
}
Project 2 - Contoh penggunaan interface:
using System;
using [Link];
using [Link];

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()
{
[Link] = "rak piring";
[Link] = "bersih";
[Link] = "";
}
public void diBersihkan(){
[Link] = "bersih";
[Link] = "rak piring";
[Link] = "";
}
public void diIsi(string minuman) {
[Link] = minuman;
[Link] = "meja";
[Link] = "siap minum";
}
public void dipakai() {
[Link] = "tempat cuci piring";
[Link] = "kotor";
[Link]= "";
}
public string status() {
return "\nlokasi: " + [Link] + "\nkondisi: " + [Link] +
"\nisi : " + [Link];
}
}
class Program
{
static void Main(string[] args)
{
[Link]("\n*** buat class cangkir baru");
cangkir biru = new cangkir();
[Link]([Link]());
[Link]("\n*** isi dengan susu");
[Link]("susu");
[Link]([Link]());
[Link]("\n*** diminum");
[Link]();
[Link]([Link]());
[Link]("\n*** cangkir dicuci");
[Link]();
[Link]([Link]());
[Link]();
}
}

You might also like