Objective:
This assignment helps students understand how to implement interfaces in Java—a crucial concept in software engineering.
Task:
-
Implement the given
Utilities211Interface
interface in a class calledUtilities211Code.java
. -
Test your implementation using the provided
Utilities211Driver.java
class. -
Ensure your code follows good Java coding standards, is well-commented, and produces the correct results.
Utilities211Interface.java (Interface Specification)
Utilities211Code.java (Implementation)
Utilities211Driver.java (Test Driver)
Expected Output
How to Use:
-
Save each class in a separate
.java
file (as named above). -
Compile:
-
Run the driver:
public abstract interface Utilities211Interface {
abstract boolean validString(String s);
abstract int numberSign(double d); abstract int range(int i1, int i2); }
import java.util.*;
public class Utilities211Code implements Utilities211Interface {
public boolean validString(String s){ s =s.replaceAll("\\s", "");
if(s.length() >=2 ) return true; else return false; }
public int numberSign(double d){
if(d >0) return 1; else if(d <0) return -1; else return 0; }
public int range(int i1, int i2){ int count =0; if(i1 > i2 ){
for(int i = i2 ; i <i1 ; i++ ){ count++; } return count; }
else{ for(int i = i1 ; i <i2 ; i++ ){ count++; } return count;
} } }
public class Utilities211Driver { public static void main(String[] args){
double d= −2.1 ; int x = 5; int y = −6; String s1 = "x ";
String s2 = "cow ";
Utilities211Code u = new Utilities211Code( );
System.out.println(u.validString(s1));
System.out.println(u.validString(s2));
System.out.println(u.numberSign( d ));
System.out.println(u.range( x,y ) ); } }