216k views
5 votes
A method named printTabs (with no parameters) produces a table as the numbers 1 to 10, their squares and their cubes as shown below. a) Write the method printTabs. Include the method in a working program named Test.java and invoke the method printTabs from main(). b) Modify program Test.java by adding a function selTabs to accept the starting value of the table, the number of values to be displayed, and the increment between the values. If the increment is not explicitly sent, the function should use a default value of 1. A call to selTabs(6,5,2) should produce a table of five lines, the first line starting with number 6 and each succeeding number increasing by 2. 12 144 1728 14 196 2744

1 Answer

3 votes

Final answer:

To create a table of numbers, squares, and cubes in Java, you can write a method named printTabs. The method should use a for loop to iterate through the numbers from 1 to 10 and print their squares and cubes. You can modify the program by adding a function named selTabs that accepts the starting value, count, and increment for the table.

Step-by-step explanation:

To create a table of numbers, their squares, and their cubes, you need to write a method named printTabs. Here's an example of how you can implement the printTabs method in a working program named Test.java:



public class Test {
public static void main(String[] args) {
printTabs();
}

public static void printTabs() {
for (int i = 1; i <= 10; i++) {
int square = i * i;
int cube = i * i * i;
System.out.println(i + " " + square + " " + cube);
}
}
}

To modify the program and add a function named selTabs that accepts the starting value of the table, the number of values to be displayed, and the increment between the values, you can use the following code:

public class Test {
public static void main(String[] args) {
selTabs(6, 5, 2);
}

public static void selTabs(int start, int count, int increment) {
for (int i = 0; i < count; i++) {
int number = start + (i * increment);
int square = number * number;
int cube = number * number * number;
System.out.println(number + " " + square + " " + cube);
}
}
}

Learn more about printTabs

User Carlos Paulino
by
7.4k points