Instructions For this assignment, your task is to implement an interface (given
ID: 3754532 • Letter: I
Question
Instructions For this assignment, your task is to implement an interface (given below) using recursion. You are not allowed to use iteration (for, while, do-while, etc) for any of the solutions. The interface IRecursive.java and the class Routecity.java (to help you implement the last 2 methods in the interface) have been provided. Implement the interface in a class named MyRecursiveImpl.java Implement the following interface: public interface IRecursive l string roverse (String str /reverses given string int multiply(int a, int /multiplies a and b double getTotal(intill array //adds numbers in 2d array String merge (String strl, String str2) /merges strl and str /into alphabetically //sorted string int countcities (Routecity startcity) /counts the //number of //cities in route int getTotalPeopleBoarded (Routecity startCity): /totals number of //people that boarded //in the entire route boolean ist.astCityInRoute (Routecity eity) true if given city //is last city itn //routeExplanation / Answer
package interfaces;
// Interface IRecursive definition
public interface IRecursive
{
// Reverse given string
String reverse(String str);
// Multiplies a and b
int multiply(int a, int b);
// Adds numbers in 2D array
double getTotal(int [][] array);
// Merge str1 and str2 into alphabetically sorted string
String merge(String str1, String str2);
// Counts the number of cities in route
int countCities(RouteCity startCity);
// Total number of people that boarded in the entire root
int getTotalPeopleBoard(RouteCity startCity);
// Returns true if given city is last city in route otherwise false
boolean isLastCityInRoute(RouteCity city);
}// End of interface
----------------------------------------------------------------------------------------------
package interfaces;
// Class RouteCity definition
class RouteCity
{
// Creates an array to store numbers
int arr[] = new int [20];
// To store current counter for the array
int counter;
// Parameterized constructor to assign parameter value to array
RouteCity(int no)
{
// Assigns parameter value at counter index position of the array
// and increase the counter by one
arr[counter++] = no;
}// End of parameterized constructor
// Overloads parameterized constructor with number and object
RouteCity(int no, RouteCity rc)
{
// Assigns parameter object rc's array to implicit object's array
arr = rc.arr;
// Assigns parameter object rc's counter to implicit object's counter
counter = rc.counter;
// Assigns parameter value (no) at counter index position of the array
// and increase the counter by one
arr[counter++] = no;
}// End of parameterized constructor
// Recursive method to return sum of all the elements of array
int countTotal(int arr[], int len)
{
// Checks if length is zero or less than zero return 0
if (len <= 0)
return 0;
// Otherwise recursively calls the method with length minus one
return (countTotal(arr, len - 1) + arr[len - 1]);
}// End of method
}// End of class RouteCity
// Defines a class MyRecursiveImpl and implements interface IRecursive
public class MyRecursiveImpl implements IRecursive
{
// Overrides recursive method to return reverse of a given string
public String reverse(String str)
{
// Checks if string str is null or length of the string is less than or equals to 1
if ((str == null)||(str.length() <= 1) )
// Returns the string
return str;
// Otherwise recursively calls the method with substring of str from 1 position
// by adding character at 0 position
return reverse(str.substring(1)) + str.charAt(0);
}// End of method
// Overrides method to return multiplies a and b
public int multiply(int a, int b)
{
return a * b;
}// End of method
// Overrides recursive method to return sum of 2D array
public double getTotal(int [][] array)
{
// Checks if array is null or
// array number of rows is 0 or
// number of columns is 0
if (array == null || array.length == 0 || array[0].length == 0)
// Then return 0
return 0;
// Otherwise recursively calls the method with number of rows and current row number of columns
return getTotal(array, 0, 0, 0, array.length, array[0].length);
}// End of method
// Overrides recursive helper method to return sum of 2D array
private static long getTotal(int[][] array, long sum, int curR, int curC, int maxR, int maxC)
{
// Checks if current row is greater than or equals to maximum row
if (curR >= maxR)
// Returns sum
return sum;
// Otherwise checks if current column is greater than or equals to maximum column
if (curC >= maxC)
// Recursively calls the method with next row
return getTotal(array, sum, curR + 1, 0, maxR, maxC);
// Recursively calls the method with next column
return getTotal(array, sum + array[curR][curC], curR, curC + 1, maxR, maxC);
}// End of method
// Overrides recursive method to return merge str1 and str2 into alphabetically sorted string
public String merge(String str1, String str2)
{
// Checks if string str1 is equals to null
if(str1 == null || str1.equals(""))
// Checks if string str2 is null then return string str1 otherwise return str2
return str2 == null? str1 : str2;
// Otherwise checks if string str2 is null
else if (str2 == null || str2.equals(""))
// Returns string str1
return str1;
// Otherwise checks if string str1 character at 0 position is less than string str2 character at 0 position
else if(str1.charAt(0) < str2.charAt(0))
// Recursively calls the method by adding string str1 character at 0 position
// with substring from 1 position of string str1
return str1.charAt(0) + merge(str1.substring(1, str1.length()), str2);
// Otherwise greater
else
// Recursively calls the method by adding string str2 character at 0 position
// with substring from 1 position of string str2
return str2.charAt(0) + merge(str1, str2.substring(1, str2.length()));
}// End of method
// Overrides method to count and return the number of cities in route
public int countCities(RouteCity startCity)
{
return startCity.counter;
}// End of method
// Overrides method to count total number of people that boarded in the entire root
public int getTotalPeopleBoard(RouteCity startCity)
{
// Calls the method to count
return startCity.countTotal(startCity.arr, startCity.counter);
}// End of method
// Overrides method to returns true if given city is last city in route otherwise false
public boolean isLastCityInRoute(RouteCity city)
{
// Checks if city counter is 1 then return true
if(city.counter == 1)
return true;
// Otherwise returns false
else
return false;
}// End of method
}// End of class MyRecursiveImpl
-----------------------------------------------------------------------------------------
package interfaces;
// Driver class MyRecursiveImplTest definition
public class MyRecursiveImplTest
{
// main method definition
public static void main(String[] args)
{
// Creates an object of the class MyRecursiveImpl
IRecursive recObj = new MyRecursiveImpl();
// Call the method to test
System.out.println("Expected: olleH. Got: " + recObj.reverse("Hello"));
System.out.println("Expected: 6. Got: " + recObj.multiply(2, 3));
int [][] scores2D = new int [][]{{1,2,3}, {4,5,6}, {7,8,9}};
System.out.println("Expected: 45.0. Got: " + recObj.getTotal(scores2D));
String str1 = "acgh";
String str2 = "bdef";
System.out.println("Expected: abcdefgh. Got: " + recObj.merge(str1, str2));
RouteCity rc3 = new RouteCity(15);
RouteCity rc2 = new RouteCity(30, rc3);
RouteCity rc1 = new RouteCity(25, rc2);
System.out.println("Expected: 3. Got: " + recObj.countCities(rc1));
System.out.println("Expected: 70. Got: " + recObj.getTotalPeopleBoard(rc1));
System.out.println("Expected: false. Got: " + recObj.isLastCityInRoute(rc1));
System.out.println("Expected: true. Got: " + recObj.isLastCityInRoute(rc3));
}// End of main method
}// End of class
Sample output:
Expected: olleH. Got: olleH
Expected: 6. Got: 6
Expected: 45.0. Got: 45.0
Expected: abcdefgh. Got: abcdefgh
Expected: 3. Got: 3
Expected: 70. Got: 70
Expected: false. Got: false
Expected: true. Got: true