Senin, 22 Desember 2014

PROGRAM BILANGAN FIBONACCI



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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {

            int x = 1, y = 1, z = 1;

            Console.Write("Masukan jumlah bilangan fibonacci = ");
            int jumlah = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < jumlah; i++)
            {
                Console.Write(z + " ");
                z = x + y;
                x = y;
                y = z;
            }
            Console.Read();

        }
    }

}




Hasil Outputnya


PROGRAM SUSUNAN BINTANG WARNA TERTENTU



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

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            int baris, kolom;

            for (baris = 0; baris <= 9; baris++)
            {
                for (kolom = 0; kolom <= baris; kolom++)
                {
                    if (kolom % 2 == 1)
                    {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.Write("*");
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("*");
                    }
                }
                Console.Write("\n");
                Console.ReadLine();
            }
        }
    }
}



Hasil Outputnya




Minggu, 21 Desember 2014

Cara Menggabungkan 2 Method

Tugas Pemrograman Visual 2
Menggabungkan 2 Method (Mencari jumlah nilai & rata-rata)





Nama              : Wahyu Nurfian
NIM                : 12.12.0306
Kelas              : SI 12 A





STMIK AMIKOM PURWOKERTO
TAHUN  2012/2013






CLASS MAHASISWA


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

namespace Latihan
{
    class Mahasiswa
    {
        string jurusan, NIM, nama;

        public string Nama
        {
            get { return nama; }
            set { nama = value; }
        }

        public string NIM1
        {
            get { return NIM; }
            set { NIM = value; }
        }

        public string Jurusan
        {
            get { return jurusan; }
            set { jurusan = 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; }
        }

        float nilai_algoritma, nilai_spbd, nilai_pbo;

        public float Nilai_pbo
        {
            get { return nilai_pbo; }
            set { nilai_pbo = value; }
        }

        public float Nilai_spbd
        {
            get { return nilai_spbd; }
            set { nilai_spbd = value; }
        }


        float nilai_algoritmaa, nilai_spbdd, nilai_pboo;

        public float Nilai_pboo
        {
            get { return nilai_pboo; }
            set { nilai_pboo = value; }
        }

        public float Nilai_spbdd
        {
            get { return nilai_spbdd; }
            set { nilai_spbdd = value; }
        }

        public float Nilai_algoritmaa
        {
            get { return nilai_algoritmaa; }
            set { nilai_algoritmaa = value; }
        }

        int thnLhr;

        public int ThnLhr
        {
            get { return thnLhr; }
            set { thnLhr = value; }
        }

        //construktor
        public Mahasiswa()
        {
        }



        //overloading construktor
        public Mahasiswa(string NIM, string nama, string jurusan, DateTime tgl_lahir, double ipk, float nilai_algoritma, float nilai_spbd, float 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;    
        }

       
        // method
        public void tampilData(Mahasiswa mhs)
        {
            Console.WriteLine("NIM \t\t: "+mhs.NIM);
            Console.WriteLine("Nama \t\t: "+mhs.nama);
            Console.WriteLine("Jurusan \t: "+mhs.jurusan);
            Console.WriteLine("Tanggal Lahir   : "+mhs.tgl_lahir);
            Console.WriteLine("IPK \t\t: "+mhs.ipk);
           
        }

        // overloading method
        public void tampilData()
        {
            Console.WriteLine("NIM \t\t: "+NIM);
            Console.WriteLine("Nama \t\t: "+nama);
            Console.WriteLine("Jurusan \t: "+jurusan);
            Console.WriteLine("Tanggal Lahir   : "+tgl_lahir);
            Console.WriteLine("IPK \t\t: "+ipk);
           
        }

        public void isiNilaiAlgoritma(Mahasiswa nilaiAlgoritma)
        {
            //float nilaiA;
            Console.Write("Masukan nilai Algoritma : ");
            nilai_algoritmaa = float.Parse(Console.ReadLine());
        }

