Imagine that you have a list of all the genders (as single characters) of 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” (Hint: Better multiply something by 1.0 to make sure that you get floats not integers.)

What will be an ideal response?

```
/? ?
? Method t o c o u n t t h e number o f males and f e m a l e s i n a s t r i n g
? where male i s coded as M and f e m a l e i s coded as F
? This method w i l l p r i n t o u t t h e p e r c e n t a g e o f males and f e m a l e s
?/
public s t a t i c void p r i n t P e r c e n t M a l e F e m a l e ( S t r i n g a S t r i n g )
{
i n t males = 0 ;
i n t f e m a l e s =0;
char currChar ;
// l o o p t h r o u g h c h a r a c t e r s i n t h e s t r i n g
f o r ( i n t i = 0 ; i < a S t r i n g . l e n g t h ( ) ; i ++)
{
currChar = a S t r i n g . charAt ( i ) ;
i f ( currChar == ’M’ )
males++;
e l s e i f ( currChar == ’F ’ )
f e m a l e s ++;
}
double t o t a l = males + f e m a l e s ;
System . out . p r i n t l n ( ” P e r c e n t f e m a l e s i s ” + f e m a l e s / t o t a l +
” p e r c e n t males i s ” + males / t o t a l ) ;
}

```

Computer Science & Information Technology

You might also like to view...

By restricting access to the tables in Datasheet view, the integrity of the data and structure of the data is not at risk

Indicate whether the statement is true or false

Computer Science & Information Technology

Some attachments harbor viruses, especially those with a(n) ____ extension.

A. .jpeg B. .docx C. .exe D. .zip

Computer Science & Information Technology