My spouse needed to figure out what 83 cm equaled in inches.  I know the formula, so I could have easily written this out by hand...but why?  I also know that I could have written this using one file...but why?  This is another example of how to separate methods into separate class files.  
Well 1 inch = 0.393700787 centimeters, so 83 * 0.393700787 = 32.677165321 inches!
[ Program.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CM2IN
{
   class Cent2Inches
   {
      static void Main(string[] args)
      {
         SetConsoleTitle.ConsoleTitle();
         GetUserInput.UserInput();
      }
   }
}
[  SetConsoleTitle.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CM2IN
{
   class SetConsoleTitle
   {
      public static void ConsoleTitle()
      {
         string strTitle = "Centimeter to Inches Calculator v1.0";  
         // Set Console Title
         Console.Title = strTitle;
      }
   }
}
[  GetUserInput.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CM2IN
{
   class GetUserInput
   {
      public static void UserInput()
      {
         string strUserInput;
         Console.Write("Enter Centimeters: ");  
         // Prompt for user input
         strUserInput = Console.ReadLine();  // Read user input
         ConvertUserInput.ConvertInput(strUserInput);  
         // Pass user input to Convert Class
      }
   }
}
[  ConvertUserInput.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CM2IN
{
   class ConvertUserInput
   {
   public static void ConvertInput(string s)
      {
         double d;
         d = Convert.ToDouble(s);  // Convert string to double
         PerformCalculation.Calculate(d);  
         // Pass double value to Calculation Class
      }
   }
}
[  PerformCalculation.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CM2IN
{
   class PerformCalculation
   {
      public static void Calculate(double i)
      {
         double cm = 0.393700787;
         double total;
         total = (i * cm);  // Perform Calculation
         DisplayResults.Results(total);  
         // Pass Results to Display results Class
      }
   }
}
[  DisplayResults.cs ]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CM2IN
{
   class DisplayResults
   {
      public static void Results(double r)
      {
         Console.WriteLine("Total Inches: " + r);  // Print results
         Console.ReadLine();
         Console.Clear();
      }
   }
}

Visual c# 2008 Centimeters to Inches Calc...
Posted by
Samson J. Loo
Monday, September 22, 2008
at
11:28 AM
Subscribe to:
Post Comments (Atom)
 
0 comments:
Post a Comment