subject

A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open, and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100. After all the students have passed through the building and changed the lockers, which lockers are open? Write a program to find your answer and * * display all open locker numbers separated by exactly one space. *
* (Hint: Use an array of 100 Boolean elements, each of which indicates whether a *
* locker is open (true) or closed (false). Initially, all lockers are closed.) *
/
public class Exercise_07_23 {
/** Main method */
public static void main(String[] args) {
String[] lockers = new String[100];
// Close all the lockers
closeAllLockers(lockers);
// Invoke the stuentLockerChanges method
studentLockerChanges(lockers);
// Display all open lock numbers
print(lockers);
}
/** isOpen returns true if l is the string "OPEN". False otherwise*/
public static boolean isOpen(String l) {
return l == "OPEN";
}
/** closeAllLockers fills the array with the string "CLOSED" */
public static void closeAllLockers(String[] lockers) {
for (int i = 0; i < lockers. length; i++) {
lockers[i] = "CLOSED";
}
}
/** studentLockerChanges changes the string in each
* element from "CLOSED" to "OPEN" or Vice versa */
public static void studentLockerChanges(String[] lockers) {
int start = 0; // Locker student begins with
for (int s = 1; s <= lockers. length; s++) {
for (int l = 0; l < lockers. length; l += s) {
if (isOpen(lockers[l]))
lockers[l] = "CLOSED";
else
lockers[l] = "OPEN";
}
start++;
}
}
/** print displays all open locker numbers separated by exactly one space */
public static void print(String[] lockers) {
for (int i = 0; i < lockers. length; i++) {
if (isOpen(lockers[i])) {
System. out. print("L" + (i + 1) + " ");
}
}
System. out. println();
}
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 13:10
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
question
Computers and Technology, 23.06.2019 09:30
Given a link with a maximum transmission rate of 32.8 mbps. only two computers, x and y, wish to transmit starting at time t = 0 seconds. computer x sends filex (4 mib) and computer y sends filey (244 kib), both starting at time t = 0. statistical multiplexing is used, with details as follows packet payload size = 1000 bytes packet header size = 24 bytes (overhead) ignore processing and queueing delays assume partial packets (packets consisting of less than 1000 bytes of data) are padded so that they are the same size as full packets. assume continuous alternating-packet transmission. computer x gets the transmission medium first. at what time (t = ? ) would filey finish transmitting? give answer in milliseconds, without units, and round to one decimal places (e.g. for an answer of 0.013777 seconds you would enter "13.8" without the quotes)
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
List 3 items that were on kens resume that should have been excluded
Answers: 1
question
Computers and Technology, 24.06.2019 02:00
Which steps will open the system so that you can enter a question and do a search for
Answers: 1
You know the right answer?
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the...
Questions
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
question
Mathematics, 15.09.2020 21:01
Questions on the website: 13722363