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

Below is a class for a program im supposed to be constructing. How it works is I

ID: 653233 • Letter: B

Question

Below is a class for a program im supposed to be constructing. How it works is I was given the code shell for the project and each class contains comments which tell you how to construct each class. Im not even sure if the code that I have so far is right but im stuck on the whole public void add(Device dev) section I have no clue how to finish coding this class, any help would be greatly appreciated!

package supportcenter;

/**
* This class will be the data structure used in this program.
*
* It uses an array of Devices as the storage, and uses numDevices
* to track how many Devices have been entered into the array.
* This allows only the devices to be iterated through rather than the
* entire array.
*
* @author Your Name
* @version 0.0
*/

public class DeviceList {

   private Device[] array;
   private int numDevices;

   /**
   * No-arg constructor.
   * Set fields to defaults.
   * array should be of size 50.
   *
* @param Device
* @param d
   */
   public DeviceList(Device[] Device, int d)
   {
array = Device;
numDevices = d;
   }
   /**
   * args Constructor.
   * set fields equal to parameters.
   *
   * @param array - previously created array.
   * @param numDevices - number of devices in array.
   */
   public DeviceList(Device[] array, int numDevices)
   {
this.array = array;
this.numDevices = numDevices;

   }

   /**
   * Accessor for array. Needed in SupportCenter.
   *
   * @return Device[]
   */
   public Device[] getArray()
   {
       return array;
   }

   /**
   * Add a new device from a device that is already made.
   * Check to see if the new device already exists in the array,
   * if not, add it to the end and increment numDevices.
   *
   * @param dev
   */
   public void add(Device dev)
   {

  
   }

   /**
   * Removes a device that is equal to dev.
   * Uses its own lookup to check if dev is equal to anything in the array.
   * if so, it removes it in the same way as the other removes
   * Check to see if there are any devices in the list first.
   *
   * @param dev
   */
   public void remove(Device dev)
   {

   }

   /**
   * Removes a device with the same device deviceNum
   * Looks up the device based off deviceNum provided, if not found it prints an error
   * if it is not the last device in the list, it is replaced by the last device in the list
   * if it is the last device, it makes that index null in the array
   * it decrements numDevices if the device is found.
   *
   * @param deviceNum
   */
   public void remove(int deviceNum)
   {

   }

   /**
   * Removes a device with the same name or phone.
   * Looks up the device based off of info provided, if not found it prints an error
   * if it is not the last device in the list, it is replaced by the last device in the list
   * if it is the last device, it makes that index null in the array
   * it decrements numDevices if the device is found.
   *
   * @param info - name or phone number.
   */
   public void remove(String info)
   {

   }

   /**
   * Returns a device with a given deviceNum.
   * This should search the array and return the index of the last device with
   * the given deviceNum.
   *
   * @param deviceNum
   * @return Device
   * @throws Exception
   */
   public int lookup(int deviceNum)
   {
       return 0;
   }

   /**
   * Returns a device with a given phone number or name.
   * This should search the array and return the index of the last device with
   * the given ownerName or phoneNum.
   *
   * @param info
   * @return Device
   * @throws Exception
   */
   public int lookup(String info)
   {
       return 0;
   }

   /**
   * toString for DeviceList.
   * This should display the return a string made up of the strings from each Device in the array.
   *
   * @return String
   */
   public String toString()
   {
       return "";
   }
}

Explanation / Answer

//Device.java
//As Device class not provided,
//it is assumed to have device id and name
package supportcenter;
public class Device
{
   private String deviceName;
   private String phoneNumber;
   private int deviceId;
  
   public Device()
   {
       deviceName="";
       phoneNumber="";
      
       deviceId=0;
   }
  
   public Device(int deviceId,String deviceName,String phoneNumber)
   {
       this.deviceId=deviceId;
       this.deviceName=deviceName;      
       this.phoneNumber=phoneNumber;
   }
  
   public int getID()
   {
       return deviceId;      
   }
  
   public String getName()
   {
       return deviceName;      
   }
  
  
   public String getPhoneNumber()
   {
       return phoneNumber;
   }
  
   @Override
   public String toString() {
      
       return "ID: "+deviceId+" Name: "+deviceName+" Phone Number: "+phoneNumber;
   }
  
}

----------------------------------------------------------------------------

//DeviceList.java
package supportcenter;

/**
* This class will be the data structure used in this program.
*
* It uses an array of Devices as the storage, and uses numDevices
* to track how many Devices have been entered into the array.
* This allows only the devices to be iterated through rather than the
* entire array.
*
* @author Your Name
* @version 0.0
*/

public class DeviceList
{

   private Device[] array;
   private int numDevices;

   /**
   * No-arg constructor.
   * Set fields to defaults.
   * array should be of size 50.
   *
   * @param Device
   * @param d
   */
   //Default constructor that sets the numDevices and intiailiz
   //the array of default size
   public DeviceList()
   {      
       numDevices = 50;
       array = new Device[numDevices];
   }
   /**
   * args Constructor.
   * set fields equal to parameters.
   *
   * @param array - previously created array.
   * @param numDevices - number of devices in array.
   */
   //Parameterized constructor that sets the numDevices and intiailiz
       //the array of given size
   public DeviceList(Device[] devList, int numDevices)
   {
       this.numDevices = numDevices;
       array = new Device[numDevices];

       for (int index = 0; index < numDevices; index++)       
           array[index]=devList[index];  
   }

   /**
   * Accessor for array. Needed in SupportCenter.
   *
   * @return Device[]
   */
   public Device[] getArray()
   {
       return array;
   }

