Question

Write a Java program to do the following with your name. This can all be done...

Write a Java program to do the following with your name. This can all be done in the main() method.

1. Create a String variable called myName and assign your personal name to it. Use proper capitalization for a legal name. I.e. String myName = "Billy Bob";

2. Load myName with the upper case version of itself and display the result.

3. Load myName with the lower case version of itself and display the result.

4. Capitalize the first letter of each given name in the lowercased name and display the result. Load the result into myName. Hint: This is not a single method call. You will need a loop to perform this. Hint: The substring method will be very helpful as you should likely build a new string result. Avoid magic number references. This solution should work with any proper name.

5. Using the value of the variable myName from Step 4:

  1. Display the Initials of the name. ( This may require a Loop)
  2. Use the trim methd on myName and store the results into myName.
  3. Display the size of the name
0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main
{
public static void main (String[]args)
{
String myName="Billy Bob";
myName=myName.toUpperCase();//toUpperCase converts into capital
System.out.println(myName);
myName=myName.toLowerCase();//converts lower case
System.out.println(myName);
int i;
int ch,ch1;
for(i=0;i<myName.length()-1;i++)//parse through String
{
ch=myName.charAt(i);//store present Character
if(i==0)//for first we have to make it capital
{
ch=ch-32;//-32 will convert into capital
myName=(char)ch+myName.substring(1); //append converted capital and remaining String
}
if(ch==' ')//if the Character is ' ' next one should be capital
{
ch1=myName.charAt(i+1)-32;//convert next Character into upper case
//append upto that Character and add converted upper case and remaining
myName=myName.substring(0,i+1)+(char)ch1+myName.substring(i+2);
  
}
}
System.out.println(myName);
//first Character is always capital print that first Character
System.out.print("The first letters of all names are:"+myName.charAt(0)+" ");
for(i=1;i<myName.length()-1;i++)//parse through remaining string
{
ch=myName.charAt(i);
if(ch==' ')//if the Character is ' ' then next Character is definetly capital
System.out.print(myName.charAt(i+1)+" ");//print next character
}
myName=myName.trim();
System.out.println("\nThe size of string "+myName+" is "+myName.length());
}
}

Different input

Add a comment
Know the answer?
Add Answer to:
Write a Java program to do the following with your name. This can all be done...
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
  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • This program will take the classic tongue-twister "Peter Piper picked a peck of pickled peppers." and...

    This program will take the classic tongue-twister "Peter Piper picked a peck of pickled peppers." and generate a tongue twister phrase based on user input for a last name, a unit of measurement and a vegetable. For all three cases, the user input may be multiple words. Once read, the user input will be modified to fit the following rules: last name: No leading or trailing whitespace; the first character will be upper case and the rest lower case. unit...

  • Java Write a Simple Program This question is about following the directions below. Failure to do...

    Java Write a Simple Program This question is about following the directions below. Failure to do so, results in lost credit. Write the definition of a method that takes in an integer number, and returns true if the number is prime, otherwise, returns false. A prime number is only divisible by itself and 1. Do not write the code for main here. The code must follow these steps in the order shown: 1. Name the method isPrime, and make it...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

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