Challenges
Last updated
Last updated
Create a String variable inside your application and initialize it with "The quick brown fox jumps over the lazy dog"
. Now output this String to the terminal together with an uppercase version of the String. Check the API of the String class. String objects have a method to do just this.
A character (char
datatype in Java) in most programming languages is nothing but a number. The first 128 character representations are called the ASCII values (ASCII stands for American Standard Code for Information Interchange). So an ASCII code is the numerical representation of a character such as 'a' or '@'. Below is a list of these values.
The code below requests a single character from the user. It checks if the inputted character is a lowercase alphabet letter. Expand the code so it also states if the character is a capital letter or a number. Characters can be compared just as numbers.
Take a good look at how the comparison is applied to a character datatype. Also note the difference between 1
which is the number and '1'
which is the character.
Example output:
Ask the user of your application for his/her name and store this data inside a String variable. Output a personalized message.
For example:
Request a single word from the user and reverse it. Make sure to lowercase it first, otherwise it might look weird.
Use a basic for loop for the reversing process. Make use of the length()
method to get the number of characters in the String.
For example:
Request a single word from the user and check if it is a palindrome.
INFO - Palindrome
A palindrome is a word, number, sentence, or verse that reads the same backward or forward. It derives from the Greek palin dromo, which means "running back again"
Example output:
Another example:
Request a single word from the user. This can be accomplished using the next()
method of Scanner
.
Now creating the following String variable inside your application:
Next select a random vowel from the String. Search the word that was provided by the user and display an appropriate message to the user if the vowel was found inside the string provided by the user.
Example:
Another example:
Create an application that requests a sentence from the user. Save the sentence in a String variable. Next check if the sentence contains a specific word. You can choose this word yourself. Check the String API for a method that allows you to search in a String.
For example:
Another example:
Create an application that requests a sentence from the user. Now split the sentence into words and print each word on a separate line. There are several ways to do this. Pick one you understand.
Example:
Create a variable called secret
of type String
an initialize it with a single word (choose it yourself). Display the secret to the user but do not show the actual letters. Show some symbol instead of each letter which you can pick for yourself. Show as much symbols as that there are letters.
Now let the user guess for letters of the secret word. If the user finds a letter, display the secret again but this time with the guessed letter revealed.
You are basically creating the core of a Hangman game here.
Example:
Create a Java program that converts a decimal number (0 till 255) to hexadecimal number (0x00 to 0xFF). Only a single byte is required.
Java has no native support for hexadecimal notation so you will need to create a String for the hexadecimal number. The easiest solution lies in the usage of the the division operator /
and the modulo operator %
.
Example:
Characters can also be used in simple calculations as shown below: