Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, June 17, 2016

Main C# Collections - Part 1

Di C# ada beberapa collection, antara lain :
  • List
  • ArrayList
  • HashTable
  • Stack
  • Queque
  • Dictionary
  • NameValueCollection
Soo...Pertama-tama kita bahas yaaang List... Jadi silahkan buat project baru di visual studio... :D... Daaan..berikut operasi2 dasarnya...
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            /*** Nampung list untuk integer ***/
            List<int> intList = new List<int>();
            
            /**** Nambain Element *****/
            intList.Add(3);
            intList.Add(5);
            intList.Add(7);
            intList.Add(9);
            intList.Add(2);
            intList.Add(6);


            /*** Ukuran/jumlah element dalam list ***/
            Console.WriteLine("Jumlah Element : "+intList.Count);
            Console.WriteLine("Isi intList : ");
            foreach (int a in intList) Console.WriteLine(a);


            /*** insert item baru ***/
            intList.Insert(2,99);
            intList.Insert(5, 99);
            Console.WriteLine("\nIsi intList setelah insert element : ");
            foreach (int a in intList) Console.WriteLine(a);
            
            
            /*** search element dalam list ****/
            Console.WriteLine(" search 7 : "+intList.Contains(7));
            Console.WriteLine(" index 7 : "+intList.IndexOf(7));
            Console.WriteLine(" search 77 : " + intList.Contains(77));
            Console.WriteLine(" index 77 : " + intList.IndexOf(77));


            /*** sort item dalam list ***/
            intList.Sort();
            Console.WriteLine("\nIsi intList setelah di-sort : ");
            foreach (int a in intList) Console.WriteLine(a);

            /*** remove item ***/
            Console.WriteLine(" remove 99 : " + intList.Remove(99));
            Console.WriteLine(" remove 999 : " + intList.Remove(999));


            /*** clear total isi list ***/
            intList.Clear();

            
            Console.ReadLine();
        }
    }
}
Hasil :

Terus untuk yg rada advance dikit.... Seperti ini :
Pertama-tama bikin class baru :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  public class akatsuki
    {
        public string name { get; set; }
        public int rating { get; set; }
        public string jutsu { get; set; }


        public override string ToString()
        {
            return "Name : "+ name+ ", Rating : "+ rating+ ", Jutsu "+jutsu;
        }
    }
Berikut operasi-operasinya....
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            /*** Deklarasi ***/
            List<akatsuki> myList = new List<akatsuki>();
            
            /*** Nambain element baru ***/
            myList.Add(new akatsuki() { name = "Itachi", rating=91, jutsu="Izanami"});
            myList.Add(new akatsuki() { name = "Hyosoka", rating = 93, jutsu = "Bungeejigum" });
            myList.Add(new akatsuki() { name = "Nagato", rating = 91, jutsu = "Sira Tensei" });
            myList.Add(new akatsuki() { name = "Poipo", rating = 91, jutsu = "xxxxx" });

            /* Nampilin element */
            foreach (akatsuki a in myList) Console.WriteLine("Nama : "+a.name + " Rating : "+a.rating+ " Jutsu : "+a.jutsu);


            /* Search item */
            Console.WriteLine("\nElement dengan nama Itachi : "+myList.Find(x=>x.name.Contains("Itachi")));
            Console.WriteLine("Element dengan rating 91 :" + myList.Find(x => x.rating == 91));
            Console.WriteLine("Element dengan rating 90:" + myList.Find(x => x.rating == 90));

            /*Exist method*/
            Console.WriteLine("\nApa ada yg ratingnya 93 ? : "+myList.Exists(x=>x.rating == 93));
            Console.WriteLine("Apa ada yg ratingnya 95 ? : " + myList.Exists(x => x.rating == 95));


            
            Console.ReadLine();
        }
    }



    public class akatsuki
    {
        public string name { get; set; }
        public int rating { get; set; }
        public string jutsu { get; set; }


        public override string ToString()
        {
            return "Name : "+ name+ ", Rating : "+ rating+ ", Jutsu "+jutsu;
        }
    }


}
Hasil :



Thursday, June 16, 2016

Deploying Website Using IIS in Windows 7

