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

A Power Usage Simulation System needs to be able to manage a collection of appli

ID: 3624734 • Letter: A

Question

A Power Usage Simulation System needs to be able to manage a collection of appliances
(ApplianceSet). Along with standard constructors and toString method, the collection class should have
methods to:
add an appliance to the collection
find an appliance by its unique system generated ID
delete an appliance by its unique system generated ID
Use the following test program to test your collection class (add more testing to it). Either match the
method signatures shown here in your ApplianceSet, or update the teest program to match yours.
The appliance IDs are randomly generated. THE PROBLEMS HERE FOR ME ARE THE ADD APPLIANCE AND DELETE APPLIANCE METHODS. pls revise my find Appliance method if need be. The provided Testclass is below

public class TestApplianceSet {
public static void main(String[] args) {
ApplianceSet collection = new ApplianceSet(5);
Appliance a1 = new Appliance(12341234, "washer", 100, 0, .005);
if (collection.addAppliance(a1)) System.out.println("a1 added");
else System.out.println("a1 add failed");

Appliance a2 = new Appliance(12341234, "dryer", 200, 0, .01);
if (collection.addAppliance(a2)) System.out.println("a2 added");
else System.out.println("a2 add failed");

Appliance a3 = new Appliance(12341234, "TV", 25, 5, .25);
if (collection.addAppliance(a3)) System.out.println("a3 added");
else System.out.println("a3 add failed");

// this add should fail
Appliance aNull=null;
if (collection.addAppliance(aNull)) System.out.println("aNull added");
else System.out.println("aNull add failed");

Appliance a4 = new Appliance(11111111, "computer", 50, 40, 1.0);
if (collection.addAppliance(a4)) System.out.println("a4 added");
else System.out.println("a4 add failed");

Appliance a5 = new Appliance(11111111, "lights", 50, 0, .5);
if (collection.addAppliance(a5)) System.out.println("a5 added");
else System.out.println("a5 add failed");

// this add should fail
Appliance a6 = new Appliance(11111111, "dryer", 200, 0, .01);
if (collection.addAppliance(a6)) System.out.println("a6 added");
else System.out.println("a6 add failed");

System.out.println(collection);

System.out.println(" Find appliance 3: "+collection.findAppliance(3));

System.out.println(" Find appliance not there: "+collection.findAppliance(10));

if (collection.deleteAppliance(3)) System.out.println("a3 deleted");
else System.out.println("a3 delete failed");

// this delete should fail
if (collection.deleteAppliance(3)) System.out.println("a3 deleted");
else System.out.println("a3 delete failed");

System.out.println(" "+collection);

// add more testing of your own
}
}

MY appliance Class is below


public class Appliance
{
private static int counter = 1;
private int appID;
private long location;
private String appName;
private int onWatts;
private int offWatts;
private double probOn;
private Appliance [] apps;
private int numItems;

public Appliance(long l, String n, int w1, int w2, double prob)
{
appID = counter;
setLocation(1);
setName(n);
setOnWatts(w1);
setOffWatts(w2);
setProbOn(prob);
counter++;
apps = new Appliance[counter];
}

public int getAppID()
{
return appID;
}

public void setAppID(int id)
{
appID = id;
}
public long getLocation(){return location;}

public void setLocation(long newLocation)
{
if (newLocation >= 10000000 && newLocation<= 99999999)
location = newLocation;
else
location = 0;
}

public String getName()
{
return appName;
}

public void setName(String newName)
{
appName = newName;
}

public int getOnWatts()
{
return onWatts;
}

public void setOnWatts(int newOnWatts)
{
if(newOnWatts > 0 && newOnWatts>= offWatts)
> else
> }

public int getOffWatts()
{
return offWatts;
}

public void setOffWatts(int newOffWatts)
{
if(newOffWatts >= 0 && newOffWatts<= onWatts)
offWatts = newOffWatts;
else
offWatts = 0;
}
public double getProbOn()
{
return probOn;
}
public void setProbOn(double newProbOn)
{
if (newProbOn > 0 && newProbOn <= 1)
probOn = newProbOn;
else
probOn =0;
}

public String toString()
{
return getAppID()+ " Loc="+ getLocation()+" Name="+ getName()+ " OffWatts="+ getOffWatts()+ "ProbOn= " + getProbOn();
}

public void findAppliance(int newAppID)
{
for (int i = 0; i < apps.length; i++)
if (appID == newAppID)
{
System.out.println("The appliance being searched is present");
}
else
System.out.println("The appliance is not currently in the collection");
}

public Appliance deleteAppliance(int appID)
{
boolean delete = true;
Appliance temp = apps[counter -1];
int presentItems = 0;
//here what I was trying was to say if it is an empty array do this
if (apps == null)
System.out.println("There is nothing to be deleted here, this will not work");
delete = false;
//here I tried making a temp array with no length, then decrementing the number of items to remove the passed in appliance
else
{
apps[counter]= new apps[];
numItems--;
delete = true; //just showing that the delete was successful
}
return temp;
}

public boolean isFull() //method for the add method checking if the array is filled with appliances already of if there is space
{
return (numItems== apps.length);
}

public Appliance addAppliance(int appID)
{
boolean add = true;
Appliance temp = apps[counter+1];
int presentItems = 0;
if (apps == null || isFull())
{
System.out.println("This appliance cannot be added");
add = false;
}
//here I made a new temp array and i increased the number of items by 1 to make space for the new Appliance to be passed in
else
{
apps[counter] = new apps[];
numItems++;
if (numItems == 1)
presentItems= 0;
add= true;
}
return temp;
}

}

}
Thanks in advance

Explanation / Answer

import java.util.ArrayList; import java.util.Scanner; import java.io.*; public class ApplianceSet { private ArrayList apps; private int numItems; private String fileName; public ApplianceSet() //default { apps = new ArrayList(); } public ApplianceSet(int numItems) //default { apps = new ArrayList(numItems); } public int getArraySize() { return numItems; } public void setArraySize(int newNumItems) { if (newNumItems > 0) numItems = newNumItems; } public boolean isFull() { return (numItems == apps.size()); } public int findAppliance(int newAppID) { int found = 0; for (int i = 0; i