Respuesta :
Answer:
The codes below implement the problem statements
Explanation:
public class Percentages {
public static void computePercent (int a,int b)
{
System.out.println(a+" is "+(a*100/b)+"% of "+b);
}
public static void main(String []args)
{
int a=2;
int b=5;
computePercent(a,b);
computePercent(b,a);
}
}
Part(b)
import java.util.*;
public class Percentages {
public static void computePercent (int a,int b)
{
System.out.println(a+" is "+(a*100/b)+"% of "+b);
}
public static void main(String []args)
{
Scanner s= new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
computePercent(a,b);
computePercent(b,a);
}
}
Following are the code to the given question:
Program Explanation:
- Defining a class Percentages.
- In the next step, a method "computePercent" takes two double variables "n1,n2" in its parameter.
- Inside the method, two double variables "r1, r2" were declared that calculate the percentage and store its value, and print its value.
- Outside the method, the Main method is defined that defines the double variable and assigns the value, and passes into the method.
Program:
public class Percentages //defining a class Percentages
{
public static void computePercent(double n1, double n2) //defining a method computePercent that takes two parameter
{
double r1,r2;
r1 = (n1/n2)*100;//defining r1 variable that calculate percent and hold its value
r2 = (n2/n1)*100;//defining r2 variable that calculate percent and hold its value
System.out.println(n1+" is "+r1+ " percent of "+n2);//print calculated value
System.out.println(n2+" is "+r2+ " percent of "+n1);//print calculated value
}
public static void main(String[] as) //defining main method
{
double n1,n2;//defining double variable
n1 = 2.0;//initilalize value in n1
n2 = 5.0;//initilalize value in n2
computePercent(n1,n2);//calling computePercent method
}
}
Output:
Please find the attached file.
Learn more:
brainly.com/question/14214281
