Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are 100 closed lockers in a school. There are 100 students outside the sch

ID: 3638244 • Letter: T

Question

There are 100 closed lockers in a school. There are 100 students outside the school. The first student goes into the school and visits every locker and opens it. The second student goes into the school and closes every locker that is a multiple of two. The third student goes into the school and visits every locker that is a multiple of three. If the locker is open he/she closes it, if the locker is closed he/she opens it. The fourth student goes into the school and visits every locker that is a multiple of four. Again if the locker is open he/she closes it, if the locker is closed he/she opens it. This continues until all 100 students have gone into the school and gone to the appropriate lockers.

Write a program that will determine and display the status of each locker after all of the students have gone into the school.

1. Output a list of all the open lockers.
2. If each student who goes into the school places a quarter into each locker he/she visits, which locker will have the most money deposited into it?
3. What is the total amount of money that will be placed into all of the lockers?
--------------------------------------------------------------------------------------------------
//This is the method/ class I have created, I need to create another class where I
//cancopy all this method and get the answers to the questions above.
class Locker
{
private boolean open;
private double money;

public Locker()
{
open = false;
money = 0.0;
}

public void visit()
{
if (open)
open = false;
else
open = true;
money +=.25;
}
public double getMoney()
{
return money;
}

public boolean isOpen()
{
return open;
}

public String toString()
{
String out = "";
if (open)
out += " open ";
else out += " closed ";
out += "with $" + money + " inside";
return out;
}
}
--------------------------------------------------------------------------
//I started some of this, need help with completion and correction:
public class lockers2
{
public static void main(String[] arg)
{

Locker[]lockers=new Locker[100];

for(int i = 1; i < lockers.length; i++)
{
for(int j = 2; j < lockers.length; j += 2)
{
lockers[i]= new Locker();
lockers[i].visit();

Explanation / Answer

I split the files into two classes to make it easier. All that is needed is nested loops. The first one starts at 0 and goes to 99 (one per student), and the second loop is each locker the student goes to. The DecimalFormat thing is just there to make the double variables look like money (ie, 1.5 = $1.50). File 1: Locker.java import java.text.DecimalFormat; class Locker { private boolean open; private double money; public Locker() { open = false; money = 0.0; } public void visit() { if (open) open = false; else open = true; money +=.25; } public double getMoney() { return money; } public String getMoneyAsFormattedString() { double money = getMoney(); DecimalFormat moneyFormat = new DecimalFormat( "$0.00"); return moneyFormat.format(money); } public boolean isOpen() { return open; } public String toString() { String out = ""; if (open) out += " open "; else out += " closed "; out += "with " + getMoneyAsFormattedString()+ " inside"; return out; } } File 2: Driver.java import java.util.*; import java.text.DecimalFormat; public class Driver { public static void main(String[] arg) { Locker[]lockers=new Locker[100]; for (int x = 0; x< 100; x++) { lockers[x] = new Locker(); } for(int i = 0; i