C# Cognizant Handson Solutions

OpenableInterface Handson Solution Cognizant

During the birthday party of Brian, The children were asked to pick a folded sheet of paper with handwritten fortunes inside. If the paper is marked with a letter T, a toy treasure box is gifted, and if it contains a letter P, a toy parachute is gifted to the children.

Simulate the scenario using C# classes and interface.

Create an interface named IOpenable. It should contain a single method named OpenSesame with the following signature.

Interface IOpenable

Member typeIdentifier NameDescription
MethodString OpenSesame() The method contains zero parameter list and returns a string

Create classes named TreasureBox, and Parachute that  implements IOpenable. 

class TreasureBox  // should implement IOpenable

Member typeIdentifier NameDescription
MethodString OpenSesame() method should  return a string  “Congratulations , Here is your lucky win”. (return the  string exactly as specified .)

class  Parachute // should implement IOpenable

Member typeIdentifier NameDescription
MethodString OpenSesame() method should return a string  “Have a thrilling experience flying in air”(return the  string exactly as specified .)

class Program  // class for the Main method

Member typeIdentifier NameDescription
MethodMain Create instances for Parachute and TreasureBox and  call the OpenSesame method to display the fortunes.

Write a program that declares an object for each of the implementing classes and calls its OpenSesame() method . Display the String to the console.

Note:

Don’t create any new namespace.

Create classes with public access specifier.

The Main method should be defined in public class Program.

Declare the interface as public


Sample Input

Enter the letter found in the paper

T

Sample Output:

Congratulations, Here is your lucky win 

Sample Input

Enter the letter found in the paper

P

Sample Output:Have a thrilling experience flying in air    

OpenableInterface.cs

using System;

public interface IOpenable
{
    String OpenSesame();
}

public class TreasureBox : IOpenable
{
    public String OpenSesame()
    {
        return "Congratulations , Here is your lucky win";
    }
}

public class Parachute : IOpenable
{
    public String OpenSesame()
    {
        return "Have a thrilling experience flying in air";
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        TreasureBox t = new TreasureBox();
        Parachute p = new Parachute();

        Console.WriteLine("Enter the letter found in the paper");
        char ch = Console.ReadLine()[0];

        if (ch == 'T')
            Console.WriteLine(t.OpenSesame());
        else if (ch == 'P')
            Console.WriteLine(p.OpenSesame());
    }
}

Similar Posts