Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp5
  8. {
  9. public class Person
  10. {
  11. public string Name;
  12.  
  13. public void Introduce(string to)
  14. {
  15. Console.WriteLine("Hi {0}, I am {1}", to, Name);//untuk print introduce
  16. }
  17. public static Person Parse(string str)//untuk assign string
  18. {
  19. var person = new Person();//untuk assign input menjadi new Person
  20. person.Name = str;
  21.  
  22. return person;//mengembalikan variable
  23. }
  24. }
  25. class Program
  26. {
  27. static void Main(string[] args)
  28. {
  29. var person = Person.Parse("tommy");//untuk memanggil class person
  30. person.Introduce("Arya");//untuk memanggil object introduce
  31. Console.WriteLine("Press enter to close...");//agar tidak force close consolenya
  32. Console.ReadLine();//untuk assign enter agar console bisa close
  33. }
  34. }
  35. }
  36. ------------------------------------------------------------------------------
  37. namespace WindowsFormsApp3
  38. {
  39. class konsumsi
  40. {
  41. public int konsum (int i)
  42. {
  43. return i * 6 * 30;
  44. }
  45. }
  46. }
  47. ------------------------------------
  48. namespace ConsoleApp6
  49. {
  50. class MyArray
  51. {
  52. static void Main(string[] args)
  53. {
  54. int[] n = new int[10];
  55. int i, j;
  56.  
  57. for ( i = 0; i < 10; i++)
  58. {
  59. n[i] = i + 100;
  60. }
  61.  
  62. for(j = 0; j < 10; j++)
  63. {
  64. Console.WriteLine("Elemen[{0}] = {1}", j, n[j]);
  65. }
  66. Console.ReadKey();
  67. }
  68. }
  69. }
  70. -------------------------------------------
  71. namespace week6_1
  72. {
  73. public partial class Form1 : Form
  74. {
  75. public Form1()
  76. {
  77. InitializeComponent();
  78. }
  79. public void CreateGrid()
  80. {
  81. int width = 5;//inisialisasi width
  82. int height = 5;
  83.  
  84. int[,] grid = new int[width, height];//inisialisasi array rectangle
  85. grid[2, 3] = 3;//assign grid
  86.  
  87. string message = null;//inisialisasi message
  88. for (int x = 0; x < width; x++)//looping buat array
  89. {
  90. for (int y = 0; y < height; y++)//looping
  91. {
  92. message += (grid[x, y] + " ");//assign grid[x,y] ke variable message
  93. }
  94. }
  95. MessageBox.Show(message);//untuk ngeprint di message box
  96. }
  97. private void Button2_Click(object sender, EventArgs e)
  98. {
  99. CreateGrid();//memanggil fungsi CreateGrid
  100. }
  101. }
  102. }
  103. -----------------------------------------------
  104. namespace kuli
  105. {
  106. class akomodasi
  107.  
  108. {
  109. public int a(int i)
  110. {
  111. return i * 6;
  112. }
  113. }
  114. }
  115. ----------------------------------------
  116.  
  117. namespace kuli
  118. {
  119. class Hitung
  120. {
  121. public int total(int a, int b, int c, int d, int e,int f, int semester)
  122. {
  123. return semester*(a + b + c + d + e + f );
  124. }
  125.  
  126. }
  127. }-----------------------------------------------------
  128. private void Button1_Click(object sender, EventArgs e)
  129. {
  130. konsumsi a = new konsumsi();//untuk membuat object baru untuk dilempar ke class
  131. transportasi b = new transportasi();
  132. akomodasi c = new akomodasi();
  133. komunikasi d = new komunikasi();
  134. kuliah asde = new kuliah();
  135. buku f = new buku();
  136.  
  137. int m = a.b(int.Parse(textBox7.Text));//untuk mengambil inputan dari textbox
  138. int n = b.a(int.Parse(textBox5.Text));
  139. int o = c.a(int.Parse(textBox6.Text));
  140. int p = d.a(int.Parse(textBox4.Text));
  141. int q = asde.a(int.Parse(textBox3.Text));
  142. int r = f.a(int.Parse(textBox8.Text));
  143.  
  144. int semester = int.Parse(textBox2.Text);
  145. int total = (m + n + o + p + q + r) * semester;//menjumlah
  146.  
  147.  
  148. string ff = total.ToString();//merubah total dari int ke string
  149.  
  150. string name = textBox1.Text;//assign name dari textbox1
  151.  
  152.  
  153. MessageBox.Show("Hai, " + name + "! Biaya yang kamu keluarkan untuk kuliah selama " + textBox2.Text + " semester adalah sebesar Rp." + ff);//untuk print messagebox
  154. }
  155. ------------------------------------------------------------
  156.  
  157. private void Button1_Click(object sender, EventArgs e)
  158. {
  159. int[] arr = { 0, 0, 0, 0, 0, 0, 3, 3, 0, 2, 7 }; // inisialisasi array
  160. int batas = arr.GetUpperBound(0); // buat menentukan batas akhir
  161. string mes = null;// menyimpan mes untuk 5 angka terakhir
  162. for(int i = 0; i <= batas; i++)//looping i kurang dari batas
  163. {
  164. if (i > 5)//mencegah 6 nol di awal agar tidak tercetak
  165. {
  166. MessageBox.Show(arr[i].ToString());
  167. mes += arr[i].ToString();//mengcompound arr[i] di variable mes
  168. }
  169.  
  170. }
  171. MessageBox.Show(mes);//print mes
  172. }
  173. }
  174. }---------------------------------------------------------
  175.  
  176. namespace week6_3
  177. {
  178. public partial class Form1 : Form
  179. {
  180. public Form1()
  181. {
  182. InitializeComponent();
  183. }
  184.  
  185. private void Button1_Click(object sender, EventArgs e)
  186. {
  187. string[][] arr = new string[3][];//declare array yang berisi 2 elemen
  188.  
  189. //inisialisasi elemen
  190. arr[0] = new string[4] { "00000029747", "00000032921", "00000033278", "00000032899" };
  191. arr[1] = new string[4] { "00000017859", "00000018233", "00000018055", "00000018291" };
  192. arr[2] = new string[4] { "00000027128", "00000033027", "00000032493", "00000032792" };
  193.  
  194. for(int i = 0; i < arr.Length; i++)
  195. {
  196. MessageBox.Show("element: "+i);
  197.  
  198. for(int j = 0; j < arr[i].Length; j++)
  199. {
  200. MessageBox.Show("value: " + arr[i][j]);
  201. }
  202. }
  203. string nim = "00000033027";
  204.  
  205. for(int i = 0; i < arr.Length; i++)
  206. {
  207. int hasil = Array.IndexOf(arr[i], nim);
  208.  
  209. if(hasil > -1)
  210. {
  211. MessageBox.Show("Posisi duduk baris ke " + (i + 1).ToString() + " kolam ke " + (hasil + 1).ToString());
  212. }
  213. }
  214.  
  215. }
  216. }
  217. ------------------------------------------------------------------------
  218. using System;
  219. using System.Collections.Generic;
  220. using System.ComponentModel;
  221. using System.Data;
  222. using System.Drawing;
  223. using System.Linq;
  224. using System.Text;
  225. using System.Threading.Tasks;
  226. using System.Windows.Forms;
  227. using week6_4_2;//make week6_4_2 can accessed from here
  228.  
  229.  
  230. namespace week6_4
  231. {
  232.  
  233.  
  234. public partial class Form1 : Form
  235. {
  236. static int bb = 1;//intialize variable and make it global
  237.  
  238. public Form1()
  239. {
  240. InitializeComponent();
  241. }
  242.  
  243. private void Button1_Click(object sender, EventArgs e)
  244. {
  245. int kali = int.Parse(textBox1.Text);//convert string input from textbox1 to integer and store it in kali variable
  246. richTextBox1.Text = "index\tvalue";//print a string to richtextbox1
  247. float[] arr = new float[kali];//intialize array with float datatype, because its needed for calculate average
  248. float sum = 0;//intialize sum for later
  249. float avg = 0;//intialize avg for later
  250. for (bb = 0; bb < kali; bb++)//intialize a looping for pop up a box multiple times , sesuai dengan inputan user di variable kali
  251. {
  252. int bc = bb + 1;// intialize variable and +1 for printing in label1 and richtextbox
  253. using (Form2 f2 = new Form2())//make form2 can accessed from form1
  254. {
  255. f2.label1.Text = "masukkan nilai elemen array " + bc + " ?";//print a string to label1
  256. f2.ShowDialog();//popup the showdialog box
  257. arr[bb] = f2.aa;//store a input from user to array arr[bb]
  258. richTextBox1.Text += "\n" + bc.ToString() +"\t"+ arr[bb].ToString();
  259. sum += arr[bb];//count total array value and sum it
  260. avg = sum / kali;//find average by dividing sum with kali
  261.  
  262.  
  263. }
  264.  
  265.  
  266.  
  267. }
  268.  
  269. textBox3.Text = sum.ToString();//output sum to textbox3
  270. textBox2.Text = avg.ToString();//output sum to textbox2
  271. richTextBox2.Text = "Element\tvalue\tHistogram";//print a value to richtextbox2
  272. int ii;//intialize ii
  273. int i;//intialize i
  274.  
  275. for (i = 0; i < kali; i++)//intialize loop for printing in textbox2
  276. {
  277. string a = "";//intialize string a
  278. for (int j = 0; j < arr[i]; j++)//inisialisasi looping untuk print * sesuai value inputan user
  279. {
  280. a += "*";//store * ke variable a sesuai value inputan user
  281.  
  282. }
  283. ii = i + 1;// agar i mulai dari 1
  284. richTextBox2.Text += "\n" + ii.ToString() + "\t" + arr[i] + "\t" + a;//print berkali kali ke richtextbox2
  285.  
  286. }
  287.  
  288. }
  289.  
  290. private void Label1_Click(object sender, EventArgs e)
  291. {
  292.  
  293. }
  294.  
  295. private void TextBox2_TextChanged(object sender, EventArgs e)
  296. {
  297.  
  298. }
  299.  
  300. private void TextBox3_TextChanged(object sender, EventArgs e)
  301. {
  302.  
  303. }
  304. }
  305. }
  306. ---------------------------------------------------------------------------
  307. namespace week6_4_2
  308. {
  309. public partial class Form2 : Form
  310. {
  311. public int aa;
  312. public Form2()
  313. {
  314. InitializeComponent();
  315. }
  316.  
  317.  
  318. public void Label1_Click(object sender, EventArgs e)
  319. {
  320. }
  321.  
  322. private void Button1_Click(object sender, EventArgs e)
  323. {
  324. aa = int.Parse(textBox1.Text);//store input from textbox1 to a variable
  325. Close();//close inputbox after button ok pressed
  326.  
  327. }
  328. }
  329. }
  330. ---------------------------------------------------------------------------------
  331. using System;
  332. using System.Collections.Generic;
  333. using System.Linq;
  334. using System.Text;
  335. using System.Threading.Tasks;
  336.  
  337. namespace prak7_satu
  338. {
  339.  
  340.  
  341.  
  342.  
  343. class Program
  344. {
  345. class Animal // base class
  346. {
  347. public string name;
  348. public int age;
  349. public float happiness;
  350.  
  351. public void PrintBase()
  352. {
  353. Console.WriteLine("Name: " + name);
  354. Console.WriteLine("Age: " + age);
  355. Console.WriteLine("Happiness: " + happiness);
  356. }
  357. }
  358. class Dog : Animal // derived class
  359. {
  360. public int spotCount;
  361. public void Bark()
  362. {
  363. Console.WriteLine("WUF!");
  364. base.happiness += 0.1f;
  365. }
  366. }
  367. class Cat : Animal // derived class
  368. {
  369. public float cuteness;
  370. public void Meow()
  371. {
  372. Console.WriteLine("MEOW!");
  373. }
  374. }
  375. static void Main(string[] args)
  376. {
  377. Dog spotty = new Dog();//new object
  378. spotty.name = "Spotty";
  379. spotty.age = 4;
  380. spotty.happiness = 0.8f;
  381. spotty.spotCount = 25;
  382. spotty.PrintBase();
  383. spotty.Bark();
  384. Console.WriteLine("New Happiness: " + spotty.happiness);
  385.  
  386. Console.WriteLine();
  387.  
  388. Cat heisenberg = new Cat();//new object
  389. heisenberg.name = "Heisenberg";
  390. heisenberg.age = 13;
  391. heisenberg.happiness = 0.3f;
  392. heisenberg.cuteness = 0.4f;
  393. heisenberg.PrintBase();
  394. heisenberg.Meow();
  395.  
  396. Console.ReadKey();
  397. }
  398. }
  399. }
  400. -------------------------------------------------------------------------
  401. using System;
  402. using System.Collections.Generic;
  403. using System.ComponentModel;
  404. using System.Data;
  405. using System.Drawing;
  406. using System.Linq;
  407. using System.Text;
  408. using System.Threading.Tasks;
  409. using System.Windows.Forms;
  410.  
  411. namespace prak7_dua
  412. {
  413. public partial class Form1 : Form
  414. {
  415. class Cmath//base class
  416. {
  417. public int Tambah(int a, int b)
  418. {
  419. return a + b;
  420. }
  421. public int Kurang(int a, int b)
  422. {
  423. return a - b;
  424. }
  425. }
  426. class Cmath2 : Cmath // derived class
  427. {
  428. public int kali(int a, int b)
  429. {
  430. return a * b;
  431. }
  432. public int bagi(int a, int b)
  433. {
  434. return a / b;
  435. }
  436. }
  437. public Form1()
  438. {
  439. InitializeComponent();
  440. }
  441.  
  442. private void Button1_Click(object sender, EventArgs e)
  443. {
  444. Cmath vCmath = new Cmath();
  445. int vhasil;
  446. vhasil = vCmath.Tambah(1, 2);
  447. MessageBox.Show(vhasil.ToString());
  448. }
  449.  
  450. private void Button2_Click(object sender, EventArgs e)
  451. {
  452. Cmath2 vCmath2 = new Cmath2();
  453. int vhasil;
  454. vhasil = vCmath2.kali(1, 2);
  455. MessageBox.Show(vhasil.ToString());
  456. }
  457. }
  458. }
  459. -----------------------------------------------------------------------------
  460. using System;
  461. using System.Windows.Forms;
  462.  
  463. namespace WindowsFormsApplication2
  464. {
  465. public partial class Form1 : Form
  466. {
  467. public Form1()
  468. {
  469. InitializeComponent();
  470. }
  471.  
  472. private void buttonOk_Click(object sender, EventArgs e)
  473. {
  474. buttonOk.Enabled = false; // disable button OK
  475. buttonClear.Enabled = true; // enable button OK
  476. Pekerja pekerja; // inisialisasi variabel pekerja
  477.  
  478. // jika upahtambahan kosong, maka pekerja
  479. if (string.IsNullOrEmpty(textUpahTambahan.Text))
  480. {
  481. // buat instance pekerja
  482. pekerja = new Pekerja();
  483. // set property
  484. pekerja.Nama = textNama.Text;
  485. pekerja.Alamat = textAlamat.Text;
  486. pekerja.TglLahir = dateTimePickerTglLahir.Value.Date;
  487. pekerja.JamKerja = Convert.ToDouble(textJamKerja.Text);
  488. pekerja.UpahPerJam = Convert.ToDouble(textUpahPerJam.Text);
  489. // isi textupahditerima dengan upahjamkerja
  490. textUpahDiterima.Text = formatNumber(pekerja.UpahJamKerja);
  491. } else // jika tidak, maka pekerjatetap
  492. {
  493. // buat instance pekerjatetap
  494. pekerja = new PekerjaTetap();
  495. // set property
  496. pekerja.Nama = textNama.Text;
  497. pekerja.Alamat = textAlamat.Text;
  498. pekerja.TglLahir = dateTimePickerTglLahir.Value.Date;
  499. pekerja.JamKerja = Convert.ToDouble(textJamKerja.Text);
  500. pekerja.UpahPerJam = Convert.ToDouble(textUpahPerJam.Text);
  501. (pekerja as PekerjaTetap).UpahTambahan = Convert.ToDouble(textUpahTambahan.Text);
  502. // isi textupahditerima dengan upahditerima
  503. textUpahDiterima.Text = formatNumber((pekerja as PekerjaTetap).UpahDiterima);
  504. }
  505.  
  506. // isi textumur dengan umur pekerja
  507. textUmur.Text = pekerja.Umur.ToString();
  508. }
  509.  
  510. private void buttonClear_Click(object sender, EventArgs e)
  511. {
  512. // disable button clear
  513. buttonClear.Enabled = false;
  514. // enable button OK
  515. buttonOk.Enabled = true;
  516.  
  517. // kosongkan semua input
  518. textNama.Clear();
  519. textNamaLengkap.Clear();
  520. textNamaPanggilan.Clear();
  521. textAlamat.Clear();
  522. dateTimePickerTglLahir.ResetText();
  523. textJamKerja.Clear();
  524. textUpahPerJam.Clear();
  525. textUpahTambahan.Clear();
  526. textUpahDiterima.Clear();
  527. textUmur.Clear();
  528. }
  529.  
  530. private string formatNumber(double number)
  531. {
  532. // format angka menjadi currency
  533. return number.ToString("Rp#,##0");
  534. }
  535. }
  536. }
  537. --------------------------------------------------------------------------
  538. using System;
  539.  
  540. namespace WindowsFormsApplication2
  541. {
  542. class Karyawan
  543. {
  544. public string Nama { get; set; } // property string nama
  545. public string Alamat { get; set; } // property string alamat
  546. public DateTime TglLahir { get; set; } // property datetime tgllahir
  547.  
  548. // property umur
  549. public int Umur
  550. {
  551. get
  552. {
  553. // Hari ini
  554. DateTime today = DateTime.Today;
  555. // Hitung umur
  556. int age = today.Year - TglLahir.Year;
  557. // Jika tanggal hari ini belum melewati ulang tahunnya, maka umur - 1
  558. if (today.DayOfYear < TglLahir.DayOfYear)
  559. age = age - 1;
  560.  
  561. return age;
  562. }
  563. }
  564. }
  565. }
  566. -----------------------------------------------------
  567. namespace WindowsFormsApplication2
  568. {
  569. class Pekerja : Karyawan
  570. {
  571. public double JamKerja { get; set; } // property double jamkerja
  572. public double UpahPerJam { get; set; } // property double upahperjam
  573.  
  574. public double UpahJamKerja // property double upahjamkerja
  575. {
  576. get
  577. {
  578. // upahjamkerja = jamkerja * upahperjam
  579. return JamKerja * UpahPerJam;
  580. }
  581. }
  582. }
  583. }
  584. ---------------------------------------------------------
  585. namespace WindowsFormsApplication2
  586. {
  587. class PekerjaTetap : Pekerja
  588. {
  589. public double UpahTambahan { get; set; } // property double upahtambahan
  590.  
  591. public double UpahDiterima // property double upahditerima
  592. {
  593. get
  594. {
  595. // upahditerima = upahjamkerja + upahtambahan
  596. return UpahJamKerja + UpahTambahan;
  597. }
  598. }
  599. }
  600. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement