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

Class Design involving 2d arrays and overloading. Write a simple “Device” class

ID: 3841076 • Letter: C

Question

Class Design involving 2d arrays and overloading.

Write a simple “Device” class with a few features that could be used to compare Device instances and a Driver class with which to test/drive this “Device” class.

Skills Expected

• Multi-dimensional array

• Constructor and method overloading

Assignment Description

You will create two Class objects (and subsequently submit two .java files)

• Device

• Driver

Notes:

• ONLY the Driver Class should have static methods.

• The Device Class should NOT have static methods or static instance variables (constants ok).

• The below is the minimum you must include – you could include additional instance variables, methods, etc. as you need. Class Design: Device The Device class is intended to be an abstract and simplified representation of a device. Each device will have a multi-dimensional array of “features” – which contain the feature name (e.g. “Storage Size”) in one row and the feature data (e.g. “64GB”) in another row.

Data to store

• Device name

o Make this a private instance with a public getter and setter

• Features List (e.g. String[][])

o Make this a private instance with no getter and setter Available actions

• [2 Points] Constructor

o Take in as argument the device name (enforce invariants)

o Initialize the features list to have at least 10 columns and two rows

• [2 Points] Overloaded Constructor

o Take in as argument the device name as well as features list (enforce invariants)

• [2 Points] Add Feature

o Take in as argument the feature name and value and

If the feature name is NOT already in the list of features, add both the name and value to the list of features

If the feature name is already in the list of features, update the value to include the previous value as well as the new value.

• [2 Points] Get Feature Value

o Take in as argument a feature name and return the value associated with the feature if found. Return “Feature not found” as appropriate

. • [2 Points] Get Feature Value

o Take in as argument an index value and return the value associated with feature for the given index. Be sure to check for invariants

. • [2 Points] Show Features

o Print out a “table” of features for the current Device instance.

Class Design: Driver

• “Positive” testing (checking for “valid” conditions)

o [1 Point] Create a Device with only device name

o [1 Point] Create a Device with both a device name as well as a feature list

o [1 Point] Add at least three new features (with different values) to all device instances

o [1 Point] Get a feature value based on the feature name

o [1 Point] Get a feature value based on the index

o [1 Point] Print out the list of features

• “Negative” testing (checking for “invalid” conditions)

o [1 Point] Attempt to create a Device with “invalid” device name (e.g. empty string)

o [1 Point] Attempt to create a Device with “invalid device name and/or feature list

o [1 Point] Attempt to add a feature that has already been added to the feature list

o [1 Point] Attempt to get the value for a feature not in the feature list

o [1 Point] Attempt to get the value for a feature given an invalid index (e.g. -1)

• “Boundary” testing

o [1 Point] Attempt to create a Device with a feature list that is exactly at capacity

o [1 Point] Attempt to create a Device with a feature list that exceeds capacity (e.g. 11 columns)

o [1 Point] Attempt to add a feature with invalid name/value

o [1 Point] Attempt to show features when the feature list has nothing in it yet

Explanation / Answer

Hi,

Please see below the Classes.

Please comment for any queries/feedbacks.

Thanks.

Device.java


public class Device {
   private String [][] features;
   private String name;
   private int counter;
  
   /**
   [2 Points] Constructor
   o Take in as argument the device name (enforce invariants)
   o Initialize the features list to have at least 10 columns and two rows
   **/
   public Device(String name){
       //Checking for invalid name
       if(null == name || "".equalsIgnoreCase(name)){
           System.out.println("Name is not Valid");
           return;
       }
       this.name = name;
       this.features = new String [2][10];
       this.counter =0;
   }
   /**
   *[2 Points] Overloaded Constructor
    * Take in as argument the device name as well as features list (enforce invariants)
   */
   public Device(String name, String[][] features){
       //Checking for invalid name
       if(null == name || "".equalsIgnoreCase(name)){
           System.out.println("Name is not Valid");
           return;
       }
       //Checking for invalid features
       if(null == features || features.length ==0){
           System.out.println("Features not Valid");
           return;
       }
       this.name = name;
       this.features = features;
      
       for(int i=0; i<features[0].length;i++){
           if(null!= features[0][i]){
               this.counter++;
           }
       }
   }
  
   /**
   * [2 Points] Add Feature
    * Take in as argument the feature name and value and
    * If the feature name is NOT already in the list of features, add both the name and value to the list of features
    * If the feature name is already in the list of features, update the value to include the previous value as well as the new value.
   * @return
   */
  
