/* C# A Beginner's Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295 */ /* Project 2-1 Talking to Mars Call this file mars.cs */ using System; public class Mars { public static void Main() { double distance; double lightspeed; double delay; double delay_in_min; distance = 34000000; // 34,000,000 miles lightspeed = 186000; // 186,000 per second delay = distance / lightspeed; Console.WriteLine("Time delay when talking to Mars: " + delay + " seconds."); delay_in_min = delay / 60; Console.WriteLine("This is " + delay_in_min + " minutes."); } }
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
Implement the Pythagorean Theorem
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Implement the Pythagorean Theorem. using System; public class Pythagorean { public static void Main() { double s1; double s2; double hypot; string str; Console.WriteLine("Enter length of first side: "); str = Console.ReadLine(); s1 = Double.Parse(str); Console.WriteLine("Enter length of second side: "); str = Console.ReadLine(); s2 = Double.Parse(str); hypot = Math.Sqrt(s1*s1 + s2*s2); Console.WriteLine("Hypotenuse is " + hypot); } }
the differences between int and double
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* This program illustrates the differences between int and double. */ using System; public class Example3IntDouble { public static void Main() { int ivar; // this declares an int variable double dvar; // this declares a floating-point variable ivar = 100; // assign ivar the value 100 dvar = 100.0; // assign dvar the value 100.0 Console.WriteLine("Original value of ivar: " + ivar); Console.WriteLine("Original value of dvar: " + dvar); Console.WriteLine(); // print a blank line // now, divide both by 3 ivar = ivar / 3; dvar = dvar / 3.0; Console.WriteLine("ivar after division: " + ivar); Console.WriteLine("dvar after division: " + dvar); } }
Compute the area of a circle
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Compute the area of a circle. using System; public class ComputeCircle { static void Main() { double radius; double area; radius = 10.0; area = radius * radius * 3.1416; Console.WriteLine("Area is " + area); } }
explicit conversions from decimal to int
using System; class DecimalToU_Int32Demo { const string formatter = "{0,17}{1,19}{2,19}"; public static string GetExceptionType( Exception ex ) { string exceptionType = ex.GetType( ).ToString( ); return exceptionType.Substring(exceptionType.LastIndexOf( '.' ) + 1 ); } public static void DecimalToU_Int32( decimal argument ) { object Int32Value; object UInt32Value; try { Int32Value = (int)argument; } catch( Exception ex ) { Int32Value = GetExceptionType( ex ); } try { UInt32Value = (uint)argument; } catch( Exception ex ) { UInt32Value = GetExceptionType( ex ); } Console.WriteLine( formatter, argument, Int32Value, UInt32Value ); } public static void Main( ) { DecimalToU_Int32( 123M ); } }
decimal property
using System; class MainEntryPoint { static void Main(string[] args) { Money cash1 = new Money(); cash1.Amount = 40M; Console.WriteLine("cash1.ToString() returns: " + cash1.ToString()); Console.ReadLine(); } } class Money { private decimal amount; public decimal Amount { get { return amount; } set { amount = value; } } public override string ToString() { return "$" + Amount.ToString(); } }
Manually create a decimal number
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Manually create a decimal number. using System; public class CreateDec { public static void Main() { decimal d = new decimal(12345, 0, 0, false, 2); Console.WriteLine(d); } }