91 Styles Sty Par 3. Alien Name Recognition Th e United Federation of Planets (U
ID: 3870509 • Letter: 9
Question
91 Styles Sty Par 3. Alien Name Recognition Th e United Federation of Planets (UFP) is preparing to make first contact with a new alien race, the Bynars, Very little is known about this species except the following The Bynar language is composed solely of sequences of 1s and Os. If a Bynar's name contains an odd number of 1s, he is a male; if it contains an even number of 1s, she is a female. Diplomacy is critical during first contact. It would be disastrous if a UFP ambassador were to address a male Bynar as "miss" or a female Bynar as "mister." Starfleet's Diplomatic Corps has suggested that you can solve the problem by writing a Java method for the Universal Translator that will determine whether a given Bynar is male or female, by name alone Complete the getGender.) method, which takes a single String argument that represents a Bynar name (you may assume that this string only contains the characters '1' and '0'). If the name contains an odd number of 1s, the method should return the String "male": otherwise, it should return the String "female." Your method should not print anything. Hint: There are multiple ways to approach this problem. You can use the modulo operator (8) to determine whether a given integer is even or odd. Alternately, you might use a boolean variable, flipping its value as you process the name. Stony Brook University of 3 899 Words English (US) + 120%Explanation / Answer
import java.util.*;
class Test
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the name of the Bynar you are speaking to : ");
String bName = sc.nextLine();
String gender = getGender(bName);
System.out.println(" This Bynar is " + gender +" "); //call to function
}
public static String getGender(String bName)
{
int sumOnes = 0;
for(int i= 0;i<bName.length();i++)
{
if(bName.charAt(i) == '1') //check each character in bName is it is equal to 1
sumOnes++; // if character = 1,increment sumOnes
}
if(sumOnes % 2 == 0) // check if sumOnes is even
return "Female";
else
return "male";
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.