SQL SAMSON

everything sql

Archive for the ‘cm to in conversion’ Category

Java Centimeters to Inches Calc…

with one comment

As before here is the Java version of the Centimeters to Inches calculator.

[ Main.java ]
public class Main
{
   public static void main(String[] args)
   {
      GetUserInput.GetInput();
   }
}

[ GetUserInput.java ]
import java.util.*;
public class GetUserInput
{
  &nbspstatic void GetInput()
   {
      String strUserInput;
      Scanner in = new Scanner(System.in);
      System.out.print(“Enter Centimeters: “);
      strUserInput = in.nextLine();
      in.close();
      ConvertUserInput.ConvertInput(strUserInput);
   }
}

[ ConvertUserInput.java ]
public class ConvertUserInput
{
   static void ConvertInput(String s)
   {
      double d;
      d = Double.parseDouble(s);
      PerformCalculation.Calculate(d);
   }
}

[ PerformCalculation.java ]
public class PerformCalculation
{
   static void Calculate(double i)
   {
      double cm = 0.393700787;
      double total;
      total = (i * cm);
      DisplayResults.Results(total);
   }
}

[ DisplayResults.java ]
public class DisplayResults
{
   static void Results(double r)
   {
      System.out.println(“total Inches: “ + r);
   }
}

Written by Samson Loo

September 22, 2008 at 9:18 pm

Visual c# 2008 Centimeters to Inches Calc…

leave a comment »

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();
      }
   }
}

Written by Samson Loo

September 22, 2008 at 6:28 pm