Class HexNum

java.lang.Object
edu.iu.jrsalata.HexNum

public class HexNum extends Object
The HexNum class represents a hexadecimal number and provides methods for converting between different number systems (decimal, binary, and hexadecimal), performing arithmetic operations, and formatting the hexadecimal number.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected String
    Holds the hex representation of this number
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new HexNum object with a default value of "0".
    HexNum(int value)
    Constructs a HexNum object by converting an integer value to its hexadecimal string representation.
    HexNum(String value, NumSystem numSystem)
    Constructs a HexNum object by converting the given value from the specified number system to hexadecimal.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(int value)
    Adds the specified integer value to the current value.
    add(HexNum value)
    Adds the given HexNum value to this HexNum and returns the result as a new HexNum.
    Converts the hexadecimal number to its binary representation.
    getBin(int digits)
    Converts the hexadecimal number to its binary representation with a specified number of digits.
    int
    Converts the hexadecimal value of this object to its decimal equivalent.
    void
    set(int value)
    Sets the value of this HexNum object by converting the given decimal integer to its hexadecimal representation.
    void
    set(String value, NumSystem numSystem)
    Sets the value of this HexNum object by converting the given value from the specified number system to hexadecimal.
    subtract(int value)
    Subtracts a given integer value from the current hexadecimal value.
    Subtracts the given HexNum value from this HexNum and returns the result as a new HexNum.
    Returns a string representation of the hexadecimal number.
    toString(int digits)
    Converts the hexadecimal number to a string representation with a specified number of digits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • value

      protected String value
      Holds the hex representation of this number
  • Constructor Details

    • HexNum

      public HexNum()
      Constructs a new HexNum object with a default value of "0".
    • HexNum

      public HexNum(int value)
      Constructs a HexNum object by converting an integer value to its hexadecimal string representation.
      Parameters:
      value - the integer value to be converted to hexadecimal
    • HexNum

      public HexNum(String value, NumSystem numSystem)
      Constructs a HexNum object by converting the given value from the specified number system to hexadecimal.
      Parameters:
      value - the value to be converted to hexadecimal
      numSystem - the number system enum of the given value (e.g., binary, decimal)
  • Method Details

    • toString

      public String toString()
      Returns a string representation of the hexadecimal number.
      Overrides:
      toString in class Object
      Returns:
      the string representation of this hexadecimal number.
    • toString

      public String toString(int digits)
      Converts the hexadecimal number to a string representation with a specified number of digits. If the number of digits is greater than the length of the hexadecimal value, the result is padded with leading zeros. If the number of digits is less than the length of the hexadecimal value, the result is truncated to the specified number of digits.
      Parameters:
      digits - the number of digits for the string representation
      Returns:
      the string representation of the hexadecimal number with the specified number of digits
    • add

      public HexNum add(int value)
      Adds the specified integer value to the current value.
      Parameters:
      value - the integer value to be added to the current value.
      Returns:
      a new HexNum object representing the sum of the current value and the specified integer value.
    • add

      public HexNum add(HexNum value)
      Adds the given HexNum value to this HexNum and returns the result as a new HexNum.
      Parameters:
      value - the HexNum to be added to this HexNum
      Returns:
      a new HexNum representing the sum of this HexNum and the given value
    • subtract

      public HexNum subtract(int value)
      Subtracts a given integer value from the current hexadecimal value.
      Parameters:
      value - the integer value to subtract from the current hexadecimal value.
      Returns:
      a new HexNum object representing the result of the subtraction.
    • subtract

      public HexNum subtract(HexNum value)
      Subtracts the given HexNum value from this HexNum and returns the result as a new HexNum.
      Parameters:
      value - the HexNum to be subtracted from this HexNum
      Returns:
      a new HexNum representing the result of the subtraction
    • set

      public void set(int value)
      Sets the value of this HexNum object by converting the given decimal integer to its hexadecimal representation.
      Parameters:
      value - the decimal integer to be converted and set as the value
    • set

      public void set(String value, NumSystem numSystem)
      Sets the value of this HexNum object by converting the given value from the specified number system to hexadecimal.
      Parameters:
      value - the value to be converted and set
      numSystem - the number system of the given value (e.g., binary, decimal)
    • getDec

      public int getDec()
      Converts the hexadecimal value of this object to its decimal equivalent.
      Returns:
      the decimal equivalent of the hexadecimal value.
    • getBin

      public String getBin()
      Converts the hexadecimal number to its binary representation.
      Returns:
      A string representing the binary equivalent of the hexadecimal number.
    • getBin

      public String getBin(int digits)
      Converts the hexadecimal number to its binary representation with a specified number of digits. If the binary representation is shorter than the specified number of digits, it is left-padded with zeros.
      Parameters:
      digits - the number of digits for the binary representation
      Returns:
      the binary representation of the hexadecimal number as a string