Computers and Technology

Light Switches Consider the following logic puzzle I have a board of light switches, numbered 0,1.2...1023 Each light switch can be switched on or off. All switches are initially off. Step 1: All of the switches are flipped starting at 0. At this point, all of the light switches are on. Step 2 Every second switch is flipped, starting at 0. At this point, lights 0,2.4.6,8... are off. Lights 1,3,5,7,9 are still on. Step 3: Every third switch is flipped, starting at 0. So switches 0.3,6,9,12,... are flipped So if a switch is on, it is flipped off. And if a switch is off, it is flipped on Step 1023 Every 1023 rd switch is flipped, starting at 0 So switches 0 and 1023 are flipped. The question is: "After step 1023, which switches are on and which are off?" To answer this question, you will be using object oriented programming though tech- nically you could solve it using a "trick (as) Light Switch Create a class called LaightSwitch with following properties. . When a LightSwitch is created, it's default state should be attable using an argument to the constructor. If the input argument is "on", it should start off us on, if the input argument is "off" it should start of as "off". • It should be posible to turn the switch on using a method called turno. • It should be posible to turn off the switching a method called turn off • There should also be a flip method that flips the current the current state of the Light Switch (on to off or off to on) • If a switch is printed, it should print "I am on" or "I am off' depending on the the current state (they are smart switches). Write a str. method to accomplish this. • You shouldn't store the state of the switch using a string, use a 'bool' attribute instead. (b) SwitchBoard Build a SwitchBoard class with the following properties: • When a switchboard is created, it should be possible to specify the number of switches it contains. You should represent a SwitchBoard as a list of Light Switches. • All switches should start in the "off" position. . If a switchboard is printed, it should print like the following: " The following switches are on: 0 2 4 6 8". It is fine for everything to be printed on the same line. Write a str , method to accomplish this. • There should be a method called which switch, that should return a list of integers representing which switches are on in order (e. g. (1,3,5,7,9]). • There should a method called flip, which takes a single integer argument, n and flips the state of the n'th light switch. • There should be a flip_every method that takes one argument, n and flips the state of every n'th switch, starting at 0. So flip. every(2) would flip switches 0, 2, 4, 6, etc. • There should be a method reset(), which should turn off all the sitches in the board. . If flip is called with an input argument outside the bounds of the list (e. g. 100 is passed in when there are only 16 switches), then your program should not crash, instead an message you printed to user stating the switch specified does not exist Finally, create an instance of the Switch Board class in your main program to solve the original question of which light switches will still be on after step 1023. Print the SwitchBoard at the end.

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 11:00, lexhorton2002
The great length of north america causes the climate to be varied. true false
Answers: 2
image
Computers and Technology, 22.06.2019 13:10, LuckyCharms988
Calculating the "total price" of an item is tedious, so implement a get_item_cost method that just returns the quantity times the price for an item. by the way, the technical term for this kind of instance method is an accessor method, but you'll hear developers calling them getters because they always start with "get" and they get some value from instance attributes. in order to make the items sortable by their total total price, we need to customize our class. search the lectures slides for "magic" to see how to do this. see section 9.8 for an additional reference. the receipt class: this will be the class that defines our receipt type. obviously, a receipt will consist of the items on the receipt. this is called the composition design pattern. and it is very powerful. instance attributes: customer_name : it is very important to always know everything you can about your customers for "analytics", so you will keep track of a string customer name in objects of type receipt. date : the legal team has required that you keep track of the dates that purchases happen for "legal reasons", so you will also keep track of the string date in objects of type receipt. cart_items : this will be a list of the items in the cart and hence end up on the receipt. methods: 1. create a default constructor that can take a customer name as an argument, but if it gets no customer name, it will just put "real human" for the customer_name attribute. it should also accept a date argument, but will just use the value "today" for the date instance attribute if no date is given. the parameters should be named the same as the instance attributes to keep things simple. 2. add_item : self-descriptive. takes a parameter which we hope beyond hope is of type itemtopurchase and adds it to the cart_items. returns none. 3. print_receipt : takes a single parameter isevil, with default value true. returns a total cost of all the items on the receipt (remember to factor in the quantity). prints the receipt based on the following specification: for example, if isevil is true, and customer_name and date are the default values: welcome to evilmart, real human today have an evil day! otherwise, it should print: welcome to goodgo, real human today have an good day! then the receipt should be printed in sorted order like we discussed earlier, but whether or not it starts with the highest cost (think reverse), depends on the value of isevil. if it is evil, then the lowest cost items should print first, but if it is good, then it will print the highest cost items first. (cost meaning price*quantity). remember to return the total cost regardless! your main() function: the main flow of control of your program should go in a main() function or the program will fail all the unit tests. get the name of the customer with the prompt: enter customer name: get the date with the prompt: enter today's date then, ask the question: are you evil? your program should consider the following as true: yeah yup let's face it: yes hint: what do these strings all have in common? your program should consider all the following as false: no nah perhaps but i'm leaning no (just be glad you don't have to handle "yeah no.") okay enough horsing around. (get it? aggies? ! horsing! ) next, in the main() function, you will have to create a receipt object and start adding things into it using an input-while loop. the loop will prompt the user for the item name exactly as in the previous zylab (9.11). but unlike the previous zylab, the loop will terminate only if an empty string is entered for the item name. then, the price and the quantity will be prompted for exactly as in the previous zylab. create the itemtopurchase objects in the same manner as the previous zylab, but don't forget to add them to the receipt using your add_item instance method. then, the items on the receipt should be printed with the same formatting as in the previous zylab, of course with either "good" or "evil" ordering. however, on the last line, the total should be printed as follows: where 10 is replaced by the actual total. sample run here is what a sample run of the final program should look like: enter customer name: nate enter today's date: 12/20/2019 are you evil? bwahahahaha yes enter the item name: bottled student tears enter the item price: 2 enter the item quantity: 299 enter the item name: salt enter the item price: 2 enter the item quantity: 1 enter the item name: welcome to evilmart, nate 12/20/2019 have an evil day! salt 1 @ $2 = $2 bottled student tears 299 @ $2 = $598 total: $600
Answers: 1
image
Computers and Technology, 23.06.2019 05:30, savyblue1724707
Sally is editing her science report about living things. she needs to copy a paragraph from her original report. order the steps sally needs to do to copy the text to her new document.
Answers: 1
image
Computers and Technology, 23.06.2019 09:30, jason9394
Facial expressions and gestures are examples of messages.
Answers: 3
Do you know the correct answer?
Light Switches Consider the following logic puzzle I have a board of light switches, numbered 0,1.2....

Questions in other subjects:

Konu
Mathematics, 03.05.2021 23:30
Konu
English, 03.05.2021 23:30
Konu
Chemistry, 03.05.2021 23:30