C# Declare a new delegate type called BoolToggle Returns void Takes in a bool Cr
ID: 3828911 • Letter: C
Question
C#
Declare a new delegate type called BoolToggle
Returns void
Takes in a bool
Create a LightSwitch class that does the following:
Declares an event of type BoolToggle called OnToggle
Has a public string property called Name
Has one constructor that takes in a single string name parameter
Has a bool property called IsOn
i.Public get, private set
ii.Defaults to false
Has a Toggle() method that simply flips the value of the IsOn bool, then notifies all subscribers by firing the OnToggle event.
Override the ToString() to return a simple String with the name and current state of IsOn.
Create a Lightbulb class that does the following:
Has a public string property called Name
Has a constructor that takes in a single string name parameter
Has a ConnectSwitch method that takes in a LightSwitch instance and, if not already connected, subscribes to that switch’s OnToggle event.
Has a DisconnectSwitch method that takes in a LightSwitch instance and, if connected, unsubscribes from that switch’s OnToggle event.
If you haven’t guessed yet, the Lightbulb will need to keep track of the LightSwitches to which it is connected. A Lightbulb may only be connected to at most 2 separate LightSwitches. A Lightbulb can never be connected to a specific LightSwitch more than once.
The Lightbulb is on IF AND ONLY IF:
i.It is connected to one LightSwitch and the LightSwitch is on.
ii.It is connected to two LightSwitches where both LightSwitches have the same IsOn value.
iii.All other cases mean the Lightbulb is off.
The Lightbulb is allowed to check a switch’s IsOn status IF AND ONLY IF it is done during the connect/disconnect methods. AT NO OTHER TIME is the bulb allowed to check the switch’s IsOn state. Instead, the bulb’s IsOn state should only be impacted by the firing of OnToggle events.
Override the ToString() to return a simple String with the name and current state of the Lightbulb. It should also list the switches to which it is connected and their respective IsOn states. If a bulb is not connected to any switch, some simple indicator of that is fine. In all cases, make sure the bulb’s string is readable and user friendly.
Now, create an application that allows the user to:
Create and name new LightSwitches
Create and name new Lightbulbs
Connect/disconnect Lightbulbs and LightSwitches
Toggle specific LightSwitches
Print out the current state of all LightSwitches and Lightbulbs
Quit the application
The main menu should loop as needed until the user decides to exit the app
Explanation / Answer
//THIS CAN BE MADE MORE EASY BY USING JAVA EVENT HANDLING AND METHOD OVERRIDDING CONCEPTS COMPARED IN C#
import java.awt.*;
import java.awt.event.*;
class LightSwitch
//declaring boolean variable
{
Bool IsOn=FALSE;
}
//declaring constructor
LightSwitch(String Name){
System.out.print("parameterized constructor");
System.out.println(" Name");
}
//declaring method OnToggleget n set
public bool OnToggleget(bool IsOn,String Name)
{
System.out.println("IsOn "+IsOn);
System.out.println("Switch Name "+Name);
}
public bool OnToggleSet(bool IsOn,String Name)
{
IsOn=True;
}
}
//create event OnToggle
class LightSwitch extends Frame implements ActionListener{
TextField tf;
//Create Event
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText("OnToggle");
}
public static void main(String args[]){
new AEvent();
}
}
//Java event handling by LightBulb class
Claa LightBulb extend LightSwitch
class LightBulb extends Frame
{
//create event for sub class
TextField tf;
AEvent1(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
LightBulb o=new LightBulb(this);
b.addActionListener(o);//passing outer class instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
//override methods of parent class
public bool OnToggleget(bool IsOn,String Name)
{
System.out.println("IsOn "+IsOn);
System.out.println("Switch Name "+Name);
}
public bool OnToggleSet(bool IsOn,String Name)
{
IsOn=True;
}
public static void main(String args[]){
new AEvent2();
}
}
class LightBulb implements ActionListener{
Lightbulb obj;
AEvent1(Lightbulb obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e){
obj.tf.setText("OnToggle");
}
}
class Test2{
public static void main(String args[]){
LightSwitch s=new LightSwitch();
LightBulb i=new LightBulb();
i.OnToggleSet(ON,XXX);
i.OnToggleget(ON,YYY);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.