Koding-Koding Yang Dipakai Pada Chapter 4 Object Oriented Programming (Class, Inheritance, Encapsulation Dan Interface) Project 1
Koding-Koding Yang Dipakai Pada Chapter 4 Object Oriented Programming (Class, Inheritance, Encapsulation Dan Interface) Project 1
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;