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

Q1. (20 pts.) Interface Filterable Design an interface named Filterable. This in

ID: 3874068 • Letter: Q

Question

Q1. (20 pts.) Interface Filterable Design an interface named Filterable. This interface has only one method filter that returns true for objects that are accepted (i.e., passes the filter) and false for objects that are rejected (i.e., does not pass the filter) Q2. (50 pts.) Class EncapsulatedNumber that implements Filterable Encapsulate the java.lang.Number class that is the superclass of classes BigDecimal, Biglnteger, Byte, Double, Float, Integer, Long, and Short. The name of this class will be EncapsulatedNumber. The requirements are given below: Every object of type EncapsulatedNumber contains a field named number of type Number. This field will be private Add a constructor that takes as argument a Number * Make this class implement the Filterable interface, such that method filter will return true when the number is positive (i.e., greater than 0.0 in its doubleValue) * Override the toString() method to return the String representation of the number. Q3. (30 pts.) static methods Implement the following 3 static methods public static void printArray(EncapsulatedNumberll arr) Correctness: prints all elements of the array on the console. Displayed values should be numeric. public static EncapsulatedNumberll filterEncapsulatedNumbers(EncapsulatedNumberll arr) Correctness: apply the filter on all input objects. If all fails to pass, then return null. Otherwise return an array that contains passing objects. public static void main(Stringl] args) Correctness: declare and initialize an array of EncapsulatedNumber objects. Print the array Filter the array. Then print the array again. 1. 2. 3.

Explanation / Answer

import java.util.Arrays;

//Name the class as EncapsulatedNumber to get the output

//Section 1::

//1)Interface Filterable with one method filter that returns boolean true or false

interface Filterable {

boolean filter();

}

//Section 2::

//2) Declare a class EncapsulatedNumber that implements Filterable interface

public class EncapsulatedNumber implements Filterable {

//2.1 Every object of EncapsulatedNumber has a private field named number of Type Number

private Number number;

//2.2 Constructor that takes an argument of type Number

//Note: This Constructor is used to initialize the private member of the class.

public EncapsulatedNumber(Number argNumber) {

this.number = argNumber;

}

//2.3 Make this class implement filterable interface such that method returns true when number is positive

@Override

public boolean filter() {

//converting the number to intvalue and then comparing to zero because whether the number is float,double or integer integer part of number is enough to determine if it is positive or negative

if (number.intValue() > 0) {

return true;

} else {

return false;

}

}

//2.4 Override toString() method to return String representation of number

@Override

public String toString() {

return number.toString();

}

//Section 3:::::::

//3.1 printArray method that would print all the array elements passed to it on the console

public static void printArray(EncapsulatedNumber arr[]) {

//Arrays.toString() is predefined method in Arrays utils class that would print all the elements on the console

System.out.println(Arrays.toString(arr));

}

// 3.2 filterEncapsulatedNumbers is a method that would filter out negative numbers from the array and returns an array that contains only positive numbers

public static EncapsulatedNumber[] filterEncapsulatedNumbers(EncapsulatedNumber arr[]) {

// Declaring a temporay array of length original array passed as parameter, to store the filtered out numbers i.e only positive numbers

EncapsulatedNumber[] filteredEncapsulatedNumbers = new EncapsulatedNumber[arr.length];

// Iterating the unfiltered array passed as parameter

for (int i = 0; i < arr.length; i++) {

//In this call the filter method is called by object reference of type EncapsulatedNumber

//Here arr[i] where i can be 0,1,2 .... length of the array

//when we call arr[i].filter the value stored an array at ith location is considered by the filter method for deciding number as postive or negative.

//consider i=0 when we call arr[i].filter() i.e arr[0].filter in our program arr[0]=12. So the 12 value is considered and decided positive or negative in method Filter

boolean filter = arr[i].filter();

//Based on the return type of the filter() for each value passed from the array the values are stored in a temporary array called filteredEncapsulatedNumbers

//i.e if arr[i].filter() method returns true the value store in arr[i] will be stored in temporary array filteredEncapsulatedNumbers ith location or else the value is ignored.

if (filter == true) {

filteredEncapsulatedNumbers[i] = arr[i];

}

}

//Logic to return the filtered array if filtered array is not empty return the array or else return null

if (filteredEncapsulatedNumbers.length > 0) {

return filteredEncapsulatedNumbers;

} else {

return null;

}

}

//3.3 Main method that declares the array,inititalize the array, prints the array, filters and again print the filtered elements.

public static void main(String[] args) {

//Array Declaration

EncapsulatedNumber[] encapsulatedNumbers = new EncapsulatedNumber[5];

//Array initialization

encapsulatedNumbers[0] = new EncapsulatedNumber(12);

encapsulatedNumbers[1] = new EncapsulatedNumber(30.5f);

encapsulatedNumbers[2] = new EncapsulatedNumber(40.3456677);

encapsulatedNumbers[3] = new EncapsulatedNumber(-31.5f);

encapsulatedNumbers[4] = new EncapsulatedNumber(-44.3456677);

//Printing of the unfiltered array

printArray(encapsulatedNumbers);

//Calling filterEncapsulatedNumbers method to filter out negative numbers from the array

EncapsulatedNumber[] filterEncapsulatedNumbers = filterEncapsulatedNumbers(encapsulatedNumbers);

//printing the array that is filtered i.e without negative values

printArray(filterEncapsulatedNumbers);

}

}