Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Scrabble Score API Page 1 of 1 Methods: calculateScore(String word) Identifier:

ID: 3807523 • Letter: S

Question

Scrabble Score API Page 1 of 1 Methods: calculateScore(String word) Identifier: Parameters word the word for which you should calculate the points in Scrabble Return Value: int-the number of points for the parameter word Other: This method should be static. This method should use the following system for scoring: 0 blank. 1 e, a i. o, n, r, t, la s, u 2-d, g 3 b, c m, p 4 f, h, v, w, y 8 -j, x. 10-q, z You should represent blank tiles by using the space character Ci.e.." If the parameter word is null or contains any characters that are not letters or a space, it should return the value -1 to indicate that the input was invalid. This method should be able to handle input that is lowercase, uppercase, or a mixture of both.

Explanation / Answer

StringScore.java

import java.util.Scanner;

public class StringScore {


   public static void main(String[] args) {

       //Declaring variables
       String str;
       int score;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);  
      
       //This loop continues to execute until the user enters a valid string
       while(true)
       {
           //getting the string entered by the user
           System.out.println("Enter the Strng :");
           str=sc.nextLine();
          
           /* calling the method by passing
           * the user entered string as argument
           */
           score=calculateScore(str);
          
           //If the returned value of the method is -1 then display error message
           if(score==-1)
           {
           System.out.println("** Invalid String **");
           continue;
           }
           else
           {
               //Displaying the Score of the string
               System.out.println("Score :"+score);
              
               break;
           }
  
       }
      
   }

  
   /* This method will calculate the score for the String
   * @ params word of type String
   * @return score of type integer
   */
   private static int calculateScore(String word) {
      
       int score=0;
       for(int i=0;i<word.length();i++)
       {
           if(!Character.isAlphabetic(word.charAt(i)) && !Character.isSpaceChar(word.charAt(i)) )
               return -1;
           else if(word.charAt(i)==' ')
               score+=0;
           else if(word.charAt(i)=='e'||word.charAt(i)=='a'||word.charAt(i)=='i'||word.charAt(i)=='o'||word.charAt(i)=='n'||
                   word.charAt(i)=='r'||word.charAt(i)=='t'||word.charAt(i)=='l'||word.charAt(i)=='s'||word.charAt(i)=='u'||
                   word.charAt(i)=='E'||word.charAt(i)=='A'||word.charAt(i)=='I'||word.charAt(i)=='O'||word.charAt(i)=='N'||
                   word.charAt(i)=='R'||word.charAt(i)=='T'||word.charAt(i)=='L'||word.charAt(i)=='S'||word.charAt(i)=='U')
               score+=1;
               else if(word.charAt(i)=='d'||word.charAt(i)=='g'||word.charAt(i)=='D'||word.charAt(i)=='G')
                   score+=2;
               else if(word.charAt(i)=='b'||word.charAt(i)=='c'||word.charAt(i)=='m'||word.charAt(i)=='p'||
                       word.charAt(i)=='B'||word.charAt(i)=='C'||word.charAt(i)=='M'||word.charAt(i)=='P')
                   score+=3;
               else if(word.charAt(i)=='f'||word.charAt(i)=='h'||word.charAt(i)=='v'||word.charAt(i)=='w'||
                       word.charAt(i)=='y'||word.charAt(i)=='F'||word.charAt(i)=='H'||word.charAt(i)=='V'||
                       word.charAt(i)=='W'||word.charAt(i)=='Y')
                   score+=4;
               else if(word.charAt(i)=='k'||word.charAt(i)=='K')
                   score+=5;
               else if(word.charAt(i)=='j'||word.charAt(i)=='x'||word.charAt(i)=='J'||word.charAt(i)=='X')
                   score+=8;
               else if(word.charAt(i)=='q'||word.charAt(i)=='z'||word.charAt(i)=='Q'||word.charAt(i)=='Z')
                   score+=10;
       }
       return score;
   }

}

___________________

Output:

Enter the Strng :
HEl8@
** Invalid String **
Enter the Strng :
Very BEautiFuL
Score :24

________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote