Question

In Java In Scrabble each player has a set of tiles with letters on them. The...

In Java

In Scrabble each player has a set of tiles with letters on them. The object of the game is to use those letters to spell words. The scoring system is complex, but longer words are usually worth more than shorter words.

Imagine you are given your set of tiles as a string, like "quijibo", and you are given another string to test, like "jib".

Write a method called canSpell that takes two strings and checks whether the set of tiles can spell the word. You might have more than one tile with the same letter, but you can only use each tile once.

Homework Answers

Answer #1

CODE:

package com.company;

//main driver class
class Main{
    //main method
    public static void main(String[] args) {
        String tile = "quijibo";
        String word = "jib";
        //testing the function
        if(canSpell(tile,word)){
            System.out.println(tile+" can spell "+word);
        }else
            System.out.println(tile+" cannot spell "+word);
    }

    //can spell method returns true if the word string is possible
    //to extract from the tiles String otherwise false
    public static boolean canSpell(String tiles, String word){
        StringBuilder tileString = new StringBuilder(tiles.toLowerCase());
        word = word.toLowerCase();
        //extracting char by char from word String
        boolean found;
        for(int i=0;i<word.length();i++){
            found = false;
            for(int j=0;j<tileString.length();j++){
                if(word.charAt(i) == tileString.toString().charAt(j)){
                    //if the current letter is found in the tile that letter is removed from the tiles String
                    //and found is set to true
                    tileString.deleteCharAt(j);
                    found = true;
                    break;
                }
            }
            //if in any iteration found remains false that means the word cannot be
            //spelt using the given String tiles hence the function returns false
            if(!found)
                return false;
        }
        //if all the letters of the String word are present in the String tiles the
        //function returns true
        return true;
    }
}

_________________________________________

CODE IMAGES:

_________________________________________

OUTPUT:

___________________________________________________

Feel free to ask any questions in the comments section
Thank You!

Know the answer?
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
Similar Questions
A tile is selected from seven tiles, each labeled with a different letter from the first...
A tile is selected from seven tiles, each labeled with a different letter from the first seven letters of the alphabet. The letter selected will be recorded as the outcome. Consider the following events. Event X : The letter selected is found in the word " BAG ". Event Y : The letter selected comes after " C ". Give the outcomes for each of the following events. If there is more than one element in the set, separate them...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been using bike sharing as a method of transportation for the flexibility, cost savings, exercise, and a myriad of other benefits. While being quite successful, many bike shares are still relatively new and have room for optimization. To help with the optimization process, we want to analyze bike share data from the city of Los Angeles. The first step of the process is to collect...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do...
Code in Java SAMPLE PROGRAM OUTPUT Because there are several different things your program might do depending upon what the user enters, please refer to the examples below to use to test your program. Run your final program one time for each scenario to make sure that you get the expected output. Be sure to format the output of your program so that it follows what is included in the examples. Remember, in all examples bold items are entered by...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
Amir-Ali Mohamed was born into a family whose most prosperous days were behind them in Nemeyistan....
Amir-Ali Mohamed was born into a family whose most prosperous days were behind them in Nemeyistan. His father was an opium addict who slowly squandered the family savings on opium and several failed business ventures. Mohamed’s’ mother came from money, but her inheritance was largely gone by the time he was a youth. After her death, the family’s financial circumstances became so bad that Mohamed was forced to leave school before graduating in order to find fulltime work to support...
Circle the letter that corresponds to the best answer for each question. 1.     Which of the...
Circle the letter that corresponds to the best answer for each question. 1.     Which of the following statements concerning the nursing process is accurate? a.     The nursing process is nurse oriented. b.    The steps of the nursing process are separate entities. c.     The nursing process is nursing practice in action. d.    The nursing process comprises four steps to promote patient well-being. 2.     Which of the following groups legitimized the steps of the nursing process in 1973 by devel- oping standards...