Answer to Question #189411 in C# for CHANDRASENA REDDY

Question #189411

Sameer has written a code to create the Hash Table. The code is given below.

class Program

{

static void Main()

{

}

static Hashtable GetHashtable()

{

// Create and return new Hashtable.

Hashtable hashtable = new Hashtable();

hashtable.Add("Area", 1000);

hashtable.Add("Perimeter", 55);

hashtable.Add("Mortgage", 540);

return hashtable;

}

}

You need to perform some tasks on this code. Write the functionality in the main method.

Task 1: See if the Hashtable contains the key “Perimeter”.

Task 2: Print the value of “Area” with indexer.

Task 3: Remove the entry for “Mortgage”


1
Expert's answer
2021-05-13T23:24:53-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;


namespace C_SHARP
{
    class Program
    {


        static void Main()
        {
            Hashtable hashtable = GetHashtable();
            //Task 1: See if the Hashtable contains the key "Perimeter".
            if (hashtable.ContainsKey("Perimeter"))
            {
                Console.WriteLine("The Hashtable contains the key \"Perimeter\"");
            }
            else {
                Console.WriteLine("The Hashtable does not contain the key \"Perimeter\"");
            }


            //Task 2: Print the value of "Area" with indexer.
            int index = 0;
            int indexArea=-1;
            foreach (string key in hashtable.Keys)
            {
                if (hashtable.ContainsKey("Area"))
                {
                    indexArea = index;
                }
                index++;
            }
            if (indexArea!=-1)
            {
                Console.WriteLine("The Hashtable with the key \"Area\" has index {0}", indexArea);
            }
            else
            {
                Console.WriteLine("The Hashtable does not contain the key \"Area\"");
            }


            Console.WriteLine();
            foreach (string key in hashtable.Keys)
            {
                Console.WriteLine(string.Format("{0,-20} {1,-20}", key, hashtable[key]));
            }
            //Task 3: Remove the entry for "Mortgage"
            Console.WriteLine();
            if (hashtable.ContainsKey("Mortgage"))
            {
                hashtable.Remove("Mortgage");
                Console.WriteLine("The entry for \"Mortgage\" has been deleted.");
            }
            else
            {
                Console.WriteLine("The Hashtable does not contain the key \"Mortgage\"");
            }
            Console.WriteLine();
            foreach (string key in hashtable.Keys)
            {
                Console.WriteLine(string.Format("{0,-20} {1,-20}", key, hashtable[key]));
            }


            Console.ReadLine();
        }


        static Hashtable GetHashtable()
        {


            // Create and return new Hashtable.


            Hashtable hashtable = new Hashtable();
            hashtable.Add("Area", 1000);
            hashtable.Add("Perimeter", 55);
            hashtable.Add("Mortgage", 540);


            return hashtable;


        }


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS