Question

Complete the generateFormalName method so that… you return the formal name (Mr. or Ms. + last...

Complete the generateFormalName method so that… you return the formal name (Mr. or Ms. + last name) given a full name and gender (Strings) as parameters.

--You can assume a valid name & gender (any case allowed) is passed in. Example 1: ("Bob Smith", "MaLE") passed in should generate "Mr. Smith" Example 2: ("Maggie May", "feMALE") passed in should generate "Ms. May"

Tip 1: You are given a String formalName initialized to the empty String -- you will want to concatenate other Strings onto this to produce the full formalName.

Tip 2: Write your algorithm in English first.

Tip 3: Think of all of the methods at your disposal and which could be helpful.

Given Code:

public class StringPractice {

   public String generateFormalName(String fullName, String gender)
   {
       String formalName = "";
      
       //Insert your code here:
      
       return formalName;
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.lang.*;
import java.io.*;

public class StringPractice
{
   public static void main (String[] args) throws java.lang.Exception
   {
       System.out.println(generateFormalName("Maggie May", "feMALE"));
   }
   public static String generateFormalName(String fullName, String gender)
{
String formalName = "";
  
String[] arrOfStr = fullName.split("\\s");
if(gender.toLowerCase().equals("female"))
{
   formalName="Ms. "+arrOfStr[1];
}
else if(gender.toLowerCase().equals("male"))
{
   formalName="Mr. "+arrOfStr[1];
}
  
return formalName;
}

}

Add a comment
Know the answer?
Add Answer to:
Complete the generateFormalName method so that… you return the formal name (Mr. or Ms. + last...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • These are the instructions for a java file. We have not learned array of string or...

    These are the instructions for a java file. We have not learned array of string or anything that complex. I am trying to solve this possibly using the IndexOf method. 13.4 Assignment 4: Formal Name Generator Complete the generateFormalName method so that... you return the formal name (Mr. or Ms. + last name) given a full name and gender (Strings) as parameters. --You can assume a valid name & gender (any case allowed) is passed in. Example 1: ("Bob Smith",...

  • Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer...

    Please run the program and show the result with screenshots after. Thank you (Java Eclipse) Customer Data: Area Codes Assume you work for a company that tracks customer information, including name, gender and phone numbers Your company has a file called customers.txt which contains the following information: Jiming Wu F 4082123458 James Brown M 8315678432 Leanna Perez F 4087654433 Xing Li M 8313214555 Stacey Cahill O 8312123333 Mohammed Abbas M 4083134444 Kumari Chakrabarti F 4086667777 Shakil Smith M 4082123333 Jung...

  • Complete the Person class: For setEmail, setFirstName, and setSurname: if the method is passed an empty...

    Complete the Person class: For setEmail, setFirstName, and setSurname: if the method is passed an empty string, it should do nothing; but otherwise it should set the appropriate field. For setMobile: if the method is passed a valid mobile phone number, it should set the appropriate field; otherwise it should do nothing. A string is a valid mobile phone number if every character in it is a digit from 0 to 9. Hints: To convert a string so you can...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • WHERE in this code should you put the main method so that the code will compile...

    WHERE in this code should you put the main method so that the code will compile WITHOUT changing anything else in the code? Simply adding the main method only? public class Account { private String name; private double balance;    public Account(String name, double balance) { this.name = name; if (balance > 0.0) this.balance = balance; } public void deposit(double depositAmount) { if (depositAmount > 0.0) balance += depositAmount; }    public double getBalance() { return balance; } public String...

  • LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing...

    LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems. Power For this method you will practice using methods from the Math class. The first step you must take is to write the...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • C++ with Pseudocode in the beginning This assignment will require you to write a program that...

    C++ with Pseudocode in the beginning This assignment will require you to write a program that will create an array of 10 string objects. The array will be initialized with the strings which contain the person’s name and phone number in one string. The following is an example of test data: “Renee Javens, 678-1223”, “Joe Looney, 586-0097”, “Geri Palmer, 223-8787”, “Lynn Presnell, 887-1212”, “Bill Wolfe, 223-8878”, “Sam Wiggins, 486-0998”, “Bob Kain, 586-8712”, “Tim Haynes, 586-7676”, “John Johnson, 223-9037”, “Jean James,...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

  • PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with...

    PLEASE READ ALL Modify the MergeSort code provided in class (if you find a mistake with it extra credit) (https://repl.it/@ashokbasawapatna/MergeSortExample) to work with Students (No need for generic types, you will only write Main.java). Then use the compareTo method for strings in Java to sort the students in Alphabetical order via MergeSort. 1) Create an array of 10 students by hand. Make sure it's not in alphabetical order. ONLY USE LOWER CASES FOR NAMES (I'll only test with that). 2)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT