Tuesday, January 5, 2010

"Exercise One and Well Begun"

Welcome to Programming for Poets very first post!

I'm learning C# and ASP.NET with no previous programming experience (or any computer experience at all, except extensive excel use). Therefore, I'm going for explanations aimed at the lowest common denominator in "noob" status... people like me :)

Presently, I'm working through the exercises in the book "Learn to Program" by Chris Pine (http://www.pragprog.com/titles/ltp2/learn-to-program-2nd-edition). The book and the exercises are for Ruby, but since I'm working on C#, I'm using the same questions and coming up with my own answers. I've also read Deitel's Learn C# book, and have extensively worked through

The questions in the first chapter are as follows:

Write a program which tells you:

  • how many hours are in a year?
  • how many minutes are in a decade?
  • how many seconds old are you?
  • how many chocolates do you hope to eat in your life?
    Warning: This part of the program could take a while to compute!

Here's a tougher question:

  • If I am 1031 million seconds old, how old am I?
so I've done the following...

using System;
using System.Web.UI.WebControls;

namespace Numbers
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnAnswerThisQuestion.Click += btnAnswerThisQuestion_Click;
btnClarification.Click += btnClarification_Click;
btnAskAnother.Click += btnAskAnother_Click;


if (!IsPostBack)
{
ddlQuestions.Items.Add(new ListItem("How many hours are in a year?", "1"));
ddlQuestions.Items.Add(new ListItem("How many minutes are in a decade?", "2"));
ddlQuestions.Items.Add(new ListItem("How many seconds old are you?", "3"));
ddlQuestions.Items.Add(new ListItem("How many chocolates do you hope to eat in your life?", "4"));
ddlQuestions.Items.Add(new ListItem("If I am 1031 million seconds old, how old am I?", "5"));
}

}

void btnAskAnother_Click(object sender, EventArgs e)
{
panelClarification.Visible = false;
tbClarificiation.Text = string.Empty;
lblAnswer.Visible = false;
btnAskAnother.Visible = false;
}

void btnClarification_Click(object sender, EventArgs e)
{
int question = int.Parse(ddlQuestions.SelectedItem.Value);

string input = tbClarificiation.Text.ToUpper();

DateTime startDate = new DateTime();
DateTime endDate = new DateTime();

switch(question)
{
case 1:

#region How Many Hours are in a year?

if(!DateTime.TryParse(input, out startDate))
{
lblAnswer.Visible = true;
lblAnswer.Text = "Entry must be in mm/dd/yyyy format.";
btnAskAnother.Visible = true;
break;
}
endDate = startDate.AddYears(1);

TimeSpan hoursInYear = TimeSinceDate(startDate, endDate);

lblAnswer.Text = (hoursInYear.TotalHours).ToString("#,#");

lblAnswer.Visible = true;
btnAskAnother.Visible = true;
break;
#endregion

case 2:

#region How Many Minutes are in a Decade?

if(!DateTime.TryParse(input, out startDate))
{
lblAnswer.Visible = true;
lblAnswer.Text = "Entry must be in mm/dd/yyyy format.";
btnAskAnother.Visible = true;
break;
}

endDate = startDate.AddYears(10);

TimeSpan minInDecade = TimeSinceDate(startDate, endDate);

lblAnswer.Text = (minInDecade.TotalMinutes).ToString("#,#");

lblAnswer.Visible = true;
btnAskAnother.Visible = true;

break;
#endregion


case 3:

#region How many seconds old am I?

if(!DateTime.TryParse(input, out startDate))
{
lblAnswer.Visible = true;
lblAnswer.Text = "Entry must be in mm/dd/yyyy format.";
btnAskAnother.Visible = true;
break;
}

endDate = DateTime.Today;
TimeSpan secondsOld = TimeSinceDate(startDate, endDate);

lblAnswer.Text = (secondsOld.TotalSeconds).ToString("#,#");

lblAnswer.Visible = true;
btnAskAnother.Visible = true;

break;

#endregion


case 4:
int degOfChocPref;
if(!int.TryParse(tbClarificiation.Text, out degOfChocPref))
{
lblAnswer.Visible = true;
lblAnswer.Text = "Entry must be a whole number between 1 and 5 (inclusive).";
btnAskAnother.Visible = true;
break;
}

switch(degOfChocPref)
{
case 1:
lblAnswer.Visible = true;
lblAnswer.Text = "You'll eat approximately 4 chocolates per year for the rest of your life.";
btnAskAnother.Visible = true;
break;
case 2:
lblAnswer.Visible = true;
lblAnswer.Text = "You'll eat approximately 12 chocolates per year for the rest of your life.";
btnAskAnother.Visible = true;
break;

case 3:
lblAnswer.Visible = true;
lblAnswer.Text = "You'll eat approximately 20 chocolates per year for the rest of your life.";
btnAskAnother.Visible = true;
break;

case 4:
lblAnswer.Visible = true;
lblAnswer.Text = "You'll eat approximately 30 chocolates per year for the rest of your life.";
btnAskAnother.Visible = true;
break;

case 5:
lblAnswer.Visible = true;
lblAnswer.Text = "You should probably contemplate not eating any more chocolate.";
btnAskAnother.Visible = true;
break;
}
break;

case 5:
break;

}
}

public TimeSpan TimeSinceDate(DateTime startDate, DateTime endDate)
{

TimeSpan span = endDate.Subtract(startDate);

return span;
}

void btnAnswerThisQuestion_Click(object sender, EventArgs e)
{
panelClarification.Visible = true;

int question = int.Parse(ddlQuestions.SelectedItem.Value);

switch (question)
{
case 1:
lblClarification.Text = "What date does the year begin? (mm/dd/yyyy)";
break;

case 2:
lblClarification.Text = "What date does the decade begin? (mm/dd/yyyy)";
break;

case 3:
lblClarification.Text = "When were you born? (mm/dd/yyyy)";
break;

case 4:
lblClarification.Text = "How much do you like chocolate on a scale of 1 to 5, with 5 being the highest?";
break;

case 5:
const int yearsOld = 1031000000/60/60/24/365;
lblAnswer.Text = yearsOld.ToString();

panelClarification.Visible = true;
lblClarification.Visible = false;
tbClarificiation.Visible = false;
lblAnswer.Visible = true;
btnAskAnother.Visible = true;
break;
}


}
}
}

And it works!

I'm ecstatically happy!

No comments:

Post a Comment