Sabtu, 23 Januari 2010

Persiapan UAS Praktek C# : Mencari Genap dan bilangan terbesar (OOP)











using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace aplikasi1
{
    public class org_tua
    {
        private int[] angka = new int[101];
     
        protected int dat;
        protected void x_angka (int x, int y)
        {
            angka[x] = y + y;        
        }
        protected int y_angka(int z)
        {
            return angka[z];
        }
       
        public virtual void GetDat()
        {
            Console.Clear();
            Console.Write("Berapa Banyak Data [1-100] : ");
        }

    }

    class Anak_data : org_tua
    {
        genap g = new genap();

        public override void GetDat()
        {
            base.GetDat();
            g.cek_num(Console.ReadLine(),1);
            base.dat = g.ambil_num;
            Console.WriteLine();
            Console.WriteLine("Masukkan Angka 1-10 :");
            int i =1;
            while((i<=base.dat)&&(base.dat<=100)){
                Console.Write("Data ke-{0} : ", i);
                g.cek_num(Console.ReadLine(),2);
                base.x_angka(i, g.ambil_num);

                if (g.ambil_num <= 10 && g.ambil_num >= 1)
                {
                    Console.Write("{0} + {1} = {2} ", g.ambil_num, g.ambil_num, base.y_angka(i));
                    i += 1;
                }
                Console.WriteLine();
            }

        } 

       

        public void bil_genap()
        {
            try
            {
                if (base.dat > 0)
                {
                    int j;
                    for (j = 1; j <= base.dat; j++)
                    {
                        Console.Write("{0}. Penjumlahan genap sampai {1} = ", j, base.y_angka(j));
                        g.cetak(this, j);
                        Console.WriteLine();
                    }
                }
                else
                {
                    throw new Exception("Tidak Ada Data!.");
                }
            }
            catch (Exception warning)
            {
                MessageBox.Show(warning.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        public void bil_max()
        {
            try
            {
                if (base.dat > 0)
                {
                    int j;
                    Console.WriteLine("Data : ");
                    for (j = 1; j <= base.dat; j++)
                    {
                        Console.WriteLine("Data Ke-{0} = {1}", j, base.y_angka(j));
                    }

                    Console.WriteLine();
                    Console.Write("Bilangan Terbesar Dari Data Di Atas Adalah : ");
                    g.max(this, base.dat);
                }
                else
                {
                    throw new Exception("Tidak Ada Data!.");
                }
            }
            catch (Exception warning)
            {
                MessageBox.Show(warning.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        public int z_angka(int z)
        {

            return base.y_angka(z);
          
        }



    }


    class genap
    {
        private int num;
        public void cek_num(String s, byte pil)
        {
            try
            {
                num = 0;
                bool cek = int.TryParse(s, out num);
                if ((cek == false))
                {                  
                    throw new Exception("Data yang anda masukkan tidak valid. Lihat Kembali Ketentuannya.");
                }
                else
                {
                    if ((pil == 1) && ((num < 1) && (num > 100)))
                    {
                        throw new Exception("Data yang anda masukkan tidak valid. Lihat Kembali Ketentuannya.");
                      
                    }
                    else if ((pil == 2) && ((num < 1) && (num > 10)))
                    {
                        throw new Exception("Data yang anda masukkan tidak valid. Lihat Kembali Ketentuannya.");
               
                    }
                   
                }
            }
            catch (Exception warning)
            {
                MessageBox.Show(warning.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
       
        public int ambil_num
        {
            get
            {return num;}
        }

        public void cetak(Anak_data ad, int j)
        {
            int temp=0;
            for (int k = 1; k <= ad.z_angka(j); k++)
            {
                if (k % 2 == 0)
                {
                    temp = temp + k;
                    if (k == ad.z_angka(j) || k == ad.z_angka(j) - 1)
                    {
                        Console.Write("{0} = {1} ", k, temp);
                    }
                    else
                    {
                        Console.Write("{0} + ", k);
                    } 
                }
            }
        }
        private int[] angka_max = new int[101];
     
        public void max(Anak_data ad, int dat)
        {
            int temp;
           
            temp = ad.z_angka(1);
            int i = 1;
            angka_max[i] = 1;
            for (int k = 2; k <= dat; k++)
            {               
                if (ad.z_angka(k) >= temp)
                {

                    if (ad.z_angka(k) == temp)
                    {
                        i += 1;
                        angka_max[i] = k;
                    }
                    else
                    {
                        temp = ad.z_angka(k);
                        i = 1;
                        angka_max[i] = k;
                    }
                }
            }
            Console.Write("{0}", temp);
            Console.WriteLine();
            Console.Write("Ada Pada Data Ke - ");
            for (int k = 1; k <= i; k++)
            {
                if (k > 1)
                {
                    if (k == i)
                    {
                        Console.Write(" dan Ke - {0}", angka_max[k]);
                    }
                    else
                    {
                        Console.Write(", Ke - {0},", angka_max[k]);
                    }
                }
                else
                {
                    Console.Write("{0}", angka_max[k]);
                }
            }
        }
    }

    class umum
    {

        Anak_data ad = new Anak_data();

        public void menu()
        {
            String pilihan = "";

            do
            {
                try
                {
                    Console.Clear();
                    Console.WriteLine("Menu");
                    Console.WriteLine("A.Input Data");
                    Console.WriteLine("B.Genap");
                    Console.WriteLine("C.Bilangan Terbesar");
                    Console.WriteLine("D.Biodata Pemrogram");
                    Console.WriteLine("E.Exit");
                    Console.Write("Pilih menu : ");
                    pilihan = Console.ReadLine();
                    decimal num = 0;
                    bool cek = decimal.TryParse(pilihan, out num);
                    if (cek == true)
                    {
                        throw new Exception("Anda Memasukkan Angka ");
                    }


                    switch (pilihan.ToUpper())
                    {
                        case "A":
                            ad.GetDat();
                            Console.ReadLine();
                            break;

                        case "B":
                            Console.Clear();
                            ad.bil_genap();

                            Console.ReadLine();
                            break;

                        case "C":
                            Console.Clear();
                            ad.bil_max();

                            Console.ReadLine();
                            break;

                        case "D":
                            Console.Clear();
                            Console.WriteLine("Fahmi Faizal");
                            Console.WriteLine("juventini_11ind@yhoo.com");
                            Console.WriteLine("© 2010 ASISTEN-2007. All rights reserved.");
                            Console.ReadLine();
                            break;

                        case "E":
                            throw new Exception("Terimakasih");
                        default:                            
                            throw new Exception("Maaf, pilihan anda Salah!!");
                    }


                }
                catch (Exception warning)
                {
                    MessageBox.Show(warning.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            } while (pilihan.ToUpper() != "E");

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------
//MENU PROGRAM PEMANGGIL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace aplikasi1
{
   
    class kelas_utama
    {
        static void Main()
        {
            umum u = new umum();
            u.menu();
        }
    }
}