        public void isiNilaiSpbd(Mahasiswa nilaiSpbd)
        {
            //float nilaiS;
            Console.Write("Masukan Nilai SPBD : ");
            nilai_spbdd = float.Parse(Console.ReadLine());
        }

       
        public void isiNilaiPbo(Mahasiswa nilaiPbo)
        {
            //float nilaiP;
            Console.Write("Masukan Nilai PBO : ");
            nilai_pboo = float.Parse(Console.ReadLine());
        }

        public void jumlahNilai(Mahasiswa jumlah)
        {
            float algo, spbd, pbo, jml;
            algo=nilai_algoritmaa;
            spbd = nilai_spbdd;
            pbo = nilai_pboo;
            jml = algo + spbd + pbo;
            Console.WriteLine("Jumlah Nilai : " + jml);
        }

        public void rataRata(Mahasiswa rata)
        {
            float algo, spbd, pbo, rata2;
            algo = nilai_algoritmaa;
            spbd = nilai_spbdd;
            pbo = nilai_pboo;
            rata2 =(algo + spbd + pbo)/ 3;
            Console.WriteLine("Rata - Rata : " + rata2);
        }
        public void uu(Mahasiswa uuu)
        {
            Console.Write("ketik kan tahun lahir : ");
            thnLhr = int.Parse(Console.ReadLine());
            int um = DateTime.Now.Year - thnLhr;
            Console.WriteLine("");
            Console.WriteLine("umur sekarang : " + um);
        }
}}


CLASS PROGRAM
  

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

namespace Latihan
{
    class Program
    {
        static void Main(string[] args)
        {

            Mahasiswa mhs1 = new Mahasiswa();


            mhs1.Nama = "Nurvina Dwi Andari";
            mhs1.NIM1 = "12.12.0306";
            mhs1.Jurusan = "Sistem Informasi";
            mhs1.Tgl_lahir = DateTime.Parse("1994-07-01");
            mhs1.Ipk = 3.75;

            mhs1.tampilData(mhs1);
            Console.WriteLine("");

            mhs1.isiNilaiAlgoritma(mhs1);
            mhs1.isiNilaiSpbd(mhs1);
            mhs1.isiNilaiPbo(mhs1);
            Console.WriteLine("");

            mhs1.jumlahNilai(mhs1);
            Console.WriteLine("");

            mhs1.rataRata(mhs1);
            Console.WriteLine("");

            mhs1.uu(mhs1);

            Console.Read();
        }
    }
}








Menampilkan Data Grid View Matakuliah

TUGAS PEMROGRAMAN VISUAL II






Disusun Oleh
         Nama              : Wahyu Nurfian
         NIM                : 12.12.0062
         Kelas              : SI 12 A



SEKOLAH TINGGI MANAJEMEN INFORMATIKA DAN KOMPUTER
AMIKOM PURWOKERTO
JL.LET.JEND.POL SUMARTO PURWOKERTO
2013



