Homework4_A

Create – in both languages C# and VB.NET – a demonstrative program which computes the online arithmetic mean (if it’s a numeric variable) and the distribution for a discrete variable (can use values simulated with RANDOM object).

How i worked

In this application i used a class for store Dogs information, such as weight and breed.
I use Random method for data (i also searched for a database to capture the data, but i didnt’t find it).
I didn’t use a second class to represent the Frequency Distribution but i used a list to store the new dog instances and i used the override method for demostration purpose.

I tried to modulate the code and make it generic, so as to have it more robust and readable. Also for this reason the class has been put in a separate file.

   Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
 
        Dim Dog As Dog = new_dataset()
        ListOfDog.Add(Dog)
        print_Database(Dog)
        online_mean(Dog)
        print_OnArMean()
        discrete_distribution(DicOfBreed, Dog.Breed)
        discrete_distribution(DicOfAgeDistributio, Dog.Age)
    End Sub

Vb.Net

Form1.vb

Public Class Form1
    Public R As New Random

    'List for store the dog
    Public ListOfDog As New List(Of Dog)

    'Array of breed
    Public PossibleBreed() As String = {"German Shepherd", "Alaskan Malamute", "Beauceron", "Bernese Mountain Dog",
                                        "Black Russian Terrier", "Weimaraner", "Pointer", "Rottweiler", "Siberian Husky",
                                        "Afghan Hound", "Boxer", "Collie", "Weimaraner", "Dalmatian", "Golden Retriver",
                                        "Irish Setter", "Labrador Retriver"}

    Public Count As Integer
    Public CurrentArMean As Double

    'Dictionary for Frequence Distribution of the breed(Breed,count)
    Public DicOfBreed As New Dictionary(Of String, Integer)
    'Dictionary for Frequence Distribution of the age(Age,count)
    Public DicOfAgeDistributio As New Dictionary(Of Integer, Integer)


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        inizialized()
    End Sub
    Public Sub inizialized()
        Count = 0
        CurrentArMean = 0
        DicOfBreed.Clear()
        DicOfAgeDistributio.Clear()
        ListOfDog.Clear()
        Me.RTxTDatabase.Text = ("Breed:".PadRight(25) & "Weight:".PadRight(10) & "Age:" & Environment.NewLine & Environment.NewLine)
        Me.RTxtArOnMean.Text = ("Mean: " & Environment.NewLine & Environment.NewLine)
        Me.RTxtBreedDistribution.Text = "Distribution: " & Environment.NewLine & Environment.NewLine
    End Sub




    Public Sub online_mean(CollForMean As Dog)
        'Online algo for mean
        Count += 1
        CurrentArMean = CurrentArMean + (CollForMean.Age - CurrentArMean) / Count

    End Sub
    Public Overloads Sub discrete_distribution(dictionary As Dictionary(Of String, Integer),
                                     b As String)
        If (dictionary.ContainsKey(b)) Then
            dictionary(b) += 1
        Else
            dictionary.Add(b, 1)
        End If
    End Sub
    Public Overloads Sub discrete_distribution(dictionary As Dictionary(Of Integer, Integer), b As Integer)
        If (dictionary.ContainsKey(b)) Then
            dictionary(b) += 1
        Else
            dictionary.Add(b, 1)
        End If
    End Sub
    Public Sub print_OnArMean()
        Me.RTxtArOnMean.AppendText(CurrentArMean.ToString("N2") & Environment.NewLine)
    End Sub
    Public Sub print_Database(Dog As Dog)
        Me.RTxTDatabase.AppendText(Dog.Breed.PadRight(25) & Dog.Weight.ToString("N2").PadRight(12) & Dog.Age & Environment.NewLine)

    End Sub
    Public Overloads Sub print_Distribution(dictionary As Dictionary(Of String, Integer))
        Me.RTxtBreedDistribution.AppendText("Breed Distribution: " & Environment.NewLine & Environment.NewLine)
        Me.RTxtBreedDistribution.AppendText("Count:".PadRight(10).PadLeft(35) & "Relative Freq:".PadRight(18) & "Percentage:" & Environment.NewLine & Environment.NewLine)
        For Each k As KeyValuePair(Of String, Integer) In dictionary
            Dim RelFreq = k.Value / ListOfDog.Count
            Me.RTxtBreedDistribution.AppendText(k.Key.PadRight(30) & k.Value.ToString.PadRight(10) & RelFreq.ToString("N2").PadRight(15) &
                                                 (RelFreq * 100).ToString("N2") & " %" & Environment.NewLine)
        Next
    End Sub
    Public Overloads Sub print_Distribution(dictionary As Dictionary(Of Integer, Integer))
        Me.RTxtBreedDistribution.AppendText(Environment.NewLine & Environment.NewLine & "Age Distribution: " & Environment.NewLine & Environment.NewLine)
        Me.RTxtBreedDistribution.AppendText("Count:".PadRight(10).PadLeft(35) & "Relative Freq:".PadRight(18) & "Percentage:" & Environment.NewLine & Environment.NewLine)
        For Each k As KeyValuePair(Of Integer, Integer) In dictionary
            Dim RelFreq = k.Value / ListOfDog.Count
            Me.RTxtBreedDistribution.AppendText(k.Key.ToString.PadRight(30) & k.Value.ToString.PadRight(10) & RelFreq.ToString("N2").PadRight(15) &
                                                 (RelFreq * 100).ToString("N2") & " %" & Environment.NewLine)
        Next

    End Sub
    Public Function new_dataset() As Dog
        'Large Dog breed Weight's is between 30 and 50kg
        'And can be old max(usually) 12 years (used for discrete mean exercise's)
        'We consider dogs only over 1 year old
        Dim Dog As New Dog With {
            .Breed = PossibleBreed(R.Next(0, PossibleBreed.Length)),
            .Weight = R.NextDouble * 20 + 30,
            .Age = R.Next(1, 13)
        }
        Return Dog
    End Function
    Private Sub BtnGo_Click(sender As Object, e As EventArgs) Handles BtnGo.Click
        Me.Timer.Start()
    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick

        Dim Dog As Dog = new_dataset()
        ListOfDog.Add(Dog)
        print_Database(Dog)
        online_mean(Dog)
        print_OnArMean()
        discrete_distribution(DicOfBreed, Dog.Breed)
        discrete_distribution(DicOfAgeDistributio, Dog.Age)
    End Sub

    Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click
        Me.Timer.Stop()
        Me.RTxtArOnMean.AppendText(Environment.NewLine & ListOfDog.Count & " occurrences" & Environment.NewLine)
    End Sub



    Private Sub BtnDistribution_Click(sender As Object, e As EventArgs) Handles BtnDistribution.Click
        Me.RTxtBreedDistribution.Clear()
        print_Distribution(DicOfBreed)
        print_Distribution(DicOfAgeDistributio)
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        inizialized()
    End Sub
