Added javadoc comments

This commit is contained in:
Mattrixwv
2024-08-11 21:31:00 -04:00
parent ae1346dbcd
commit 3feefdb7dd
12 changed files with 825 additions and 60 deletions

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/HexagonalNumberGenerator.java
//Matthew Ellison
// Created: 08-20-22
//Modified: 04-13-23
//Modified: 08-11-24
//This class generates hexagonal numbers
/*
Copyright (C) 2023 Matthew Ellison
Copyright (C) 2024 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -26,18 +26,48 @@ import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A generator for hexagonal numbers, which implements the {@link Iterator} interface.
*
* <p>
* Hexagonal numbers are figurate numbers that represent hexagons. The n-th hexagonal number is given by
* the formula: H(n) = 2n^2 - n.
* </p>
*
* <p>
* This generator allows iteration over hexagonal numbers starting from the first.
* </p>
*/
public class HexagonalNumberGenerator implements Iterator<Long>{
/**
* The number to generate the next hexagonal number from.
*/
protected long num;
/**
* Constructs a new HexagonalNumberGenerator starting from the first hexagonal number.
*/
public HexagonalNumberGenerator(){
num = 1L;
}
/**
* Checks if there is a next hexagonal number that can be generated without overflow.
*
* @return {@code true} if the next hexagonal number can be generated, {@code false} otherwise
*/
@Override
public boolean hasNext(){
return (2 * num * num) > num;
}
/**
* Returns the next hexagonal number.
*
* @return the next hexagonal number
* @throws NoSuchElementException if the next hexagonal number would cause overflow
*/
@Override
public Long next(){
//If the number will result in an overflow throw an exception
@@ -52,6 +82,12 @@ public class HexagonalNumberGenerator implements Iterator<Long>{
return hexNum;
}
/**
* Checks if a given number is a hexagonal number.
*
* @param x the number to check
* @return {@code true} if the number is hexagonal, {@code false} otherwise
*/
public static boolean isHexagonal(Long x){
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) + 1L) / 4L);
return ((2L * n * n) - n) == x;