Naaah... disini kita udah punya website yang sudah siap dideploy, proses pembuatan websitenya bisa dilihat disini.. Terus skrg kita bakalan ngedeploy websitenya pake IIS. So silahkan buka IIS-nya terus Add new website :
Terus masukin properti yang dibutuhkan...
Klu udah selese... tar kita dapat site baru dipanel kiri seperti terlihat sebagai berikut :
Naaah....abis itu kita ke visual studio klik menu "Build" terus pilih "Publish WebSederhana"... WebSederhana itu nama projectnya....
Daaan.... Silahkan ikuti langkah2 dibawah sesuai gambar dibawah...





Naaah...setelah selese.... balik ke IIS, klik kanan simple website terus pilih yg start kyk gini :
Atau klu mau lngsung dari panel sebelah kanan juga ada... :D
Terus browse websitenya... boleh dari panel kyk yg ditunjukin panah ijo di atas, atau dari klik kanan terus pilih browse :
Hasilnya....
Oooops... ada yang lupaa... :D
Mmmm.... buka host file di C:\Windows\System32\drivers\etc
Terus tambain :
Terus folder simpleWeb-nya share :




Terus IIS untuk simple web direstart atau disetop terus di start lagi..
Klu di Runnn...
Ooopsss....masih error... :v
Mmmmm..... klihatannya kita harus ganti targetFrameworknya... hohoho... Pilih applicationPool..
Terus klik kanan yg simpleWeb...Terus pilih Advance setting...
Terus ganti target frameworknya jadi 4.0...
Abis itu restart lagi IIS simple site...Daaan... Hasilnya....Taaadaaaaaa.... :D

Cukup sekiaaan... :)

Wednesday, June 15, 2016

splitOdd10 - Recursive - Java & C#

Given an array of ints, is it possible to divide the ints into two groups, so that the sum of one group is a multiple of 10, and the sum of the other group is odd. Every int must be in one group or the other. Write a recursive helper method that takes whatever arguments you like, and make the initial call to your recursive helper from splitOdd10(). (No loops needed.)

splitOdd10([5, 5, 5]) → true
splitOdd10([5, 5, 6]) → false
splitOdd10([5, 5, 6, 1]) → true

Java :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public boolean splitOdd10(int[] nums) {
   return helper(0, nums, 0);
}

public boolean helper(int index, int[] nums, int result)
  { 
    if(index >= nums.length)
      {
        if ((result % 10) % 2 == 1) return true;
        else return false;
      }

    return helper(index + 1, nums, result + nums[index]);
  }

Test Result:

C#:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public static Boolean splitOdd10(int[] nums)
        {
            return helper(0, nums, 0);
        }

public static Boolean helper(int index, int[] nums, int result)
        { 
            if(index >= nums.Length)
            {
                if ((result % 10) % 2 == 1) return true;
                else return false;
            }

            return helper(index + 1, nums, result + nums[index]);
        }

Test :

stringClean - Recursive - Java & C#

Given a string, return recursively a "cleaned" string where adjacent chars that are the same have been reduced to a single char. So "yyzzza" yields "yza".

stringClean("yyzzza") → "yza"
stringClean("abbbcdd") → "abcd"
stringClean("Hello") → "Helo"

Java :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public String stringClean(String str) {
  //Stop Condition
  if(str.length()<2) return str;
  
  //Main recursive process
  int index = 0;
  for(index=0; index+1<str.length(); index++)
  {
    if(str.charAt(index) != str.charAt(index+1)) break;
  }
  return str.charAt(index)+stringClean(str.substring(index+1));
}

Test Result:

C#:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
 public static string stringClean(string str)
        { 
            //kondisi setop
            if (str.Length < 2) return str;

            //main rekursif
            int index = 0;
            for (index=0; index+1 < str.Length; index++)
            {
                if (str[index] != str[index + 1]) break;
            }

            return str[index] + stringClean(str.Substring(index+1));

        }

Test Result :

allStar - Recursive - Java & C#

Given a string, compute recursively a new string where all the adjacent chars are now separated by a "*".

allStar("hello") → "h*e*l*l*o"
allStar("abc") → "a*b*c"
allStar("ab") → "a*b"
Java :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public static String allStar(String str)
 {
  //Stop Condition
  if(str.length()<1) return "";
  if(str.length() == 1) return str+"*";
  
  
  //Main recursive process
  return str.charAt(0)+"*"+allStar(str.substring(1));
 }

Hasil :

