Statement
Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. Save the application as NumbersDemo.java.
package chapter3;
public class numbersdemo {
public static void displayTwiceTheNumber(int v){
System.out.println(" The Twice Value is" + (v+v));
}
public static void displayNumberPlusFive(int v){
System.out.println(" NumberPlusFive is "+ (v+5));
}
public static void displayNumberSquared(int v){
System.out.println("Number Squared is: "+v*v);
}
public static void main(String[] args) {
int num1=10,num2=12,c=5;
displayTwiceTheNumber(num1);
displayTwiceTheNumber(num2);
System.out.println("========================" );
displayNumberPlusFive(num1);
displayNumberPlusFive(num2);
System.out.println("========================" );
displayNumberSquared(num1);
displayNumberSquared(num2);
}
}
Explanation
The code provided is a modified version of the NumbersDemo
application. It includes the implementation of three methods: displayTwiceTheNumber
, displayNumberPlusFive
, and displayNumberSquared
. These
methods perform specific calculations on the given input values.
In the main
method, two integer variables num1
and num2
are declared and
assigned values of 10 and 12, respectively. These values are then passed as
arguments to the displayTwiceTheNumber
,displayNumberPlusFive
,
and displayNumberSquared
methods.
The displayTwiceTheNumber
method takes an integer parameter v
and prints the result of doubling the input value. It calculates the result by
adding the value to itself (v +
. For example, if
v)num1
is 10, the output will be “The Twice Value is 20” since 10 + 10
equals 20.
The displayNumberPlusFive
method also takes an integer parameter v
and prints the result of adding 5 to the input value. It calculates the result
by adding 5 to the input value (v +
. For example, if
5)num2
is 12, the output will be “NumberPlusFive is 17” since 12 + 5 equals
17.
The displayNumberSquared
method takes an integer parameter v
and prints the result of squaring the input value. It calculates the result by
multiplying the input value by itself (v
. For example, if
* v)num1
is 10, the output will be “Number Squared is: 100” since 10 * 10
equals 100.
After each method call, a line of equal signs is printed to
visually separate the output of each method.
Overall, this program demonstrates how to pass values to
methods and perform specific calculations on those values. It helps illustrate
the concept of method invocation and parameter passing in Java.
Leave a Reply