Class form utama

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TesDatSet
{
Public partial class Form1 : Form
    {
public frmmatakuliah()
        {
            InitializeComponent();
        }

Private void frmmatakuliah_Load(object sender, EventArgs e)
        {
            noSKS();
            noSemester();
            tampilData();
        }       

Private void cmdSimpan_Click(object sender, EventArgs e)
        {
            simpanData();
        }

Private void cmdEdit_Click(object sender, EventArgs e)
        {
            ubahData();
        }

Private void cmdHapus_Click(object sender, EventArgs e)
        {
            hapusData();
        }

Private void cmdReset_Click(object sender, EventArgs e)
        {
            empty();
        }

Private void cmdTutup_Click(object sender, EventArgs e)
        {
Application.Exit();
        }

Private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
//seleksiBaris();
            txtKode.Text = (string)dataGridView1["kode_matkul", e.RowIndex].Value.ToString();
            txtNama.Text = (string)dataGridView1["nama_matkul", e.RowIndex].Value.ToString();
            cmbSks.SelectedItem = (byte)dataGridView1["sks", e.RowIndex].Value;
            txtPrasyarat.Text = (string)dataGridView1["prasyarat", e.RowIndex].Value.ToString();
            cmbSemester.SelectedItem = (byte)dataGridView1["semester", e.RowIndex].Value;
        }

Private void txtCari_TextChanged(object sender, EventArgs e)
        {
            cariData();
        }

// Method

void noSKS()
        {
            cmbSks.Items.Add("");
for (byte i = 1; i <= 6; i++)
            {
                cmbSks.Items.Add(i);
            }
        }

void noSemester()
        {
            cmbSemester.Items.Add("");
for (byte i = 1; i <= 8; i++)
            {
                cmbSemester.Items.Add(i);
            }
        }

void empty()
        {
            txtKode.Clear();
            txtNama.Clear();
            cmbSks.SelectedItem = "";
            txtPrasyarat.Clear();
            cmbSemester.SelectedItem = "";
            txtKode.Focus();
        }

void tampilData()
        {
string query = "select * from matakuliah order by kode_matkul asc";
matakuliahManager MM = new matakuliahManager();
            MM.TampilData(query, "matakuliah", dataGridView1);
        }

void simpanData()
        {
matakuliah m = new matakuliah(txtKode.Text, txtNama.Text, int.Parse(cmbSks.Text), txtPrasyarat.Text, int.Parse(cmbSemester.Text));
//matakuliah m = new matakuliah(txtKode.Text, txtNama.Text, cm);
matakuliahManager mm = new matakuliahManager();

int x = mm.add(m);
if (x == 1)
            {
MessageBox.Show("Data berhasil disimpan !");
                empty();
                tampilData();
            }
else
            {
MessageBox.Show("Data gagal disimpan !");
            }
        }

void hapusData()
        {
matakuliah m = new matakuliah(txtKode.Text);
matakuliahManager mm = new matakuliahManager();

int x = mm.delete(m);
if (txtKode.Text == "")
            {
MessageBox.Show("Pilih dahulu data yang akan dihapus");
            }
else
            {
if (x == 1)
                {
MessageBox.Show("Hapus data berhasil");
                    empty();
                    tampilData();
                }
else
                {
MessageBox.Show("Hapus data gagal");
                }
            }
        }

void ubahData()
        {
matakuliah m = new matakuliah(txtKode.Text, txtNama.Text, int.Parse(cmbSks.Text), txtPrasyarat.Text, int.Parse(cmbSemester.Text));
matakuliahManager mm = new matakuliahManager();

int x = mm.ubah(m);
try
            {
if (x == 1)
                {
MessageBox.Show("Ubah data berhasil !");
                    empty();
                    tampilData();
                }
else
                {
MessageBox.Show("Ubah data gagal !");
                }

            }
catch (Exception EX)
            {
MessageBox.Show(EX.Message);
            }

        }

void cariData()
        {
string query = "select* from matakuliah where nama_matkul like '%" + txtCari.Text + "%'";
matakuliahManager m = new matakuliahManager();
            m.TampilData(query, "matakuliah", dataGridView1);



Class Koneksi

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

namespace TesDataSet
{   
Class koneksi
    {
Static string conn;

Public static SqlConnection Conn
        {
get
            {
Return new SqlConnection(conn);
            }
        }
static koneksi()
        {
string connStr = "data source=VIENA; initial catalog=db_matakuliah; integrated security=true;";
            conn = connStr;
        }
    }
}










Class Fungsi

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace TesDataSet
{   
Class fungsi
    {
Static SqlConnection conn;
Static SqlCommand cmd;
Static SqlDataAdapter da;
Static DataSet ds;

Public staticint EQuery(string query)
        {
            conn = koneksi.Conn;
            cmd = new SqlCommand();
try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = query;

int i = cmd.ExecuteNonQuery();
return i;
            }
catch (SqlException ex)
            {
Console.WriteLine(ex.ToString());
return 0;
            }
finally
            {
                conn.Close();
            }
        }
publicstaticDataSet DataGrid(string query, string tabel)
        {
            conn = koneksi.Conn;
            da = newSqlDataAdapter(query, conn);
            ds = newDataSet();

try
            {
                conn.Open();
                da.Fill(ds, tabel);
return ds;
            }
catch (SqlException e)
            {
MessageBox.Show(e.Message);
returnnull;
            }
finally
            {
                conn.Close();
            }
        }
    }
}






Class Matakuliah

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

namespace TesDataSet
{
Class matakuliah
    {
string kode, nama, prasyarat;

public string Prasyarat
        {
get { return prasyarat; }
set { prasyarat = value; }
        }

Public string Nama
        {
get { return nama; }
set { nama = value; }
        }

Public string Kode
        {
get { return kode; }
set { kode = value; }
        }
int sks, semester;

public int Semester
        {
get { return semester; }
set { semester = value; }
        }

Public int Sks
        {
get { return sks; }
set { sks = value; }
        }
//constructor
public matakuliah(string kode, string nama, int sks, string prasyarat, int semester)
        {
this.kode = kode;
this.nama = nama;
this.prasyarat = prasyarat;
this.sks = sks;
this.semester = semester;

        }

public matakuliah(string kode)
        {
this.kode = kode;
        }
    }
}





Class Matakuliah Manager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TesDataSet
{
Class matakuliahManager
    {
Public void TampilData(string query, string tabel, DataGridView DG)
        {
try
            {
                DG.DataSource = fungsi.DataGrid(query, tabel);
                DG.DataMember = tabel;
                DG.Columns[0].HeaderText = "Kode";
                DG.Columns[1].HeaderText = "Nama Matakuliah";
                DG.Columns[2].HeaderText = "SKS";
                DG.Columns[3].HeaderText = "Prasyarat";
                DG.Columns[4].HeaderText = "Semester";

                DG.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                DG.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                DG.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

                DG.AutoResizeColumns();
                DG.AutoResizeColumnHeadersHeight();
                DG.AlternatingRowsDefaultCellStyle.BackColor = Color.Blue;
            }
catch (Exception e)
            {
MessageBox.Show("Error : " + e.Message);
            }
finally
            {
            }
        }
Public int add(matakuliah mk)
        {
string query;
if (mk.Prasyarat.ToString() == "")
            {
                query = "insert into matakuliah values('" + mk.Kode + "','" + mk.Nama + "',"
                    + mk.Sks + ",null," + mk.Semester + ");";
            }
else
            {
                query = "insert into matakuliah values('" + mk.Kode + "','"
                    + mk.Nama + "'," + mk.Sks + ",'" + mk.Prasyarat + "'," + mk.Semester + ");";

            }
try
            {
int x = fungsi.EQuery(query);
return x;
            }
catch (Exception ex)
            {
MessageBox.Show(ex.Message);
return 0;
            }
        }

publicint delete(matakuliah mkk)
        {
string query;

            query = " delete from matakuliah where kode_matkul='" + mkk.Kode + "'";
try
            {
int x = fungsi.EQuery(query);
return x;
            }
catch (Exception ex)
            {
return 0;
            }
        }

Public int ubah(matakuliah mk)
        {
string query;
if (mk.Prasyarat == null)
            {
                query = "update matakuliah set nama_matkul  = '" + mk.Nama + "',sks = " + mk.Sks + ",prasyarat = '" + mk.Prasyarat + "',semester = " + mk.Semester + " where kode_matkul = '" + mk.Kode + "'";
            }
else
            {
                query = "update matakuliah set nama_matkul  = '" + mk.Nama + "',sks = " + mk.Sks + ",prasyarat = null,semester = " + mk.Semester + " where kode_matkul = '" + mk.Kode + "'";

            }
try
            {
int x = fungsi.EQuery(query);
return x;
            }
catch (Exception ex)
            {
return 0;
            }
        }
    }
}





Hasil Outpunya :