官术网_书友最值得收藏!

Working with variables, methods, and sources

So far, we have been creating many variables, and JShell created a few scratch variables after we entered expressions and they were successfully evaluated. Enter the following command in JShell to list the type, name, and value of the current active variables that have been created so far in the current session:

/vars

The following lines show the results:

| float width = 50.0
| float height = 25.0
| float area = 1250.0
| PrintStream $5 = java.io.PrintStream@68c4039c
| float $6 = 1250.0
| float $8 = 1260.5

Enter the following code in JShell to assign 80.25 (float) to the previously created width variable:

width = 80.25f;

After we enter the previous line, JShell will display the next message indicating it has assigned 80.25 (float) to the existing variable named width of the float type:

width ==> 80.25
| assigned to width : float

Enter the following code in JShell to assign 40.5 (float) to the previously created height variable:

height = 40.5f;

After we enter the previous line, JShell will display the next message indicating it has assigned 40.5 (float) to the existing variable named height of the float type:

height ==> 40.5
| assigned to height : float

Enter the following command in JShell again to list the type, name, and value of the current active variables:

/vars

The following lines show the results that reflect the new values we have assigned to the width and height variables:

| float width = 80.25
| float height = 40.5
| float area = 1250.0
| PrintStream $5 = java.io.PrintStream@68c4039c
| float $6 = 1250.0
| float $8 = 1260.5

Enter the following code in JShell to create a new method named calculateRectanglePerimeter. The method receives a width variable and a height variable for a rectangle and returns the result of the multiplication by 2 of the sum of both values of the float type.

float calculateRectanglePerimeter(float width, float height) {
    return 2 * (width + height);
}

After we enter the previous lines, JShell will display the next message indicating it has created a method named calculateRectanglePerimeter with two arguments of the float type:

| created method calculateRectanglePerimeter(float,float)

Enter the following command in JShell to list the name, parameter types, and return the type of the current active methods that have been created so far in the current session:

/methods

The following lines show the results.

| calculateRectangleArea (float,float)float
| calculateRectanglePerimeter (float,float)float

Enter the following code in JShell to print the results of calling the recently created calculateRectanglePerimeter with width and height as the arguments:

calculateRectanglePerimeter(width, height);

After we enter the previous line, JShell will call the method and it will assign the results to a scratch variable whose name starts with $ and continues with a number. JShell displays the scratch variable name, $16, the value assigned to the variable that indicates the result returned by the method, 241.5, and the type for the scratch variable, float. The next lines show the message displayed in JShell after we enter the previous expression that called a method:

$16 ==> 241.5
| created scratch variable $16 : float

Now, we want to make changes to the recently created calculateRectanglePerimeter method. We want to add a line to print the calculated perimeter. Enter the following command in JShell to list the source code for the method:

/list calculateRectanglePerimeter

The following lines show the results:

 15 : float calculateRectanglePerimeter(float width, float height) {
 return 2 * (width + height);
 }

Enter the following code in JShell to overwrite the method named calculateRectanglePerimeter with a new code that prints the received width and height values and then prints the calculated perimeter with calls to the System.out.printf method that works in the same way as the built-in printf method. We can copy and paste the pieces from the previously listed source code. The changes are highlighted here:

float calculateRectanglePerimeter(float width, float height) {
 float perimeter = 2 * (width + height);
 System.out.printf("Width: %.2f\n", width);
 System.out.printf("Height: %.2f\n", height);
 System.out.printf("Perimeter: %.2f\n", perimeter);
 return perimeter;
}

After we enter the previous lines, JShell will display the next messages indicating it has modified and overwritten the method named calculateRectanglePerimeter with two arguments of the float type:

| modified method calculateRectanglePerimeter(float,float)
| update overwrote method calculateRectanglePerimeter(float,float)

Enter the following code in JShell to print out the results of calling the recently modified calculateRectanglePerimeter with width and height as the arguments:

calculateRectanglePerimeter(width, height);

After we enter the previous line, JShell will call the method and it will assign the results to a scratch variable whose name starts with $ and continues with a number. The first lines display the output generated by the three calls to System.out.printf that we added to the method. Finally, JShell displays the scratch variable name, $19, the value assigned to the variable that indicates the result returned by the method, 241.5, and the type for the scratch variable, float.

The next lines show the messages displayed in JShell after we enter the previous expression that called the new version of the method:

Width: 80.25
Height: 40.50
Perimeter: 241.50
$19 ==> 241.5
| created scratch variable $19 : float
主站蜘蛛池模板: 崇义县| 溧阳市| 镇平县| 商城县| 禹州市| 遂川县| 分宜县| 大石桥市| 宜阳县| 江北区| 安化县| 山东省| 嘉义县| 湘潭县| 江阴市| 延庆县| 钦州市| 抚松县| 壤塘县| 收藏| 隆回县| 崇礼县| 益阳市| 延庆县| 太和县| 孙吴县| 南安市| 彭阳县| 济宁市| 南宫市| 平潭县| 长沙市| 专栏| 富顺县| 东明县| 彭山县| 南京市| 巴塘县| 佛冈县| 建宁县| 宝坻区|