Presentation is loading. Please wait.

Presentation is loading. Please wait.

Write a program to calculate yearly depreciation of the value of an item is given by Dep = (purchase price - salvage value ) / years of service.

Similar presentations


Presentation on theme: "Write a program to calculate yearly depreciation of the value of an item is given by Dep = (purchase price - salvage value ) / years of service."— Presentation transcript:

1 Write a program to calculate yearly depreciation of the value of an item is given by Dep = (purchase price - salvage value ) / years of service

2

3 …………………………

4 DateTime

5 using System; class Unary { public static void Main() { //System.DateTime moment = new System.DateTime(1999, 1, 13, 3, 57, 32, 11); //Console.WriteLine(System.DateTime.Now); System.DateTime moment = System.DateTime.Now; // Year gets int year = moment.Year; // Month gets 1 (January). int month = moment.Month; // Day gets 13. int day = moment.Day; // Hour gets 3. int hour = moment.Hour; // Minute gets 57. int minute = moment.Minute; // Second gets 32. int second = moment.Second; // Millisecond gets 11. int millisecond = moment.Millisecond; // DateTime.Parse(" "); Console.WriteLine(day + " - " + month + " - " + year); }}

6 Ask the user for their birthday. It will probably easiest to ask year, then month, then day rather than parsing a combined string reliably. Calculate the age of the user. Check to see if the age of the user is impossible. For example, if the user is not yet born, output an error message. If the user claims to be 135 years old, then let them know that's not possible. Output the age of the user to the console. If it is the user's birthday, output a nice message. Compute the user's astrological sign according to both the Western (sun sign) and Chinese astrological systems. If you are not familiar with these astrological systems, look them up on the web. Output the computed signs to the console. Optionally output additional information (e.g. horoscope of the day) about the user based on their sign.

7 String handling

8 namespace ConsoleApplication4 { class Program { static void Main(string[] args) { string str1 = "hi how r u?"; string str2 = "hi how r u"; string strup, strLow; int res, idx; Console.WriteLine(str1); Console.WriteLine("len == " + str1.Length); strLow = str1.ToLower(); strup = str1.ToUpper(); Console.WriteLine("lower == " + strLow); Console.WriteLine("upper == " + strup); Console.WriteLine("one character at time"); for (int i = 0; i < str2.Length; i++) Console.Write(str2[i]); Console.WriteLine("\n");

9 if (str1 == str2) Console.WriteLine("equals"); res = string.Compare(str1, str2); if (res == 0) Console.WriteLine("equals"); else if (res < 0) Console.WriteLine("str1 > str2"); else if (res > 0) Console.WriteLine("str2 > str11");

10 str2 = "one two three four one"; //char idx = str2.IndexOf("t"); Console.WriteLine("first idx" + idx); idx = str2.LastIndexOf("o"); Console.WriteLine("last idx" + idx); //string idx = str2.IndexOf("two",StringComparison.Ordinal); Console.WriteLine("first idx" + idx); idx = str2.LastIndexOf("one",StringComparison.Ordinal); Console.WriteLine("last idx" + idx);

11 1.The string class contains several methods that operates on string. 2.Some methods takes parameter of type String.Comparision. 3.This is enumeration type that defines how comparison involving string is conducted. Strings can be compared by two ways based on binary values called ordinal comparison - StringComparision.Ordinal dictionary order - culture sensitive - StringComparision.CurrentCulture

12 //concatenation str3 = str1 + str2; Console.WriteLine(str3); //array of string string[] str = {"this", "is", "a", "test."}; str[1] = "was"; str[3] = "test, too !"; for (int i = 0; i < str.Length; i++) Console.WriteLine(str[i] + "\n"); }

13 Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection. string b = "h" b = b + "ello"

14 String is immutable i.e. Strings cannot be altered. When you alter a string (by adding to it for example), you are actually creating a new string. But StringBuilder is not immutable (Mutable) so if u have to alter a string many times, such as mutliple concatenations then use StringBuilder.

15 C# offers a class called StringBuilder which is in System.Text namespace. StringBuilder s;

16 write a program for display an integer value using words eg 19 - one nine

17 Handling arrays

18 int n = arrayname.Length;

19 using System; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int[] numbers = { 4, 3, 8, 0, 5 }; Array.Sort(numbers); foreach(int i in numbers) Console.WriteLine(i); Console.ReadLine(); }

20 Multidimensional arrays come in two flavors with C#: Rectangular arrays and jagged arrays. The difference is that with rectangular arrays, all the dimensions have to be the same size, hence the name rectangular. A jagged array can have dimensions of various sizes.


Download ppt "Write a program to calculate yearly depreciation of the value of an item is given by Dep = (purchase price - salvage value ) / years of service."

Similar presentations


Ads by Google