Answer:
import java.util.Scanner;
public class PercentagePopulation {
public static void main(String[] args) {
// 1. Create an object of the Scanner class
// This will allow for user's inputs
Scanner input = new Scanner(System.in);
// 2. Create a prompt asking the user to enter the number of males
System.out.println("Please enter the number of males");
// 3. Receive the number entered by the user and store in an int
// variable called number_of_males.
int number_of_males = input.nextInt();
// 4. Create a prompt asking the user to enter the number of
// females
System.out.println("Please enter the number of females");
// 5. Receive the number entered by the user and store in an int
// variable called, number_of_females
int number_of_females = input.nextInt();
// 6. Find the sum of the number of males and females
// Store the result in an int variable called total.
int total = number_of_males + number_of_females;
// 7. Find the percentage of males by using the appropriate
// formula. Do a type-casting to allow for division in floating point
// representation by prepending the number_of_males by (double)
double percentagemales = (double) number_of_males / total * 100.0;
// 8. Find the percentage of females by using the appropriate
// formula. Do a type casting to allow for division in floating point
// representation by prepending the number_of_males by (double)
double percentagefemales = (double) number_of_females / total * 100.0;
// 9. Print out the results
System.out.println("Percentage males : " + percentagemales + "%");
System.out.println("Percentage females : " + percentagefemales + "%");
}
}
Explanation:
Please go through the comments in the code to give an explanation of the program. The source code file has also been added to this response. Please download it and go through it.