Sunday, November 29, 2009

how to use arraylist - arraylist example

here i am going to write a program which shows example of arraylist.
think in this case that i am using arraylist to associate each
student with courses taken.

arraylists are used for collection type purposes.so it is used
widely.

program codes are following-
public class Course
    {
        private string name;
        public string Name
        {
            get { return name; }
            //set { name = value; }
        }
        public Course(string name) {
            this.name = name;
        }
        public string Output()
        {
             return "Course Name : "+ name + " ";
        }
    }
public class Student
    {
        private string name;
        public string Name
        {
            get { return name; }
        }
        private ArrayList courseTaken = new ArrayList();
        public ArrayList CourseTaken
        {
            get { return courseTaken; }
            //set { courseTaken = value; }
        }      
        public Student(string name) {
            this.name = name;      
        }
        public string Output() {
            return "Student Name : "+ name + "\n";
        }
    }
here is the entry form::
public partial class MridulExampleUi : Form
    {
        public MridulExampleUi()
        {
            InitializeComponent();
        }      
        private void MridulExampleUi_Load(object sender, EventArgs e)
        {}
        private void ShowArraylistButton_Click(object sender, EventArgs e)
        {       
            Student studentObj = new Student("mridul");
            Student studentObj3 = new Student("someone");
            Course courseObj1 = new Course("Programming");
            Course courseObj2 = new Course("Web designing");                
            studentObj.CourseTaken.Add(courseObj1);
            studentObj.CourseTaken.Add(courseObj2);
            studentObj3.CourseTaken.Add(courseObj1);         
            ArrayList arrayObj = new ArrayList();
            arrayObj.Add(studentObj);
            arrayObj.Add(studentObj3);          

            string message="";
            foreach (Student studentObj2 in arrayObj)
            {
                message+=studentObj2.Output();
                foreach(Course courseObj3 in studentObj2.CourseTaken)
                {
                  message+=courseObj3.Output();\                     
                }
                message += "\n\n";
            }
            MessageBox.Show(message);          
        }}
here is the program entry class::
static class Program
    {
        ///
        /// The main entry point for the application.
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MridulExampleUi());
        }
    }

executing parameterless base constructor for initializing some attributes of base class

Suppose you have a bank account class.from which you will generate
two classes-savings account & checking account.each one has other
attributes besides common base class attributes.

suppose common 'balance' attribute of base class needed to be
initialized to 0 then you will do that by the base  parameterless
constructor of the base class as the following way -

public abstract class BankAccount
    {
        private string accountNumber;
        private string customerName;
        private double balance;

        public BankAccount(string accountNumber, string customerName) : this()
        {
            this.accountNumber = accountNumber;
            this.customerName = customerName;
        }
       
       // here is the first constructor for initializing balance

        public BankAccount()
        {
            this.balance = 0;
        }
}

Tuesday, November 10, 2009

privacy policy

At http://www.coderarticles.blogspot.com/ the
visitors privacy is followed strictly.
this privacy policy indicates what
types of information of viewers is
received & how it is used.
 
1.Doubleclick DART cookies
by using advertising partners ads for
my website at any time,our partners
may use cookies(like Dart cookies,other
cookies) & web beacons in my website for
serving ads to my site viewers when
viewers visits my website by browsers.

those third party advertising servers
uses best technology to the advertisements
which appears on my website send
to viewers internet browsers directly.
by this way they receive IP addresses,
others of viewers computer.

www.coderarticles.blogspot.com has least
control over these advertisers cookies.

You may accept or not accept cookies by
your browsers.

2.The links to other sites
My site may contain links to other site
for purposes.If viewers go to those
linked websites,blog sites,I will not be
responsible for any security and privacy
of users.viewers are recommended to read
privacy policy of those websites.

3.I may not interchange,sell viewers personal
information which I got by user comments or email
to me & other ways unless I have viewers permission.

At last the contents in this website is
written by me only and not copied from internet
or books,etc.please,please don't try
to copy the info of this website.

An example of how objects can be used to program applications perfectly

Here i am going to write a program.I wrote it by my own
please do not copy.It shows that how applications can be
implemented easily with Objects-



# MridulFruitBusiness applications-

1.It will create a bucket owner with the owner name & the number
of flowers he/she can keep.
2.then he/she can keep in/keep out the flowers in the bucket by selecting
radiobuttons and entering the number of flowers to keep/keepout seperately.
3.if he/she clicks status button at the bottom the overall status will
be shown about how many flowers in the bucket and how many he/she can keep.

#here is the bucket class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MridulFruitsBusiness
{
    public class Bucket
    {
        private string ownerName;

        public string OwnerName
        {
            get { return ownerName; }
            set { ownerName = value; }
        }
        private int numberItems;
        public int NumberItems
        {
            get { return numberItems; }
            set { numberItems = value; }
        }
        public Bucket(string ownerName,int numberItems) {
            this.ownerName = ownerName;
            this.numberItems = numberItems;       
        }
    }
}

#the fruits keeper class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MridulFruitsBusiness
{
    public class FruitsKeeper
    {
       
        public string KeepFruits(int value, int keepValue,int keptValue, Bucket bucketObj)
        {
          
            if (value == 1)
            {
                if (bucketObj.NumberItems < keepValue)
                {
                    return "sorry u try to keep more fruits than the number of flowers";
                }
                else
                {                                                       
                    return "u kept " + keepValue.ToString() + " fruits\nu can keep more " + (bucketObj.NumberItems - keepValue).ToString() + " fruits";

                }
             }

            if (value == 2)
            {                                             
                return keepValue.ToString()+" fruits kept out of the bucket\n"+keptValue.ToString()+" fruits remaining";
            }
            else
            {
                return "no data";
            }
           
        }
    }
}

#starting point class of the program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MridulFruitsBusiness
{
    static class Program
    {
        ///
        /// The main entry point for the application.
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MridulFormUi());
        }
    }
}
#here is the User interface class(the main part)
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 MridulFruitsBusiness
{
    public partial class MridulFormUi : Form
    {
        private Bucket bucketObj = null;
        private int keptValue=0;
        public MridulFormUi()
        {
            InitializeComponent();
        }

        private void gardenerForm_Load(object sender, EventArgs e)
        {

        }       
        private void submitButton_Click(object sender, EventArgs e)
        {
            bucketObj=new Bucket(ownerTextBox.Text,Convert.ToInt16(itemsTextBox.Text));
           
            MessageBox.Show(" bucket created");
        }
        ///
        /// for keeping
        ///

        ///

        ///
        private void keepButton_Click(object sender, EventArgs e)
        {
           
            FruitsKeeper keeperObj = new FruitsKeeper();
            int value=0;
            int keepValue = Convert.ToInt16(keepTextBox.Text);
            if (radioButton1.Checked == true)
            {              
                value = 1;
                keptValue = keepValue;
                MessageBox.Show(keeperObj.KeepFruits(value,keepValue,keptValue,bucketObj));
            }
            if (radioButton2.Checked == true)
            {
                value = 2;
                keptValue = keptValue - keepValue;
                MessageBox.Show(keeperObj.KeepFruits(value, keepValue,keptValue,bucketObj));
            }           
        }
        private void statusButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show("you kept " + keptValue.ToString() + " fruits out of " + bucketObj.NumberItems.ToString());
        }
    }
}

if can't implement-comments are most welcome.good day...

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger