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

JAVA create an interface ShowProperty which has only one method displayProperty.

ID: 659186 • Letter: J

Question

JAVA create an interface ShowProperty which has only one method displayProperty. Then create two classes which both implement ShowProperty. One class is NumberList which has the following members: two private members, an array of integers and the size of the array; one public method that overrides of the method displayProperty to compute the sum of the integersin the array and then displays the sum on screen using System.out.prinf

Another class is StringList which has the following members: two strings as two private members; one public method that overrides the method displayProperty to display the very first characters of the two strings on screen using System.out.printf

Finally, create a class ShowPropertyTest which has only a main method. The main method initiates an array of ShowProperty of length 2. In this array, the first entry is for an object of NumberList and the second entry is for an object of StringList, and then carries out the displayProperty via the two objects respectively.

Explanation / Answer

ShowProperty.java

   public interface ShowProperty {

  

       public void displayProperty();

   }

NumberList.java

public class NumberList implements ShowProperty {

private int[] myArray;

private int myArraySize;

@Override

   public void displayProperty() {

       int sum = 0;

       for (int i = 0; i < myArraySize; i++) {

           sum = sum + myArray[i];

       }

       System.out.println("Array Sum : " + sum);

   }

   public NumberList(int[] myArray, int myArraySize) {

       super();

       this.myArray = myArray;

       this.myArraySize = myArraySize;

   }

}

StringList.java

public class StringList implements ShowProperty {

   private String string1;

   private String string2;

@Override

   public void displayProperty() {

       System.out.println("First Char of String-1: " + string1.charAt(0));

       System.out.println("First Char of String-2: " + string2.charAt(0));

   }

   public StringList(String string1, String string2) {

       super();

       this.string1 = string1;

       this.string2 = string2;

   }

  

  

}

ShowPropertyTest.java

public class ShowPropertyTest {

   public static void main(String[] args) {

       ShowProperty[] array = new ShowProperty[2];

       int num[] = { 1, 2, 3, 4, 5 };

       NumberList obj1 = new NumberList(num, 5);

       StringList obj2 = new StringList("John", "Adam");

       array[0] = obj1;

       array[1] = obj2;

       obj1.displayProperty();

       obj2.displayProperty();

   }

}

Sample OutPut

Array Sum : 15

First Char of String-1: J

First Char of String-2: A