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:
The following lines show the results:
Enter the following code in JShell to assign 80.25
(float
) to the previously created width
variable:
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:
Enter the following code in JShell to assign 40.5
(float
) to the previously created height
variable:
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:
Enter the following command in JShell again to list the type, name, and value of the current active variables:
The following lines show the results that reflect the new values we have assigned to the width
and height
variables:
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.
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:
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:
The following lines show the results.
Enter the following code in JShell to print the results of calling the recently created calculateRectanglePerimeter
with width
and height
as the arguments:
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:
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:
The following lines show the results:
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:
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:
Enter the following code in JShell to print out the results of calling the recently modified calculateRectanglePerimeter
with width
and height
as the arguments:
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: