Java Programming: Implementing and Testing a Custom Interface (Utilities211Interface Assignment)

 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 called Utilities211Code.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)

java

// Utilities211Interface.java public interface Utilities211Interface { /** * Method reads a string and verifies that the string contains at least 2 non-blank characters. * For example: * " a b " -> true * " x " -> false * "" -> false * " " -> false * "123abc" -> true * Length of the string does not determine validity. * @param s the string to be tested * @return true if at least 2 non-blank characters, false otherwise */ boolean validString(String s); /** * @param d number to be tested * @return 1 if the number is positive, -1 if negative, 0 if zero */ int numberSign(double d); /** * Receives 2 int numbers and counts the numbers in the range between them (exclusive). * Example: range(20, 33) returns 13, range(12, -5) returns 17 * @param i1 first integer * @param i2 second integer * @return the count of integers in the range (exclusive) */ int range(int i1, int i2); }

Utilities211Code.java (Implementation)

java

// Utilities211Code.java public class Utilities211Code implements Utilities211Interface { @Override public boolean validString(String s) { if (s == null) return false; int count = 0; for (char c : s.toCharArray()) { if (!Character.isWhitespace(c)) count++; if (count >= 2) return true; } return false; } @Override public int numberSign(double d) { if (d > 0) return 1; if (d < 0) return -1; return 0; } @Override public int range(int i1, int i2) { return Math.abs(i2 - i1); } }

Utilities211Driver.java (Test Driver)

java

// Utilities211Driver.java 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)); // false System.out.println(u.validString(s2)); // true System.out.println(u.numberSign(d)); // -1 System.out.println(u.range(x, y)); // 11 } }

Expected Output

arduino

false true -1 11

How to Use:

  1. Save each class in a separate .java file (as named above).

  2. Compile:

    nginx

    javac Utilities211Interface.java javac Utilities211Code.java javac Utilities211Driver.java
  3. Run the driver:



    java Utilities211Driver

The answer


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 ) ); } }



📩 Need a similar solution? Email me: adel455@hotmail.com















Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming