Write a method called average that accepts two integer parameters and returns their average as a floating point value. Ans



Download 18.11 Kb.
Page1/5
Date16.12.2020
Size18.11 Kb.
#54714
  1   2   3   4   5
7 short questions
7 short questions, 4 (2)

7.1 Write a method called average that accepts two integer parameters

and returns their average as a floating point value.

Ans

public double averageRange (int start, int end)

{

int sum = 0, count = 0;



if (end < start)

{

System.out.println ("ERROR: Invalid Range");



return 0;

}

else



{

for (int num = start; num <= end; num++)

{

sum += num; count++;



}

return (sum/count);

}

}

7.2 Overload the average method of Exercise 7.1 such that if three



integers are provided as parameters, the method returns the average

of all three

Ans

public double average (int num1, int num2, int num3)

{

return (num1 + num2 + num3) / 3.0;



}

7.3


Overload the average method of Exercise 7.1 to accept four

integer parameters and return their average.

Ans

public double average (int num1, int num2, int num3, int num4)

{

return (num1 + num2 + num3 + num4) / 4.0;



}

7.4


Write a method called multiConcat that takes a String and an

integer as parameters. Return a String that consists of the string


Download 18.11 Kb.

Share with your friends:
  1   2   3   4   5




The database is protected by copyright ©ininet.org 2024
send message

    Main page