You can use a for loop that counts


Other objectives include:

Use static methods.
Use local variables.
Use arithmetic expressions.
Use Scanner to input values.
Use a class constant.
Use for loops.
Hand-in Requirements

All projects and laboratories will be submitted electronically through Blackboard. Zip up your entire project directory to submit as the source. (Right click on the project folder and follow 7-Zip > Add to "project1.zip".) The project folder should include the following two files:

Tables.java
TablesOutput.txt
Tasks

Write a program that prints

Project 1 written by YOURNAME
and calls two methods to perform the following two jobs, respectively:
Print a table showing the powers of a number entered by the user. The user should also be asked to enter the maximum exponent.
Print a table showing the results of the remainder operation, varying both numerator and denominator. The user should be asked to enter the maximum denominator and numerator.
Here is an example of what your output should look like (user input is in bold and underlined):
Enter the base: 2
Enter the maximum exponent: 7
The base is 2 and the maximum exponent is 7.

Powers of 2

x 2^x
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128

Enter the maximum numerator: 6
Enter the maximum denominator: 3
The maximum numerator is 6 and the maximum denominator is 3.

Remainders

n n%1 n%2 n%3
1 0 1 1
2 0 0 2
3 0 1 0
4 0 0 1
5 0 1 2
6 0 0 0
Details

The Console

You will need to use Scanner to obtain input from the keyboard. You should declare a class constant of type Scanner named CONSOLE at the beginning of your class; you should store new Scanner(System.in) in CONSOLE. See examples in the Chapter 2 lecture notes and in the Laboratory 2 assignment.

Table for Powers of a Number

Terminology: When computing 210 (2 to the 10th power), 2 is called the base and 10 is called the exponent. For convenience, we will use b to denote the base and x for the exponent.

In the first method, you should allow the user to enter two numbers, b and the maximum value for x. The table should show the results for different values of x, from 0 to the maximum value. For example, the user might want to print different powers of 2, from x = 0 up to x = 7.

After obtaining values from the user, your method should print a title for the table, a header for each column, and a line for each value of x and bx. Use tab characters (t) to align the columns. See the example output shown above.

You can use a for loop that counts from 0 to the maximum value for x. Before the loop, you should initialize a result variable to 1. [Note that b0 = 1.] result will be used to store the value of bx. Inside the loop, one line should be printed, and then result should be updated by multiplying it by x. [Note that this update changes result to the next power of b.]

Table for Remainders

Terminology; When computing 5 % 2, 5 is called the numerator and 2 is called the denominator. For convenience, we will use n to denote the numerator and d for the denominator.

For the second method, you should print the result of n % d for different values of n and d.

First, the method should obtain two values from the user: the maximum values for n and d. Next, the method should print a title for the table and a header for each column. The first column will be for the value of n, and the following columns will be for the values of n % d for different values of d.

Then, you should print a line for each value of n, from 1 to its maximum value. The first column should be the value of n. The following columns should be the values of n % d for different values of d.

Let's consider the pseudocode for this method step-by-step.

Pseudocode for Printing the Table for Remainders
Print headers.
Print the values in the table.
The headers for the columns need to printed in the right order with the appropriate spacing.

Task 1: Pseudocode for Printing Headers
Print title on its own line.
Print blank line.
Print a tab and n.
For each value of d from 1 up to its maximum value:
Print a tab and "n%" and d.
Print a newline.
Printing the values in the table requires a double loop.

Task 2: Pseudocode for Printing the Values in the Table
For each value of n from 1 up to its maximum value:
Print a tab and n.
For each value of d from 1 up to its maximum value:
Print a tab and n % d.
Print a newline.
Rubric

[8 Points] If your program has a method that correctly prints a table for the powers of a number.
[1 Point] For printing the table's title and headers.
[2 Points] For prompting the user for two numbers (the base and the maximum exponent).
[2 Points] For a for-loop that prints each row of the table.
[2 Points] For properly calcuating the powers of a number.
[1 Point] For properly formatting the table into columns using tabs.
[8 Points] If your program has a method that correctly prints remainders for different numerators and denominators.
[2 Points] For prompting the user for two numbers (the maximum numerator and the maximum denominator).
[2 Point] For printing the table's title and headers.
[4 Points] For properly nesting the two for loops, including printing the correct numbers, and properly formatting the table into columns using tabs.
[4 points]
If the main method of your program prints "Project 1 written by [...]" and calls the two other methods.
If your submission was a Zip file named project1.zip containing a folder named project1, which contains the other files.
If your Java program was in a file named Tables.java.
If the output of your Java program was in a file named TablesOutput.txt.
If your program contains a comment that describes what the program does and contains a descriptive comment for each method.
If your program is indented properly. 

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: You can use a for loop that counts
Reference No:- TGS085125

Expected delivery within 24 Hours