C# :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
   public static string allStar(String str)
        { 
            //Kondisi stop
            if (str.Length < 1) return "";
            if (str.Length == 1) return str;


            //Proses rekursif
            return str[0] + "*" + allStar(str.Substring(1));
        }

Hasil :

array11 - Recursive - Java & C#

Given an array of ints, compute recursively the number of times that the value 11 appears in the array. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.

array11([1, 2, 11], 0) → 1
array11([11, 11], 0) → 2
array11([1, 2, 3, 4], 0) → 0

Java :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static int array11(int[] nums, int index)
 {
  //Stop Condition
  if(nums.length == 0 ) return 0;
  if(nums.length == 1)
  {
   if(nums[0] == 11) return 1;
   else return 0;
  }
  
  //Main recursive process
  int[] temp = Arrays.copyOfRange(nums, index+1, nums.length);
  if(nums[index] == 11) return 1+array11(temp,index);
  else return array11(temp,index);
 }
 

Test Result :

C# :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 public static int array11(int[] nums, int index)
        { 
            //Kondisi stop
            if (nums.Length == 0) return 0;
            if (nums.Length == 1)
            {
                if (nums[0] == 11) return 1;
                else return 0;
            }

            //Proses rekursif
            int[] temp = new int[nums.Length - index - 1];
            System.Array.Copy(nums, index+1, temp, 0, temp.Length);
            if (nums[index] == 11) return 1 + array11(temp, index);
            else return array11(temp, index);
        }

Test Result:


Tuesday, June 14, 2016

changePi - Recursive - Java & C#

Given a string, compute recursively (no loops) a new string where all appearances of "pi" have been replaced by "3.14".

changePi("xpix") → "x3.14x"
changePi("pipi") → "3.143.14"
changePi("pip") → "3.14p"

Java :
1
2
3
4
5
6
7
public String changePi(String str) {
  if (str.length() < 1) return "";
 int index = str.indexOf("pi");
 if (index < 0) return str;
 String temp = str.substring(0, index);
 return temp+"3.14"+changePi(str.substring(index+2, str.length()));
}

Test result:
C# :
1
2
3
4
5
6
7
8
9
public static String changePi(String str)
 {
            if (str.Length < 1) return "";
            int index = str.IndexOf("pi");
            if (index < 0 ) return str;
            String temp0 = str.Substring(0, index);
            String temp = str.Substring(index+2, str.Length-(2+index));
            return temp0+"3.14" + changePi(temp);
 }

Test Result :

changeXY - Recursive - Java & C#

Given a string, compute recursively (no loops) a new string where all the lowercase 'x' chars have been changed to 'y' chars.

changeXY("codex") → "codey"
changeXY("xxhixx") → "yyhiyy"
changeXY("xhixhix") → "yhiyhiy"
Java :
1
2
3
4
5
6
public String changeXY(String str) {
  if(str.length() < 1) return "";
  char temp = str.charAt(str.length()-1);
  if(temp == 'x') temp = 'y';
  return changeXY(str.substring(0,str.length()-1))+temp;
}

Test Result:

C#:
1
2
3
4
5
6
7
public static String changeXY(String str)
        {
            if (str.Length < 1) return "";
            Char temp = str[str.Length - 1];
            if (temp == 'x') temp = 'y';
            return changeXY(str.Substring(0, str.Length - 1)) + temp;
        }

Test Result :

Add Unitest to C# Console Project

Pertama-tama silahkan buat project console baru...Setelah selese, klik kanan dibagian solution projectnya seperti terlihat di bawah :
Setelah selese ngebuat project unitestnya... Klik kanan di project unit test itu... terus pilih "Add - Reference "
Terus pilih tab "Solution" terus centang project utama kita kyk gini :
Naaah...dibagian reference tar kliatan :
Sampe sini kita udah bisa masukin assert2 dkk... :D
Kyk gini :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestCodingBat
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {        
            Assert.AreEqual(3, Codingbat_csharp.Program.powerN(3, 1));
        }


        [TestMethod]
        public void TestMethod2()
        {
            Assert.AreEqual(1000, Codingbat_csharp.Program.powerN(10, 3));
        }


        [TestMethod]
        public void TestMethod3()
        {
            Assert.AreEqual(32, Codingbat_csharp.Program.powerN(2, 5));
        }
    }
}

Klu kita run :


Hasilnya :



Yeeeei... sekiaan...semoga bermanfaat.. :)