Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Follow exactly all instructions given and do not use any arrays within the progr

ID: 3807153 • Letter: F

Question

Follow exactly all instructions given and do not use any arrays within the program, only allow to have 2 while loops, 1 for loop and a method you create within the program to calculate the Octagonal Number shown below. The loops are used for input validation, no if statements allowed. Keep program as simple as possible, nothing complex or advanced techniques used within program.

Write a program in Java that does the following:

Prompts the user for a starting and ending integer value (Requires a Scanner)

Checks the values so that:

The starting value is greater than 0

The ending value is greater than the starting value

Calls a method called getOctagonalNumber to calculate and return the corresponding Octagonal Number

Prints the number and its corresponding octagonal value for the given range (in a table format)

Samples of the output are shown below. (Note, your program does not have to have the exact wording in the output, but should accomplish the same tasks.)

Sample Output 1

This program will print a table of octagonal numbers in a given range.
Enter a starting value (greater than 0): 0
Your previous entry is invalid.
Enter another starting value: 1
Enter an ending value (greater than your starting value): -9
Your previous entry is invalid.
Enter another ending value: 10

Number       Octagonal Number
-----------------------------------
   1              1
   2              8
   3              21
   4              40
   5              65
   6              96
   7             133
   8              176
   9              225
   10             280

Sample Output 2

This program will print a table of octagonal numbers in a given range.
Enter a starting value (greater than 0): 15
Enter an ending value (greater than your starting value): 10
Your previous entry is invalid.
Enter another ending value: 25

Number       Octagonal Number
-----------------------------------
   15             645
   16             736
   17             833
   18             936
   19            1045
   20             1160
   21             1281
   22             1408
   23             1541
   24             1680
   25             1825

Explanation / Answer

import java.util.*;

class TestOctagonal
{
    public static void Octagonal(int start,int end) //function to compute and display octagonal numbers in the range
    {
        int oct;
        for(int i=start;i<= end;i++)
        {
            oct = 3*i*i - 2*i;
            System.out.println(i+" "+oct);
        }
    }
   public static void main (String[] args)
   {
        Scanner scan = new Scanner(System.in);
      
        System.out.println("This program will print a table of octagonal numbers in a given range.");
      
        System.out.println("Enter a starting value (greater than 0): ");
        int start = scan.nextInt();
      
        while(start <= 0) //while loop for validating starting value
        {
        System.out.println("Your previous entry is invalid.");
        System.out.println("Enter another starting value: ");
        start = scan.nextInt();
        if(start > 0)
        break;
        }
        System.out.println("Enter an ending value (greater than your starting value): ");
        int end = scan.nextInt();
      
        while(end < start) //while loop for validating ending value
        {
        System.out.println("Your previous entry is invalid.");
        System.out.println("Enter another ending value: ");
        end = scan.nextInt();
        if(end > start)
        break;
        }

        System.out.println("Number       Octagonal Number");
        System.out.println("-----------------------------------");
      
        Octagonal(start,end); //function call
   }
}


Output:

This program will print a table of octagonal numbers in a given range.
Enter a starting value (greater than 0): 0
Your previous entry is invalid.
Enter another starting value: 1
Enter an ending value (greater than your starting value): -9
Your previous entry is invalid.
Enter another ending value: 10

Number       Octagonal Number
-----------------------------------
   1              1
   2              8
   3              21
   4              40
   5              65
   6              96
   7             133
   8              176
   9              225
   10             280