Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp5
- {
- public class Person
- {
- public string Name;
- public void Introduce(string to)
- {
- Console.WriteLine("Hi {0}, I am {1}", to, Name);//untuk print introduce
- }
- public static Person Parse(string str)//untuk assign string
- {
- var person = new Person();//untuk assign input menjadi new Person
- person.Name = str;
- return person;//mengembalikan variable
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var person = Person.Parse("tommy");//untuk memanggil class person
- person.Introduce("Arya");//untuk memanggil object introduce
- Console.WriteLine("Press enter to close...");//agar tidak force close consolenya
- Console.ReadLine();//untuk assign enter agar console bisa close
- }
- }
- }
- ------------------------------------------------------------------------------
- namespace WindowsFormsApp3
- {
- class konsumsi
- {
- public int konsum (int i)
- {
- return i * 6 * 30;
- }
- }
- }
- ------------------------------------
- namespace ConsoleApp6
- {
- class MyArray
- {
- static void Main(string[] args)
- {
- int[] n = new int[10];
- int i, j;
- for ( i = 0; i < 10; i++)
- {
- n[i] = i + 100;
- }
- for(j = 0; j < 10; j++)
- {
- Console.WriteLine("Elemen[{0}] = {1}", j, n[j]);
- }
- Console.ReadKey();
- }
- }
- }
- -------------------------------------------
- namespace week6_1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public void CreateGrid()
- {
- int width = 5;//inisialisasi width
- int height = 5;
- int[,] grid = new int[width, height];//inisialisasi array rectangle
- grid[2, 3] = 3;//assign grid
- string message = null;//inisialisasi message
- for (int x = 0; x < width; x++)//looping buat array
- {
- for (int y = 0; y < height; y++)//looping
- {
- message += (grid[x, y] + " ");//assign grid[x,y] ke variable message
- }
- }
- MessageBox.Show(message);//untuk ngeprint di message box
- }
- private void Button2_Click(object sender, EventArgs e)
- {
- CreateGrid();//memanggil fungsi CreateGrid
- }
- }
- }
- -----------------------------------------------
- namespace kuli
- {
- class akomodasi
- {
- public int a(int i)
- {
- return i * 6;
- }
- }
- }
- ----------------------------------------
- namespace kuli
- {
- class Hitung
- {
- public int total(int a, int b, int c, int d, int e,int f, int semester)
- {
- return semester*(a + b + c + d + e + f );
- }
- }
- }-----------------------------------------------------
- private void Button1_Click(object sender, EventArgs e)
- {
- konsumsi a = new konsumsi();//untuk membuat object baru untuk dilempar ke class
- transportasi b = new transportasi();
- akomodasi c = new akomodasi();
- komunikasi d = new komunikasi();
- kuliah asde = new kuliah();
- buku f = new buku();
- int m = a.b(int.Parse(textBox7.Text));//untuk mengambil inputan dari textbox
- int n = b.a(int.Parse(textBox5.Text));
- int o = c.a(int.Parse(textBox6.Text));
- int p = d.a(int.Parse(textBox4.Text));
- int q = asde.a(int.Parse(textBox3.Text));
- int r = f.a(int.Parse(textBox8.Text));
- int semester = int.Parse(textBox2.Text);
- int total = (m + n + o + p + q + r) * semester;//menjumlah
- string ff = total.ToString();//merubah total dari int ke string
- string name = textBox1.Text;//assign name dari textbox1
- MessageBox.Show("Hai, " + name + "! Biaya yang kamu keluarkan untuk kuliah selama " + textBox2.Text + " semester adalah sebesar Rp." + ff);//untuk print messagebox
- }
- ------------------------------------------------------------
- private void Button1_Click(object sender, EventArgs e)
- {
- int[] arr = { 0, 0, 0, 0, 0, 0, 3, 3, 0, 2, 7 }; // inisialisasi array
- int batas = arr.GetUpperBound(0); // buat menentukan batas akhir
- string mes = null;// menyimpan mes untuk 5 angka terakhir
- for(int i = 0; i <= batas; i++)//looping i kurang dari batas
- {
- if (i > 5)//mencegah 6 nol di awal agar tidak tercetak
- {
- MessageBox.Show(arr[i].ToString());
- mes += arr[i].ToString();//mengcompound arr[i] di variable mes
- }
- }
- MessageBox.Show(mes);//print mes
- }
- }
- }---------------------------------------------------------
- namespace week6_3
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- string[][] arr = new string[3][];//declare array yang berisi 2 elemen
- //inisialisasi elemen
- arr[0] = new string[4] { "00000029747", "00000032921", "00000033278", "00000032899" };
- arr[1] = new string[4] { "00000017859", "00000018233", "00000018055", "00000018291" };
- arr[2] = new string[4] { "00000027128", "00000033027", "00000032493", "00000032792" };
- for(int i = 0; i < arr.Length; i++)
- {
- MessageBox.Show("element: "+i);
- for(int j = 0; j < arr[i].Length; j++)
- {
- MessageBox.Show("value: " + arr[i][j]);
- }
- }
- string nim = "00000033027";
- for(int i = 0; i < arr.Length; i++)
- {
- int hasil = Array.IndexOf(arr[i], nim);
- if(hasil > -1)
- {
- MessageBox.Show("Posisi duduk baris ke " + (i + 1).ToString() + " kolam ke " + (hasil + 1).ToString());
- }
- }
- }
- }
- ------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using week6_4_2;//make week6_4_2 can accessed from here
- namespace week6_4
- {
- public partial class Form1 : Form
- {
- static int bb = 1;//intialize variable and make it global
- public Form1()
- {
- InitializeComponent();
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- int kali = int.Parse(textBox1.Text);//convert string input from textbox1 to integer and store it in kali variable
- richTextBox1.Text = "index\tvalue";//print a string to richtextbox1
- float[] arr = new float[kali];//intialize array with float datatype, because its needed for calculate average
- float sum = 0;//intialize sum for later
- float avg = 0;//intialize avg for later
- for (bb = 0; bb < kali; bb++)//intialize a looping for pop up a box multiple times , sesuai dengan inputan user di variable kali
- {
- int bc = bb + 1;// intialize variable and +1 for printing in label1 and richtextbox
- using (Form2 f2 = new Form2())//make form2 can accessed from form1
- {
- f2.label1.Text = "masukkan nilai elemen array " + bc + " ?";//print a string to label1
- f2.ShowDialog();//popup the showdialog box
- arr[bb] = f2.aa;//store a input from user to array arr[bb]
- richTextBox1.Text += "\n" + bc.ToString() +"\t"+ arr[bb].ToString();
- sum += arr[bb];//count total array value and sum it
- avg = sum / kali;//find average by dividing sum with kali
- }
- }
- textBox3.Text = sum.ToString();//output sum to textbox3
- textBox2.Text = avg.ToString();//output sum to textbox2
- richTextBox2.Text = "Element\tvalue\tHistogram";//print a value to richtextbox2
- int ii;//intialize ii
- int i;//intialize i
- for (i = 0; i < kali; i++)//intialize loop for printing in textbox2
- {
- string a = "";//intialize string a
- for (int j = 0; j < arr[i]; j++)//inisialisasi looping untuk print * sesuai value inputan user
- {
- a += "*";//store * ke variable a sesuai value inputan user
- }
- ii = i + 1;// agar i mulai dari 1
- richTextBox2.Text += "\n" + ii.ToString() + "\t" + arr[i] + "\t" + a;//print berkali kali ke richtextbox2
- }
- }
- private void Label1_Click(object sender, EventArgs e)
- {
- }
- private void TextBox2_TextChanged(object sender, EventArgs e)
- {
- }
- private void TextBox3_TextChanged(object sender, EventArgs e)
- {
- }
- }
- }
- ---------------------------------------------------------------------------
- namespace week6_4_2
- {
- public partial class Form2 : Form
- {
- public int aa;
- public Form2()
- {
- InitializeComponent();
- }
- public void Label1_Click(object sender, EventArgs e)
- {
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- aa = int.Parse(textBox1.Text);//store input from textbox1 to a variable
- Close();//close inputbox after button ok pressed
- }
- }
- }
- ---------------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace prak7_satu
- {
- class Program
- {
- class Animal // base class
- {
- public string name;
- public int age;
- public float happiness;
- public void PrintBase()
- {
- Console.WriteLine("Name: " + name);
- Console.WriteLine("Age: " + age);
- Console.WriteLine("Happiness: " + happiness);
- }
- }
- class Dog : Animal // derived class
- {
- public int spotCount;
- public void Bark()
- {
- Console.WriteLine("WUF!");
- base.happiness += 0.1f;
- }
- }
- class Cat : Animal // derived class
- {
- public float cuteness;
- public void Meow()
- {
- Console.WriteLine("MEOW!");
- }
- }
- static void Main(string[] args)
- {
- Dog spotty = new Dog();//new object
- spotty.name = "Spotty";
- spotty.age = 4;
- spotty.happiness = 0.8f;
- spotty.spotCount = 25;
- spotty.PrintBase();
- spotty.Bark();
- Console.WriteLine("New Happiness: " + spotty.happiness);
- Console.WriteLine();
- Cat heisenberg = new Cat();//new object
- heisenberg.name = "Heisenberg";
- heisenberg.age = 13;
- heisenberg.happiness = 0.3f;
- heisenberg.cuteness = 0.4f;
- heisenberg.PrintBase();
- heisenberg.Meow();
- Console.ReadKey();
- }
- }
- }
- -------------------------------------------------------------------------
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace prak7_dua
- {
- public partial class Form1 : Form
- {
- class Cmath//base class
- {
- public int Tambah(int a, int b)
- {
- return a + b;
- }
- public int Kurang(int a, int b)
- {
- return a - b;
- }
- }
- class Cmath2 : Cmath // derived class
- {
- public int kali(int a, int b)
- {
- return a * b;
- }
- public int bagi(int a, int b)
- {
- return a / b;
- }
- }
- public Form1()
- {
- InitializeComponent();
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- Cmath vCmath = new Cmath();
- int vhasil;
- vhasil = vCmath.Tambah(1, 2);
- MessageBox.Show(vhasil.ToString());
- }
- private void Button2_Click(object sender, EventArgs e)
- {
- Cmath2 vCmath2 = new Cmath2();
- int vhasil;
- vhasil = vCmath2.kali(1, 2);
- MessageBox.Show(vhasil.ToString());
- }
- }
- }
- -----------------------------------------------------------------------------
- using System;
- using System.Windows.Forms;
- namespace WindowsFormsApplication2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void buttonOk_Click(object sender, EventArgs e)
- {
- buttonOk.Enabled = false; // disable button OK
- buttonClear.Enabled = true; // enable button OK
- Pekerja pekerja; // inisialisasi variabel pekerja
- // jika upahtambahan kosong, maka pekerja
- if (string.IsNullOrEmpty(textUpahTambahan.Text))
- {
- // buat instance pekerja
- pekerja = new Pekerja();
- // set property
- pekerja.Nama = textNama.Text;
- pekerja.Alamat = textAlamat.Text;
- pekerja.TglLahir = dateTimePickerTglLahir.Value.Date;
- pekerja.JamKerja = Convert.ToDouble(textJamKerja.Text);
- pekerja.UpahPerJam = Convert.ToDouble(textUpahPerJam.Text);
- // isi textupahditerima dengan upahjamkerja
- textUpahDiterima.Text = formatNumber(pekerja.UpahJamKerja);
- } else // jika tidak, maka pekerjatetap
- {
- // buat instance pekerjatetap
- pekerja = new PekerjaTetap();
- // set property
- pekerja.Nama = textNama.Text;
- pekerja.Alamat = textAlamat.Text;
- pekerja.TglLahir = dateTimePickerTglLahir.Value.Date;
- pekerja.JamKerja = Convert.ToDouble(textJamKerja.Text);
- pekerja.UpahPerJam = Convert.ToDouble(textUpahPerJam.Text);
- (pekerja as PekerjaTetap).UpahTambahan = Convert.ToDouble(textUpahTambahan.Text);
- // isi textupahditerima dengan upahditerima
- textUpahDiterima.Text = formatNumber((pekerja as PekerjaTetap).UpahDiterima);
- }
- // isi textumur dengan umur pekerja
- textUmur.Text = pekerja.Umur.ToString();
- }
- private void buttonClear_Click(object sender, EventArgs e)
- {
- // disable button clear
- buttonClear.Enabled = false;
- // enable button OK
- buttonOk.Enabled = true;
- // kosongkan semua input
- textNama.Clear();
- textNamaLengkap.Clear();
- textNamaPanggilan.Clear();
- textAlamat.Clear();
- dateTimePickerTglLahir.ResetText();
- textJamKerja.Clear();
- textUpahPerJam.Clear();
- textUpahTambahan.Clear();
- textUpahDiterima.Clear();
- textUmur.Clear();
- }
- private string formatNumber(double number)
- {
- // format angka menjadi currency
- return number.ToString("Rp#,##0");
- }
- }
- }
- --------------------------------------------------------------------------
- using System;
- namespace WindowsFormsApplication2
- {
- class Karyawan
- {
- public string Nama { get; set; } // property string nama
- public string Alamat { get; set; } // property string alamat
- public DateTime TglLahir { get; set; } // property datetime tgllahir
- // property umur
- public int Umur
- {
- get
- {
- // Hari ini
- DateTime today = DateTime.Today;
- // Hitung umur
- int age = today.Year - TglLahir.Year;
- // Jika tanggal hari ini belum melewati ulang tahunnya, maka umur - 1
- if (today.DayOfYear < TglLahir.DayOfYear)
- age = age - 1;
- return age;
- }
- }
- }
- }
- -----------------------------------------------------
- namespace WindowsFormsApplication2
- {
- class Pekerja : Karyawan
- {
- public double JamKerja { get; set; } // property double jamkerja
- public double UpahPerJam { get; set; } // property double upahperjam
- public double UpahJamKerja // property double upahjamkerja
- {
- get
- {
- // upahjamkerja = jamkerja * upahperjam
- return JamKerja * UpahPerJam;
- }
- }
- }
- }
- ---------------------------------------------------------
- namespace WindowsFormsApplication2
- {
- class PekerjaTetap : Pekerja
- {
- public double UpahTambahan { get; set; } // property double upahtambahan
- public double UpahDiterima // property double upahditerima
- {
- get
- {
- // upahditerima = upahjamkerja + upahtambahan
- return UpahJamKerja + UpahTambahan;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement