Answer:
(a)
//Header declaration of class NumbersDemo
public class NumbersDemo {
//Main method
public static void main(String[] args) {
//Declare and initialize the two integer variable
int fnumber = 23;
int snumber = 34;
//Call the method displayTwiceTheNumber() using each of the variables above, in turn, as argument
displayTwiceTheNumber(fnumber);
displayTwiceTheNumber(snumber);
//Call the method displayNumberPlusFive() using each of the variables above, in turn, as argument
displayNumberPlusFive(fnumber);
displayNumberPlusFive(snumber);
//Call the method displayNumberSquared() using each of the variables above, in turn, as argument
displayNumberSquared(fnumber);
displayNumberSquared(snumber);
} // End of main method
// Declare the method displayTwiceTheNumber() which receives an integer number as argument
// Its return type is void since it only displays twice the number
// i.e 2 * number
private static void displayTwiceTheNumber(int number) {
System.out.println("twice of " + number + " : " + 2 * number);
}
//Declare the method displayNumberPlusFive() which receives an integer
// number as argument
//Its return type is void since it only displays the result when 5 is added
// to the number i.e number + 5
private static void displayNumberPlusFive(int number) {
System.out.println(number + " plus five(5) : " + (number + 5));
}
//Declare the method displayTwiceTheNumber which receives an integer
// number as argument
//Its return type is void since it only displays the square of the number
// i.e number * number
private static void displayNumberSquared(int number) {
System.out.println(number + " squared : " + (number*number));
}
} // End of class declaration
(b)
//import the Scanner class
import java.util.Scanner;
//Header declaration of class NumbersDemo 02
public class NumbersDemo02 {
//Main method
public static void main(String[] args) {
//create an object of the Scanner class to allow for user's input
Scanner input = new Scanner(System.in);
//prompt user to enter first number;
System.out.println("Please enter first number");
//Declare and initialize an integer variable to hold the first number
int fnumber = input.nextInt();
//prompt user to enter second number;
System.out.println("Please enter second number");
//Declare and initialize an integer variable to hold the first number
int snumber = input.nextInt();
//Call the method displayTwiceTheNumber() using each of the
// variables above, in turn, as argument
displayTwiceTheNumber(fnumber);
displayTwiceTheNumber(snumber);
// Call the method displayNumberPlusFive() using each of the variables
// above, in turn, as argument
displayNumberPlusFive(fnumber);
displayNumberPlusFive(snumber);
// Call the method displayNumberSquared() using each of the variables
// above, in turn, as argument
displayNumberSquared(fnumber);
displayNumberSquared(snumber);
}
//Declare the method displayTwiceTheNumber() which receives an
// integer number as argument
//Its return type is void since it only displays twice the number
// i.e 2 * number
private static void displayTwiceTheNumber(int number) {
System.out.println("twice of " + number + " : " + 2 * number);
}
//Declare the method displayNumberPlusFive() which receives an integer
// number as argument
//Its return type is void since it only displays the result when 5 is added
// to the number i.e number + 5
private static void displayNumberPlusFive(int number) {
System.out.println(number + " plus five(5) : " + (number + 5));
}
//Declare the method displayTwiceTheNumber which receives an integer
// number as argument
//Its return type is void since it only displays the square of the number
// i.e number * number
private static void displayNumberSquared(int number) {
System.out.println(number + " squared : " + (number*number));
}
} // End of class declaration
Sample Output to (a):
twice of 23 : 46
twice of 34 : 68
23 plus five(5) : 28
34 plus five(5) : 39
23 squared : 529
34 squared : 1156
Sample Output to (b):
>> Please enter first number
12
>> Please enter second number
23
twice of 12 : 24
twice of 23 : 46
12 plus five(5) : 17
23 plus five(5) : 28
12 squared : 144
23 squared : 529
Step-by-step explanation:
The above codes have been written in Java and it contains comments explaining every part of the code. Please go through the comments for explanation.