   public void add(String featureName, String value){
       boolean contains= false;
       int presentIndex =0;
       String presentVal ="";
       for(int i=0; i<features[0].length;i++){
           String nameIn = features[0][i];
           if(featureName.equalsIgnoreCase(nameIn)){
               contains = true;
               presentIndex = i;
               break;
           }
       }
       if(!contains){
           //add both the name and value to the list of features
           features[0][counter] = featureName;
           features[1][counter] = value;
           System.out.println("Added !"+" Name : "+featureName+ " Value : "+value);
           counter ++;
       }
       else{
           //update the value to include the previous value as well as the new value.
           features[0][presentIndex] = featureName;
           features[1][presentIndex] = presentVal+"_"+value;
           System.out.println("Updated !"+" Name : "+featureName+ " Value : "+value);
       }
      
   }
   /**
   * [2 Points] Get Feature Value
   * Take in as argument a feature name and return the value
   * associated with the feature if found. Return "Feature not found" as appropriate
   * @return String
   */
   public String get(String featureName){
       String retVal ="";
       boolean contains= false;
       for(int i=0; i<features[0].length;i++){
           String nameIn = features[0][i];
           if(featureName.equalsIgnoreCase(nameIn)){
               retVal = features[1][i];
               contains = true;
               break;
           }
       }
       if(!contains){
           System.out.println("Name not found !");
           return null;
       }
       System.out.println("Name : "+featureName+ "Value : "+retVal);
       return retVal;
      
   }
   /**
   * [2 Points] Get Feature Value
   * Take in as argument an index value and
   * return the value associated with feature for the given index. Be sure to check for invariants
   * @return String
   */
   public String get(int index){
       if(index<0){
           System.out.println("Invalid Index!");
           return null;
       }
       System.out.println("Name : "+features[0][index]+ " Value : "+features[1][index]);
       return features[1][index];
      
   }
   /**
   * [2 Points] Show Features
   * Print out a "table" of features for the current Device instance.c
   * @return
   */
   public void print(){
       for(int i=0; i<features[0].length;i++){
           if(null != features[0][i]){
               System.out.println("Name : "+features[0][i]+ " Value : "+features[1][i]);
           }
       }
   }
  
   //Getters ans Setters
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
  
}

Driver.java


public class Driver {
   public static void main(String [] args){
       /**
       * "Positive" testing (checking for "valid" conditions)
       */

       // * [1 Point] Create a Device with only device name
       Device device1 = new Device("Pendrive");

       // * [1 Point] Create a Device with both a device name as well as a feature list
       String [][] device2features = new String [2][10];
       device2features[0][0] = "Frequency";
       device2features[0][1] = "Storage";
       device2features[0][2] = "Make";
       device2features[1][0] = "24Hz";
       device2features[1][1] = "16GB";
       device2features[1][2] = "All";
       Device device2 = new Device("Player",device2features);


       // * [1 Point] Add at least three new features (with different values) to all device instances
       device1.add("AAA", "111");
       device1.add("BBB", "222");
       device1.add("CCC", "333");

       device2.add("DDD", "444");
       device2.add("EEE", "555");
       device2.add("EEE", "666");

       // * [1 Point] Get a feature value based on the feature name
       device1.get("AAA");
       device2.get("Frequency");

       //* [1 Point] Get a feature value based on the index
       device1.get(1);
       device2.get(3);

       //* [1 Point] Print out the list of features
       device1.print();
       device2.print();

       /**
       * "Negative" testing (checking for "invalid" conditions)
       */

       //[1 Point] Attempt to create a Device with “invalid” device name (e.g. empty string)
       Device device3 = new Device("");

       //[1 Point] Attempt to create a Device with “invalid device name and/or feature list
       Device device4 = new Device("Feature1",new String[0][0]);

       //[1 Point] Attempt to add a feature that has already been added to the feature list
       device2.add("Frequency","32Hz");

       //[1 Point] Attempt to get the value for a feature not in the feature list
       device2.get("AAA");

       //[1 Point] Attempt to get the value for a feature given an invalid index (e.g. -1)
       device1.get(-1);


   }


}

Sample output:

Added ! Name : AAA Value : 111
Added ! Name : BBB Value : 222
Added ! Name : CCC Value : 333
Added ! Name : DDD Value : 444
Added ! Name : EEE Value : 555
Updated ! Name : EEE Value : 666
Name : AAAValue : 111
Name : FrequencyValue : 24Hz
Name : BBB Value : 222
Name : DDD Value : 444
Name : AAA Value : 111
Name : BBB Value : 222
Name : CCC Value : 333
Name : Frequency Value : 24Hz
Name : Storage Value : 16GB
Name : Make Value : All
Name : DDD Value : 444
Name : EEE Value : _666
Name is not Valid
Features not Valid
Updated ! Name : Frequency Value : 32Hz
Name not found !
Invalid Index!