Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

A practice project for beginners (Score Calculator)

In this exercise, you’ll create a form that accepts one or more scores from the user. Each time a score is added, the score total, score count, and average score are calculated and displayed.



1. Start a new project named ScoreCalculator.
2. Add labels, text boxes, and buttons to the default form and set the properties of the form and its controls so they appear as shown above. When the user presses the Enter key, the Click event of the Add button should fire. When the user presses the Esc key, the Click event of the Exit button should fire.
3. Declare two class variables to store the score total and the score count.
4. Create an event handler for the Click event of the Add button. This event handler should get the score the user enters, calculate and display the score total, score count, and average score, and move the focus to the Score text box. It should provide for integer entries, but you can assume that the user will enter valid integer values.
5. Create an event handler for the Click event of the Clear Scores button. This event handler should set the two class variables to zero, clear the text boxes on the form, and move the focus to the Score text box.
6. Create an event handler for the Click event of the Exit button that closes the form.
7. Test the application to be sure it works correctly.


As an additional challenge...
1. Declare a class variable for an array that can hold up to 20 scores.
2. Modify the Click event handler for the Add button so it adds the score that’s entered by the user to the next element in the array. To do that, you can use the score count variable to refer to the element.
3. Move the Clear Scores button as shown above. Then, modify the Click event handler for this button so it removes any scores that have been added to the array. The easiest way to do that is to create a new array and assign it to the array variable.
4. Add a Display Scores button that sorts the scores in the array, displays the scores in a dialog box, and moves the focus to the Score text box. Be sure that only the elements that contain scores are displayed.
5. Test the application to be sure it works correctly.
It's only after we've lost everything that we're free to do anything.
form code
Code
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace ScoreCalculator
{
   public partial class form1 : Form
   {
       public form1()
       {
           InitializeComponent();
       }

       int[] intScoreTotalsArray = new int[20];
       int intScoreCount = 0;

       private void btnAdd_Click(object sender, EventArgs e)
       {
           int intScore = 0;
           Int32.TryParse(txtScore.Text, out intScore);
           intScoreTotalsArray[intScoreCount] = intScore;
           intScoreCount++;
           txtScoreTotal.ToString();
           txtScoreCount.Text = intScoreCount.ToString();
           txtScore.Focus();
           txtScore.SelectAll();
           txtAverage.Text = intScoreTotalsArray.Where(v => v > 0).Average(v => v).ToString();
           txtScoreTotal.Text = intScoreTotalsArray.Where(v => v > 0).Sum(v => v).ToString();
       }
       private void btnClearScores_Click(object sender, EventArgs e)
       {
           this.txtScore.Clear();
           this.txtScoreCount.Clear();
           this.txtAverage.Clear();
           this.txtScoreTotal.Clear();
           this.intScoreCount = 0;
           this.intScoreTotalsArray = new int[20];
           txtScore.Focus();
       }
       private void btnExit_Click(object sender, EventArgs e)
       {
           this.Close();
       }

   }
}
autogenerated code
Code
namespace ScoreCalculator
{
   partial class form1
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ComponentModel.IContainer components = null;
       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
       protected override void Dispose(bool disposing)
       {
           if (disposing && (components != null))
           {
               components.Dispose();
           }
           base.Dispose(disposing);
       }
       #region Windows Form Designer generated code
       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
           this.txtScore = new System.Windows.Forms.TextBox();
           this.txtScoreTotal = new System.Windows.Forms.TextBox();
           this.txtScoreCount = new System.Windows.Forms.TextBox();
           this.txtAverage = new System.Windows.Forms.TextBox();
           this.label1 = new System.Windows.Forms.Label();
           this.label2 = new System.Windows.Forms.Label();
           this.label3 = new System.Windows.Forms.Label();
           this.label4 = new System.Windows.Forms.Label();
           this.btnClearScores = new System.Windows.Forms.Button();
           this.btnExit = new System.Windows.Forms.Button();
           this.btnAdd = new System.Windows.Forms.Button();
           this.SuspendLayout();
           //
           // txtScore
           //
           this.txtScore.Location = new System.Drawing.Point(81, 12);
           this.txtScore.Name = "txtScore";
           this.txtScore.Size = new System.Drawing.Size(53, 20);
           this.txtScore.TabIndex = 0;
           //
           // txtScoreTotal
           //
           this.txtScoreTotal.Location = new System.Drawing.Point(81, 39);
           this.txtScoreTotal.Name = "txtScoreTotal";
           this.txtScoreTotal.ReadOnly = true;
           this.txtScoreTotal.Size = new System.Drawing.Size(53, 20);
           this.txtScoreTotal.TabIndex = 1;
           //
           // txtScoreCount
           //
           this.txtScoreCount.Location = new System.Drawing.Point(81, 67);
           this.txtScoreCount.Name = "txtScoreCount";
           this.txtScoreCount.ReadOnly = true;
           this.txtScoreCount.Size = new System.Drawing.Size(53, 20);
           this.txtScoreCount.TabIndex = 2;
           //
           // txtAverage
           //
           this.txtAverage.Location = new System.Drawing.Point(81, 93);
           this.txtAverage.Name = "txtAverage";
           this.txtAverage.ReadOnly = true;
           this.txtAverage.Size = new System.Drawing.Size(53, 20);
           this.txtAverage.TabIndex = 3;
           //
           // label1
           //
           this.label1.AutoSize = true;
           this.label1.Location = new System.Drawing.Point(37, 15);
           this.label1.Name = "label1";
           this.label1.Size = new System.Drawing.Size(38, 13);
           this.label1.TabIndex = 4;
           this.label1.Text = "Score:";
           //
           // label2
           //
           this.label2.AutoSize = true;
           this.label2.Location = new System.Drawing.Point(10, 42);
           this.label2.Name = "label2";
           this.label2.Size = new System.Drawing.Size(65, 13);
           this.label2.TabIndex = 5;
           this.label2.Text = "Score Total:";
           //
           // label3
           //
           this.label3.AutoSize = true;
           this.label3.Location = new System.Drawing.Point(6, 70);
           this.label3.Name = "label3";
           this.label3.Size = new System.Drawing.Size(69, 13);
           this.label3.TabIndex = 6;
           this.label3.Text = "Score Count:";
           //
           // label4
           //
           this.label4.AutoSize = true;
           this.label4.Location = new System.Drawing.Point(25, 96);
           this.label4.Name = "label4";
           this.label4.Size = new System.Drawing.Size(50, 13);
           this.label4.TabIndex = 7;
           this.label4.Text = "Average:";
           //
           // btnClearScores
           //
           this.btnClearScores.Location = new System.Drawing.Point(44, 135);
           this.btnClearScores.Name = "btnClearScores";
           this.btnClearScores.Size = new System.Drawing.Size(90, 23);
           this.btnClearScores.TabIndex = 8;
           this.btnClearScores.Text = "Clear Scores";
           this.btnClearScores.UseVisualStyleBackColor = true;
           this.btnClearScores.Click += new System.EventHandler(this.btnClearScores_Click);
           //
           // btnExit
           //
           this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
           this.btnExit.Location = new System.Drawing.Point(160, 135);
           this.btnExit.Name = "btnExit";
           this.btnExit.Size = new System.Drawing.Size(75, 23);
           this.btnExit.TabIndex = 9;
           this.btnExit.Text = "Exit";
           this.btnExit.UseVisualStyleBackColor = true;
           this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
           //
           // btnAdd
           //
           this.btnAdd.Location = new System.Drawing.Point(160, 12);
           this.btnAdd.Name = "btnAdd";
           this.btnAdd.Size = new System.Drawing.Size(75, 23);
           this.btnAdd.TabIndex = 10;
           this.btnAdd.Text = "Add";
           this.btnAdd.UseVisualStyleBackColor = true;
           this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
           //
           // frmScoreCalculator
           //
           this.AcceptButton = this.btnAdd;
           this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.CancelButton = this.btnExit;
           this.ClientSize = new System.Drawing.Size(257, 172);
           this.Controls.Add(this.btnAdd);
           this.Controls.Add(this.btnExit);
           this.Controls.Add(this.btnClearScores);
           this.Controls.Add(this.label4);
           this.Controls.Add(this.label3);
           this.Controls.Add(this.label2);
           this.Controls.Add(this.label1);
           this.Controls.Add(this.txtAverage);
           this.Controls.Add(this.txtScoreCount);
           this.Controls.Add(this.txtScoreTotal);
           this.Controls.Add(this.txtScore);
           this.Name = "frmScoreCalculator";
           this.Text = "Score Calculator";
           this.ResumeLayout(false);
           this.PerformLayout();

       }
       #endregion
       private System.Windows.Forms.TextBox txtScore;
       private System.Windows.Forms.TextBox txtScoreTotal;
       private System.Windows.Forms.TextBox txtScoreCount;
       private System.Windows.Forms.TextBox txtAverage;
       private System.Windows.Forms.Label label1;
       private System.Windows.Forms.Label label2;
       private System.Windows.Forms.Label label3;
       private System.Windows.Forms.Label label4;
       private System.Windows.Forms.Button btnClearScores;
       private System.Windows.Forms.Button btnExit;
       private System.Windows.Forms.Button btnAdd;
   }
}
(Nov 7, 2019 10:17 AM)Porn Wrote:  form code
Code
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace ScoreCalculator
{
   public partial class form1 : Form
   {
       public form1()
       {
           InitializeComponent();
       }

       int[] intScoreTotalsArray = new int[20];
       int intScoreCount = 0;

       private void btnAdd_Click(object sender, EventArgs e)
       {
           int intScore = 0;
           Int32.TryParse(txtScore.Text, out intScore);
           intScoreTotalsArray[intScoreCount] = intScore;
           intScoreCount++;
           txtScoreTotal.ToString();
           txtScoreCount.Text = intScoreCount.ToString();
           txtScore.Focus();
           txtScore.SelectAll();
           txtAverage.Text = intScoreTotalsArray.Where(v => v > 0).Average(v => v).ToString();
           txtScoreTotal.Text = intScoreTotalsArray.Where(v => v > 0).Sum(v => v).ToString();
       }
       private void btnClearScores_Click(object sender, EventArgs e)
       {
           this.txtScore.Clear();
           this.txtScoreCount.Clear();
           this.txtAverage.Clear();
           this.txtScoreTotal.Clear();
           this.intScoreCount = 0;
           this.intScoreTotalsArray = new int[20];
           txtScore.Focus();
       }
       private void btnExit_Click(object sender, EventArgs e)
       {
           this.Close();
       }

   }
}
autogenerated code
- snip -

Why are you using int.TryParse() if you don't check the return value?

- No array bounds checking for your magic number of 20 elements
- Redundant lambdas in some of your LINQ calls
- Where(v => v > 0).Average(v => v) defeats the point of averaging values too

So you failed.

Explanation: If some of the scores were legitimately 0% for example, you can't use 0 as a special case to conclude that it's a default initialized value.
Code
var x1 = new[] { 0, 0, 0, 10 }.Average();
var x2 = new[] { 0, 0, 0, 10 }.Where(x => x > 0).Average();

x1 = 2.5
x2 = 10
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Luffy-_-Mar 16, 2022 12:36 PM 8 357 Mar 16, 2022 12:36 PM
Last Post: Luffy-_-
[Python] by LSDJul 15, 2020 02:55 AM 2 197 Jul 15, 2020 02:55 AM
Last Post: greasyAlessandra
[Python] by TintinJan 1, 2020 11:01 AM 4 187 Jan 1, 2020 11:01 AM
Last Post: Tintin