8.8k views
3 votes
a. 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. b. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. Save the file as NumbersDem02.java.

2 Answers

3 votes

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.

User Leonel Machava
by
5.7k points
4 votes

Answer:

PART ONE:

public class NumbersDemo {

public static void main(String[] args) {

int num1= 1;

int num2= 2;

//Calling method displayTwiceTheNumber()

displayTwiceTheNumber(num1);

displayTwiceTheNumber(num2);

//Calling method displayNumberPlusFive()

displayNumberPlusFive(num1);

displayNumberPlusFive(num2);

//Calling method displayNumberSquared()

displayNumberSquared(num1);

displayNumberSquared(num2);

}

public static void displayTwiceTheNumber(int num1){

System.out.println(num1*2);

}

public static void displayNumberPlusFive(int num1){

System.out.println(num1+5);

}

public static void displayNumberSquared(int num1){

System.out.println(num1*num1);

}

}

PART TWO

import java.util.Scanner;

public class NumbersDem02 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter Values for num1 and num2");

int num1= in.nextInt();

int num2= in.nextInt();

//Calling method displayTwiceTheNumber()

displayTwiceTheNumber(num1);

displayTwiceTheNumber(num2);

//Calling method displayNumberPlusFive()

displayNumberPlusFive(num1);

displayNumberPlusFive(num2);

//Calling method displayNumberSquared()

displayNumberSquared(num1);

displayNumberSquared(num2);

}

public static void displayTwiceTheNumber(int num1){

System.out.println(num1*2);

}

public static void displayNumberPlusFive(int num1){

System.out.println(num1+5);

}

public static void displayNumberSquared(int num1){

System.out.println(num1*num1);

}

}

Step-by-step explanation:

Three methods are created to do exactly as required by the question

In part two, the scanner class is used to receive the values from the user

User Kaysa
by
5.0k points