JES Can you give me jsut what I have to type on JES? Imagine that you have a lis
ID: 3804066 • Letter: J
Question
JES
Can you give me jsut what I have to type on JES?
Imagine that you have a list of the genders (as single characters) of all the students in your class, in order of their last name. The list will look something like "MFFMMMFFMFMMFFFM" where M is male and F is female. Write a function (below) percentageGenders(string) to accept a string that represents the genders. You are to count all of the M's and F's in the string and print out the ratio (as a decimal) of the each gender. For example, if the input string were "MFFF, " then the function should print something like, "There are 0.25 males,0.75 females."Explanation / Answer
public void percentageGenders(String myString)
{
int male = 0, female = 0;
float mPercent=0.0, fPercent=0.0, len = 0;
for(int i = 0; i<myString.length(); i++)
{
if(myString.chatAt(i)=='M')
male++;
else if(myString.charAt(i)=='F')
female++;
len++;
}
mPercent=male/len;
fPercent=female/len;
System.out.println("There are "+mPercent+" males,"+fPercent+" females");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.