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 ) ;
}
```
You might also like to view...
Describe how to perform an SQL Server 2012 repair and why this might be necessary.
What will be an ideal response?
Match each of the following actions with the tab on which it is activated
I. Display formulas on the screen II. Exporting a document as a PDF file III. Create a recommended chart IV. AutoFit column widths V. Change a worksheet to landscape orientation A. Formulas tab B. Page Layout tab C. Insert tab D. File tab E. Home tab
A Web page document that contains two frameset elements would display a page with:
A) nested frames. B) a floating frame. C) an inline frame. D) two frames.
Which of the following statements about Flame is true?
a. It is an antivirus software b. It is a malware that acts as an antivirus software c. It is a DoS tool d. It is a virus designed for espionage