3

I am a beginner java programmer, studying off basic youtube videos and overflow forums, and i came across a question on an textbook sheet that asked me to use a for loop program and print this table out as well as fill in each blank

Number   Square   Cube   (n+1)/(2n-18)
______________________________________
1         #        #          #
2         #        #          #
3         #        #          #
4         #        #          #
5         #        #          #

I thought I should try it out to test myself. I came up with the following program that works perfectly for the Number, Square and Cube part of the table, but I don't understand how to generate the numbers using the given formula. The formula I initialized as a variable (double) doesn't print the actual results, and I honestly don't have a clue as to what to do. I'd rather a simple explanation than a complex one and simple changes to the code rather than complex. As I said I am a beginner, and many different methods may go right over my head. Thank you so much in advance (also an extra task asks that I print out the sums of each column. I don't know how to do that at all, and would like an explanation if possible, but wouldn't mind if I don't receive one)

int number;
int maxValue;

Scanner keyboard = new Scanner(System.in);
System.out.println("how many numbers do you want the max value to be");
maxValue = keyboard.nextInt();

System.out.println("Number\tSquare\tCube (n+1)/(2n-18)");
System.out.println("--------------------------------");
for (number = 1; number <= maxValue; number++) {
    double formula = (number + 1) / (number * 2);
    System.out.println(
        number + "\t\t\t" + number * number + "\t\t\t" +
        number * number * number + "\t\t\t" + formula);
}
cramopy
  • 3,459
  • 6
  • 28
  • 42
Jeff
  • 63
  • 6
  • What do you mean by "The formula (...) doesn't print the actual results"? What is the actual output and what were you expecting? For starters, you could try creating a `private` method (call it for instance `calculateFormula`), that would receive as argument your `number` and would return the `double` you're expecting. – António Ribeiro Dec 05 '17 at 11:55

4 Answers4

3

Your formula should be:

double formula = (double)(number + 1) / (number * 2 - 18);

Two issues:

  • missing -18
  • the / is doing an integer division unless you cast at least one operand into a double

Oh! one more thing: when number==9, there is a division by zero. A double division gives you "Infinity" whereas an integer division throws an exception.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
2

Your formula does not match the textbook. Try this:

System.out.println("Number\tSquare\tCube (n+1)/(2n-18)");
System.out.println("--------------------------------");

for (int number=1; number <= maxValue; number++) {
    double square = Math.pow(number, 2);
    double cube = Math.pow(number, 3);
    double formula = (number + 1) / (number * 2 - 18);
    System.out.println(number + "\t\t\t" + square + "\t\t\t" + cube + "\t\t\t" + formula);
}

Note: As pointed out by @MauricePerry an input of 9 would cause a divide by zero. Rather than try to catch this unchecked exception, I think you should control your input values so that this does not happen.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Your formula is not correct and you are missing few \t.

    int number;
    int maxValue;

    Scanner keyboard = new Scanner(System.in);
    System.out.println("how many numbers do you want the max value to be");
    maxValue = keyboard.nextInt();

    System.out.println("Number\t\tSquare\t\tCube \t\t(n+1)/(2n-18)");
    System.out.println("-------------------------------------------------------");
    for (number = 1; number <= maxValue; number++) {
        double formula = (number + 1) / (number * 2 - 18);
        System.out
                .println(number + "\t\t" + number * number + "\t\t" + number * number * number + "\t\t" + formula);

    }
vinS
  • 1,417
  • 5
  • 24
  • 37
1

I'm not sure which outputs are you getting, but it seems to me you're using integer division when trying to get the result of (n+1)/(2n-18).

Try using:

double decimalNumber = (double) number;
double formula = (decimalNumber + 1) / (decimalNumber * 2 - 18);
kosani
  • 11
  • 2