Package edu.iu.jrsalata
Class HexNum
java.lang.Object
edu.iu.jrsalata.HexNum
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
-
Constructor Summary
ConstructorDescriptionHexNum()
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.Constructs a HexNum object by converting the given value from the specified number system to hexadecimal. -
Method Summary
Modifier and TypeMethodDescriptionadd
(int value) Adds the specified integer value to the current value.Adds the given HexNum value to this HexNum and returns the result as a new HexNum.getBin()
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
getDec()
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
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.toString()
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.
-
Field Details
-
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
Constructs a HexNum object by converting the given value from the specified number system to hexadecimal.- Parameters:
value
- the value to be converted to hexadecimalnumSystem
- the number system enum of the given value (e.g., binary, decimal)
-
-
Method Details
-
toString
Returns a string representation of the hexadecimal number. -
toString
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
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
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
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
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
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 setnumSystem
- 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
Converts the hexadecimal number to its binary representation.- Returns:
- A string representing the binary equivalent of the hexadecimal number.
-
getBin
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
-