End Class

Dog.vb

Public Class Dog
    Public Breed As String
    Public Age As Integer
    'Public Height As Double
    Public Weight As Double
    'Public Footage As Double

    Public Overrides Function ToString() As String
        Return Breed & " " & Age & " " & Weight
    End Function
End Class

C#

form1.cs

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

namespace Application4_C
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            inizialized();
        }
        public Random R = new Random();

        // List for store the dog
        public List<Dog> ListOfDog = new List<Dog>();

        // Array of breed
        public string[] PossibleBreed = new[] { "German Shepherd", "Alaskan Malamute", "Beauceron", "Bernese Mountain Dog", "Black Russian Terrier", "Weimaraner", "Pointer", "Rottweiler", "Siberian Husky", "Afghan Hound", "Boxer", "Collie", "Weimaraner", "Dalmatian", "Golden Retriver", "Irish Setter", "Labrador Retriver" };

        public int Count;
        public double CurrentArMean;

        // Dictionary for Frequence Distribution of the breed(Breed,count)
        public Dictionary<string, int> DicOfBreed = new Dictionary<string, int>();
        // Dictionary for Frequence Distribution of the age(Age,count)
        public Dictionary<int, int> DicOfAgeDistributio = new Dictionary<int, int>();


        public void inizialized()
        {
            Count = 0;
            CurrentArMean = 0;
            DicOfBreed.Clear();
            DicOfAgeDistributio.Clear();
            ListOfDog.Clear();
            this.RTxTDatabase.Text = ("Breed:".PadRight(25) + "Weight:".PadRight(10) + "Age:" + Environment.NewLine + Environment.NewLine);
            this.RTxtArOnMean.Text = ("Mean: " + Environment.NewLine + Environment.NewLine);
            this.RTxtBreedDistribution.Text = "Distribution: " + Environment.NewLine + Environment.NewLine;
        }




        public void online_mean(Dog CollForMean)
        {
            // Online algo for mean
            Count += 1;
            CurrentArMean = CurrentArMean + (CollForMean.Age - CurrentArMean) / Count;
        }
        public void discrete_distribution(Dictionary<string, int> dictionary, string b)
        {
            if ((dictionary.ContainsKey(b)))
                dictionary[b] += 1;
            else
                dictionary.Add(b, 1);
        }
        public void discrete_distribution(Dictionary<int, int> dictionary, int b)
        {
            if ((dictionary.ContainsKey(b)))
                dictionary[b] += 1;
            else
                dictionary.Add(b, 1);
        }
        public void print_OnArMean()
        {
            this.RTxtArOnMean.AppendText(CurrentArMean.ToString("N2") + Environment.NewLine);
        }
        public void print_Database(Dog Dog)
        {
            this.RTxTDatabase.AppendText(Dog.Breed.PadRight(25) + Dog.Weight.ToString("N2").PadRight(12) + Dog.Age + Environment.NewLine);
        }
        public void print_Distribution(Dictionary<string, int> dictionary)
        {
            this.RTxtBreedDistribution.AppendText("Breed Distribution: " + Environment.NewLine + Environment.NewLine);
            this.RTxtBreedDistribution.AppendText("Count:".PadRight(10).PadLeft(35) + "Relative Freq:".PadRight(18) + "Percentage:" + Environment.NewLine + Environment.NewLine);
            foreach (KeyValuePair<string, int> k in dictionary)
            {
                var RelFreq = k.Value / (double)ListOfDog.Count;
                this.RTxtBreedDistribution.AppendText(k.Key.PadRight(30) + k.Value.ToString().PadRight(10) + RelFreq.ToString("N2").PadRight(15) + (RelFreq * 100).ToString("N2") + " %" + Environment.NewLine);
            }
        }
        public void print_Distribution(Dictionary<int, int> dictionary)
        {
            this.RTxtBreedDistribution.AppendText(Environment.NewLine + Environment.NewLine + "Age Distribution: " + Environment.NewLine + Environment.NewLine);
            this.RTxtBreedDistribution.AppendText("Count:".PadRight(10).PadLeft(35) + "Relative Freq:".PadRight(18) + "Percentage:" + Environment.NewLine + Environment.NewLine);
            foreach (KeyValuePair<int, int> k in dictionary)
            {
                var RelFreq = k.Value / (double)ListOfDog.Count;
                this.RTxtBreedDistribution.AppendText(k.Key.ToString().PadRight(30) + k.Value.ToString().PadRight(10) + RelFreq.ToString("N2").PadRight(15) + (RelFreq * 100).ToString("N2") + " %" + Environment.NewLine);
            }
        }
        public Dog new_dataset()
        {
            // Large Dog breed Weight's is between 30 and 50kg
            // And can be old max(usually) 12 years (used for discrete mean exercise's)
            // We consider dogs only over 1 year old
            Dog Dog = new Dog()
            {
                Breed = PossibleBreed[R.Next(0, PossibleBreed.Length)],
                Weight = R.NextDouble() * 20 + 30,
                Age = R.Next(1, 13)
            };
            return Dog;
        }
        private void BtnGo_Click_1(object sender, EventArgs e)
        {
            Timer.Start();
        }


        private void BtnStop_Click_1(object sender, EventArgs e)
        {
            this.Timer.Stop();
            this.RTxtArOnMean.AppendText(Environment.NewLine + ListOfDog.Count + " occurrences" + Environment.NewLine);
        }



        private void BtnDistribution_Click_1(object sender, EventArgs e)
        {
            this.RTxtBreedDistribution.Clear();
            print_Distribution(DicOfBreed);
            print_Distribution(DicOfAgeDistributio);
        }

        private void BtnClear_Click(object sender, EventArgs e)
        {
            inizialized();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            Dog Dog = new_dataset();
            ListOfDog.Add(Dog);
            print_Database(Dog);
            online_mean(Dog);
            print_OnArMean();
            discrete_distribution(DicOfBreed, Dog.Breed);
            discrete_distribution(DicOfAgeDistributio, Dog.Age);
        }
    }
}

Dog.cs

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

namespace Application4_C
{
    public class Dog
    {
        public string Breed;
        public int Age;
        // Public Height As Double
        //public double Weight;
        // Public Footage As Double

        public override string ToString()
        {
            return Breed + " " + Age + " " + Weight;
        }
    }
}

Lascia un commento