Cara membuat Class, Cara membuat Method, Cara membuat Constructor/ Konstruktor pada Visual Studio
VERSION 1
MAIN CLASS
INPUT
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Praktek_PemVis1
{
class Program
{
static void Main(string[]
args)
{
Mahasiswa
mhs1 = new Mahasiswa();
mhs1.Nim = "09.12.0171";
mhs1.Nama = "Budi";
mhs1.Jurusan = "Sistem
Informasi";
mhs1.Tgl_lahir = DateTime.Parse("1990-03-12");
mhs1.Ipk = 3.45;
Console.Write("Nilai Algoritma "+mhs1.Nim+" = ");
mhs1.Nilai_algoritma = int.Parse(Console.ReadLine());
Console.Write("Nilai SPBD " + mhs1.Nim + " = ");
Console.Write("Nilai PBO " + mhs1.Nim + " = ");
mhs1.Nilai_pbo = int.Parse(Console.ReadLine());
mhs1.tampilProfil(mhs1);
mhs1.Nilai(mhs1);
mhs1.Jumlah_nilai =
mhs1.Nilai_algoritma + mhs1.Nilai_spbd + mhs1.Nilai_pbo;
mhs1.Jumlah_Nilai(mhs1);
mhs1.Nilai_rata2 =
mhs1.Jumlah_nilai / 3;
mhs1.NilaiRata2(mhs1);
Console.WriteLine("");
Mahasiswa
mhs2 = new Mahasiswa();
mhs2.Nim = "10.11.1538";
mhs2.Nama = "Valian";
mhs2.Jurusan = "Teknik Informatika";
mhs2.Tgl_lahir = DateTime.Parse("1992-10-16");
mhs2.Ipk = 3.54;
Console.Write("Nilai Algoritma " + mhs2.Nim + " = ");
mhs2.Nilai_algoritma = int.Parse(Console.ReadLine());
Console.Write("Nilai SPBD " + mhs2.Nim + " = ");
mhs2.Nilai_spbd = int.Parse(Console.ReadLine());
Console.Write("Nilai PBO " + mhs2.Nim + " = ");
mhs2.Nilai_pbo = int.Parse(Console.ReadLine());
mhs2.tampilProfil(mhs2);
mhs2.Nilai(mhs2);
mhs2.Jumlah_nilai =
mhs2.Nilai_algoritma + mhs2.Nilai_spbd + mhs2.Nilai_pbo;
mhs2.Jumlah_Nilai(mhs2);
mhs2.Nilai_rata2 =
mhs2.Jumlah_nilai / 3;
mhs2.NilaiRata2(mhs2);
Console.WriteLine("");
Mahasiswa
mhs3 = new Mahasiswa("12.12.0018", "Jessica",
"Sistem Informasi",
DateTime.Parse("1994-07-09"),
3.67);
Console.Write("Nilai Algoritma " + mhs3.Nim + " = ");
mhs3.Nilai_algoritma = int.Parse(Console.ReadLine());
Console.Write("Nilai SPBD " + mhs3.Nim + " = ");
mhs3.Nilai_spbd = int.Parse(Console.ReadLine());
Console.Write("Nilai PBO " + mhs3.Nim + " = ");
mhs3.Nilai_pbo = int.Parse(Console.ReadLine());
mhs3.tampilProfil();
mhs3.Nilai(mhs3);
mhs3.Jumlah_nilai =
mhs3.Nilai_algoritma + mhs3.Nilai_spbd + mhs3.Nilai_pbo;
mhs3.Jumlah_Nilai(mhs3);
mhs3.Nilai_rata2 =
mhs3.Jumlah_nilai / 3;
mhs3.NilaiRata2(mhs3);
Console.WriteLine("");
Console.Read();
}
}
}
CLASS MAHASISWA
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Praktek_PemVis1
{
class Mahasiswa
{
//field
string
nim, nama, jurusan;
public string Jurusan
{
get
{ return jurusan; }
set
{ jurusan = value; }
}
public string Nama
{
get
{ return nama; }
set
{ nama = value; }
}
public string Nim
{
get
{ return nim; }
set
{ nim = value; }
}
DateTime
tgl_lahir;
public DateTime Tgl_lahir
{
get
{ return tgl_lahir; }
set
{ tgl_lahir = value; }
}
double
ipk;
public double Ipk
{
get
{ return ipk; }
set
{ ipk = value; }
}
int
nilai_algoritma, nilai_spbd, nilai_pbo;
public int Nilai_pbo
{
get
{ return nilai_algoritma; }
set
{ nilai_pbo = value; }
}
public int Nilai_spbd
{
get
{ return nilai_spbd; }
set
{ nilai_spbd = value; }
}
public int Nilai_algoritma
{
get
{ return nilai_pbo; }
set
{ nilai_algoritma = value; }
}
double
nilai_rata2;
public double Nilai_rata2
{
set {
nilai_rata2 = value; }
}
int
jumlah_nilai ;
public int Jumlah_nilai
{
get
{ return jumlah_nilai; }
set
{ jumlah_nilai = value; }
}
//constructor
public
Mahasiswa()
{
}
//Overloading Constructor
public
Mahasiswa(string nim, string
nama, string jurusan, DateTime
tgl_lahir,
double ipk)
{
this.nim
= nim;
this.nama
= nama;
this.jurusan
= jurusan;
this.tgl_lahir
= tgl_lahir;
this.ipk
= ipk;
}
//Method
public void tampilProfil(Mahasiswa
mhs)
{
Console.WriteLine(mhs.nim);
Console.WriteLine(mhs.nama);
Console.WriteLine(mhs.jurusan);
Console.WriteLine(mhs.tgl_lahir);
Console.WriteLine(mhs.ipk);
}
public void Nilai(Mahasiswa
nilai)
{
Console.WriteLine(nilai.nilai_algoritma);
Console.WriteLine(nilai.nilai_spbd);
Console.WriteLine(nilai.nilai_pbo);
}
public void NilaiRata2(Mahasiswa
NR)
{
Console.WriteLine(NR.nilai_rata2);
}
public void Jumlah_Nilai(Mahasiswa
JN)
{
Console.WriteLine(JN.jumlah_nilai);
}
//Overloading Method
public void tampilProfil()
{
Console.WriteLine(nim);
Console.WriteLine(nama);
Console.WriteLine(jurusan);
Console.WriteLine(tgl_lahir);
Console.WriteLine(ipk);
}
public void Nilai()
{
Console.WriteLine(nilai_algoritma);
Console.WriteLine(nilai_spbd);
Console.WriteLine(nilai_pbo);
}
public void NilaiRata2()
{
Console.WriteLine(nilai_rata2);
}
public void Jumlah_Nilai()
{
Console.WriteLine(jumlah_nilai);
}
}
}
VERSION 2
MAIN CLASS
INPUT
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Praktek_PemVis1
{
class Program
{
static void Main(string[]
args)
{
Mahasiswa
mhs1 = new Mahasiswa();
mhs1.Nim = "09.12.0171";
mhs1.Nama = "Budi";
mhs1.Jurusan = "Sistem Informasi";
mhs1.Tgl_lahir = DateTime.Parse("1990-03-12");
mhs1.Ipk = 3.45;
mhs1.Nilai_algoritma = 76;
mhs1.Nilai_spbd = 80;
mhs1.Nilai_pbo = 67;
mhs1.tampilProfil(mhs1);
mhs1.Nilai(mhs1);
mhs1.Jumlah_nilai =
mhs1.Nilai_algoritma + mhs1.Nilai_spbd + mhs1.Nilai_pbo;
mhs1.Jumlah_Nilai(mhs1);
mhs1.Nilai_rata2 = mhs1.Jumlah_nilai / 3;
mhs1.NilaiRata2(mhs1);
Console.WriteLine("");
Mahasiswa
mhs2 = new Mahasiswa();
mhs2.Nim = "10.11.1538";
mhs2.Nama = "Valian";
mhs2.Jurusan = "Teknik Informatika";
mhs2.Tgl_lahir = DateTime.Parse("1992-10-16");
mhs2.Ipk = 3.54;
mhs2.Nilai_algoritma = 97;
mhs2.Nilai_spbd = 98;
mhs2.Nilai_pbo = 97;
mhs2.tampilProfil(mhs2);
mhs2.Nilai(mhs2);
mhs2.Jumlah_nilai =
mhs2.Nilai_algoritma + mhs2.Nilai_spbd + mhs2.Nilai_pbo;
mhs2.Jumlah_Nilai(mhs2);
mhs2.Nilai_rata2 =
mhs2.Jumlah_nilai / 3;
mhs2.NilaiRata2(mhs2);
Console.WriteLine("");
Mahasiswa
mhs3 = new Mahasiswa("12.12.0018", "Jessica",
"Sistem Informasi", DateTime.Parse("1994-07-09"),
3.67, 98, 97, 95);
mhs3.tampilProfil();
mhs3.Nilai(mhs3);
mhs3.Jumlah_nilai = mhs3.Nilai_algoritma +
mhs3.Nilai_spbd + mhs3.Nilai_pbo;
mhs3.Jumlah_Nilai(mhs3);
mhs3.Nilai_rata2 =
mhs3.Jumlah_nilai / 3;
mhs3.NilaiRata2(mhs3);
Console.WriteLine("");
Console.Read();
}
}
}
CLASS MAHASISWA
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Praktek_PemVis1
{
class Mahasiswa
{
//field
string
nim, nama, jurusan;
public string Jurusan
{
get
{ return jurusan; }
set
{ jurusan = value; }
}
public string Nama
{
get
{ return nama; }
set
{ nama = value; }
}
public string Nim
{
get
{ return nim; }
set
{ nim = value; }
}
DateTime
tgl_lahir;
public DateTime Tgl_lahir
{
get
{ return tgl_lahir; }
set
{ tgl_lahir = value; }
}
double ipk;
public double Ipk
{
get
{ return ipk; }
set
{ ipk = value; }
}
int
nilai_algoritma, nilai_spbd, nilai_pbo;
public int Nilai_pbo
{
get
{ return nilai_algoritma; }
set
{ nilai_pbo = value; }
}
public int Nilai_spbd
{
get
{ return nilai_spbd; }
set
{ nilai_spbd = value; }
}
public int Nilai_algoritma
{
get
{ return nilai_pbo; }
set
{ nilai_algoritma = value; }
}
double
nilai_rata2;
public double Nilai_rata2
{
get
{ return nilai_rata2; }
set
{ nilai_rata2 = value; }
}
int jumlah_nilai;
public int Jumlah_nilai
{
get
{ return jumlah_nilai; }
set
{ jumlah_nilai = value; }
}
//constructor
public
Mahasiswa()
{
}
//tugas
//Overloading
Constructor
public Mahasiswa(string
nim, string nama, string
jurusan, DateTime tgl_lahir,
double ipk, int nilai_algoritma, int
nilai_spbd, int nilai_pbo)
{
this.nim
= nim;
this.nama
= nama;
this.jurusan
= jurusan;
this.tgl_lahir
= tgl_lahir;
this.ipk
= ipk;
this.nilai_algoritma
= nilai_algoritma;
this.nilai_spbd
= nilai_spbd;
this.nilai_pbo
= nilai_pbo;
}
//tugas
//Method
public
void tampilProfil(Mahasiswa
mhs)
{
Console.WriteLine(mhs.nim);
Console.WriteLine(mhs.nama);
Console.WriteLine(mhs.jurusan);
Console.WriteLine(mhs.tgl_lahir);
Console.WriteLine(mhs.ipk);
}
//tugas
public void Nilai(Mahasiswa
nilai)
{
Console.WriteLine(nilai.nilai_algoritma);
Console.WriteLine(nilai.nilai_spbd);
Console.WriteLine(nilai.nilai_pbo);
}
//tugas
public void NilaiRata2(Mahasiswa
NR)
{
Console.WriteLine(NR.nilai_rata2);
}
public void Jumlah_Nilai(Mahasiswa
JN)
{
Console.WriteLine(JN.jumlah_nilai);
}
//Overloading
Method
public void tampilProfil()
{
Console.WriteLine(nim);
Console.WriteLine(nama);
Console.WriteLine(jurusan);
Console.WriteLine(tgl_lahir);
Console.WriteLine(ipk);
}
//tugas
public void Nilai()
{
Console.WriteLine(nilai_algoritma);
Console.WriteLine(nilai_spbd);
Console.WriteLine(nilai_pbo);
}
//tugas
public void NilaiRata2()
{
Console.WriteLine(nilai_rata2);
}
public void Jumlah_Nilai()
{
Console.WriteLine(jumlah_nilai);
}
}
}
No comments:
Post a Comment