Utility Classes in Java

 When we were using the Math class in Java, we did not create an instance of it. That is because the Math class is a Utility class.

Utility classes provide a collection of static methods generally used for common reusable tasks. To prevent objects from being created from them, their constructors are set to private.

Examples of Utility classes include java.util.Collections, java.util.Arrays, and of course java.lang.Math.

Lets try to make our own utility class.

We are going to make a utility class that will classify angles into their proper category. That is to say, we supply the angle in degrees and our utility will tell us if the angle is acute, right, obtuse, straight, reflex, or full circle. Our utility will need to handle special cases like an angle of 0 degrees, or angles greater than 360 degrees.

First lets create our empty utility class.


public class MyUtility {

  // A default constructor set to private to prevent instantiation
  private MyUtility() { }

}//MyUtility

Great, now lets add a static method to classify our angles.

 
public class MyUtility {

  // A default constructor set to private to prevent instantiation
  private MyUtility() { }

  // Method to classify an angle and print the classification
  static public void angle(double d) {

    if (d < 90.0) {
        System.out.println("Acute Angle");
    } else if (d == 90.0) {
        System.out.println("Right Angle");
    } else if (d > 90.0 && d < 180.0) {
        System.out.println("Obtuse Angle");
    } else if (d == 180.0) {
        System.out.println("Straight Angle");
    } else if (d > 180.0 && d < 360.0) {
        System.out.println("Reflex Angle");
    }
  } // angle

} //MyUtility

But lets not mess around with negative numbers...

 
public class MyUtility {

  // A default constructor set to private to prevent instantiation
  private MyUtility() { }

  // Method to classify an angle and print the classification
  static public void angle(double d) {

    // Ensure the angle is positive
    d = Math.abs(d);

    
    if (d < 90.0) {
        System.out.println("Acute Angle");
    } else if (d == 90.0) {
        System.out.println("Right Angle");
    } else if (d > 90.0 && d < 180.0) {
        System.out.println("Obtuse Angle");
    } else if (d == 180.0) {
        System.out.println("Straight Angle");
    } else if (d > 180.0 && d < 360.0) {
        System.out.println("Reflex Angle");
    }
  } // angle

} // MyUtility

Finally we add our special cases and handle angles larger that 360 degrees...

 
public class MyUtility {

  // A default constructor set to private to prevent instantiation
  private MyUtility() { }

  // Method to classify an angle and print the classification
  static public void angle(double d) {
    
    // Ensure the angle is positive
    d = Math.abs(d);

    // Special case for 0.0 degrees
    if (d == 0.0) {
        System.out.println("Acute Angle");
        return; // Exit after handling 0 degrees
    }

    // Check if it's a multiple of 360 (full rotations)
    if (d % 360.0 == 0.0) {
        System.out.println("Full Angle");
        return; // Exit after handling multiples of 360
    }

    // Normalize the angle to be within the 0-360 range
    d = d % 360.0;

    // Angle classification based on the normalized value
    if (d < 90.0) {
        System.out.println("Acute Angle");
    } else if (d == 90.0) {
        System.out.println("Right Angle");
    } else if (d > 90.0 && d < 180.0) {
        System.out.println("Obtuse Angle");
    } else if (d == 180.0) {
        System.out.println("Straight Angle");
    } else if (d > 180.0 && d < 360.0) {
        System.out.println("Reflex Angle");
    }
  } // angle

} // MyUtility

Then we need some code to use our utility...

public class MainClass {

  public static void main(String[] args) { 

  MyUtility.angle(0.0);
  MyUtility.angle(720.0);
  MyUtility.angle(1080);
  MyUtility.angle(90.0);
  MyUtility.angle(135);
  MyUtility.angle(180.0);
  MyUtility.angle(270);
  MyUtility.angle(315);
  MyUtility.angle(360);
  MyUtility.angle(450);

  } //main
} //MainClass

The output is...
Acute Angle
Full Angle
Full Angle
Right Angle
Obtuse Angle
Straight Angle
Reflex Angle
Reflex Angle
Full Angle
Right Angle
It works! I hope you have enjoyed this tutorial. Thanks for reading.

Comments

Popular Posts