   /**
   * Add a new device from a device that is already made.
   * Check to see if the new device already exists in the array,
   * if not, add it to the end and increment numDevices.
   *
   * @param dev
   */
   public void add(Device dev)
   {
       if(lookup(dev.getID())==null &&
               lookup(dev.getName())==null)
       {
           Device[] tempList=new Device[array.length];

           tempList=array;
           numDevices=numDevices+1;
           array=new Device[numDevices];

           for (int index = 0; index < tempList.length; index++)
           {
               array[index]=tempList[index];  
           }

           array[numDevices-1]=dev;

       }
   }

   /**
   * Removes a device that is equal to dev.
   * Uses its own lookup to check if dev is equal to anything in the array.
   * if so, it removes it in the same way as the other removes
   * Check to see if there are any devices in the list first.
   *
   * @param dev
   */
   public void remove(Device dev)
   {
       boolean found=false;

       for (int index = 0; index < numDevices && !found; index++)
       {
           if(dev.getID()==array[index].getID() &&
                   dev.getName().equalsIgnoreCase(array[index].getName()) &&
                   dev.getPhoneNumber().equalsIgnoreCase(array[index].getPhoneNumber()))      
           {
               found=true;
               array[index]=null;
           }
       }

       if(!found)
           System.out.println("Device not found");

   }

   /**
   * Removes a device with the same device deviceNum
   * Looks up the device based off deviceNum provided, if not found it prints an error
   * if it is not the last device in the list, it is replaced by the last device in the list
   * if it is the last device, it makes that index null in the array
   * it decrements numDevices if the device is found.
   *
   * @param deviceNum
   */
   public void remove(int deviceNum)
   {
       boolean found=false;

       for (int index = 0; index < numDevices && !found; index++)
       {
           if(array[index].getID()==deviceNum)
           {
               found=true;
               array[index]=null;
           }
       }

       if(!found)
           System.out.println("Device not found");

   }

   /**
   * Removes a device with the same name or phone.
   * Looks up the device based off of info provided, if not found it prints an error
   * if it is not the last device in the list, it is replaced by the last device in the list
   * if it is the last device, it makes that index null in the array
   * it decrements numDevices if the device is found.
   *
   * @param info - name or phone number.
   */
   public void remove(String info)
   {
       boolean found=false;

       for (int index = 0; index < numDevices && !found; index++)
       {
           if(array[index].getName().equalsIgnoreCase(info)||
                   array[index].getPhoneNumber().equals(info))
           {
               found=true;
               array[index]=null;
           }
       }

       if(!found)
           System.out.println("Device not found");

   }

   /**
   * Returns a device with a given deviceNum.
   * This should search the array and return the index of the last device with
   * the given deviceNum.
   *
   * @param deviceNum
   * @return Device
   * @throws Exception
   */
   public Device lookup(int deviceNum)
   {
       if(deviceNum>0 && deviceNum<numDevices)
           return array[deviceNum];
       else
           return null;
   }

   /**
   * Returns a device with a given phone number or name.
   * This should search the array and return the index of the last device with
   * the given ownerName or phoneNum.
   *
   * @param info
   * @return Device
   * @throws Exception
   */
   public Device lookup(String info)
   {
       for (int index = 0; index < numDevices; index++)
       {
           if(array[index]!=null)
               if(array[index].getName().equalsIgnoreCase(info)|| array[index].getPhoneNumber().equals(info))
                   return array[index];
       }

       return null;

   }

   /**
   * toString for DeviceList.
   * This should display the return a string made up of the strings from each Device in the array.
   *
   * @return String
   */
   public String toString()
   {
       String deviceList="";
       int index;
       for (index = 0; index < numDevices; index++)
       {
           if(array[index]==null)
               deviceList+="Null ";
           else
               deviceList+=array[index].toString()+" ";

       }

       return deviceList;
   }
}
------------------------------------------------


//Test program that createa a Device array list
//and creates a constructor of DeviceList with
//array and of size 4.
//Calling the metod remove that removes the
//first device at index 0 location
//and add a device at to the last of the devicelist
//and print the list of devices
//DeviceDriver.java
package supportcenter;
public class DeviceDriver
{
   public static void main(String[] args)
   {
      
       //array of Device objects
       Device devices[]={
               new Device(1, "Router", "040-123-456"),
               new Device(2, "Modem", "040-124-456"),
               new Device(3, "RAM", "040-125-456"),
               new Device(4, "CD", "040-126-456")
                       };
              
       //constructor to set the devices array and size
       DeviceList deviceList=new DeviceList(devices,4);
      
       //print device list
       System.out.println(deviceList.toString());
      
       //remove element at index 0
       deviceList.remove(devices[0]);
      
       //print device list
       System.out.println(deviceList.toString());
       //add device to the list
       deviceList.add(new Device(5,"Camera","040-343-343"));
       //print device list
       System.out.println(deviceList.toString());
      
   }
}

--------------------------------------------------
Sample output:


ID: 1 Name: Router Phone Number: 040-123-456
ID: 2 Name: Modem Phone Number: 040-124-456
ID: 3 Name: RAM Phone Number: 040-125-456
ID: 4 Name: CD Phone Number: 040-126-456

Null
ID: 2 Name: Modem Phone Number: 040-124-456
ID: 3 Name: RAM Phone Number: 040-125-456
ID: 4 Name: CD Phone Number: 040-126-456

Null
ID: 2 Name: Modem Phone Number: 040-124-456
ID: 3 Name: RAM Phone Number: 040-125-456
ID: 4 Name: CD Phone Number: 040-126-456
ID: 5 Name: Camera Phone Number: 040-343-343


Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote