Question

Methods: Net beans IDE Java two methods. Identifier: calculateScore(String word) Parameters: word – the word for...

Methods:

Net beans IDE Java two methods.

Identifier:

calculateScore(String word)

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, l, s, u

2 – d, g

3 – b, c, m, p

4 – f, h, v, w, y

5 – k

8 – j, x

10 – q, z

You should represent blank tiles by using the space character (i.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.

Identifier:

displayMenu(String message, int min, int max)

Parameters:

message – The menu text that should be displayed

min – The minimum allowable menu option

max – The maximum allowable menu option

Return Value:

int – The choice selected by the user

Other:

This method should be static.

When this method is called, it will display the String parameter which is the menu text which should include the possible selections. This method should then prompt the user to make a selection and perform value and type-safe user input checking until a user has made a valid selection. If the user does not enter an int value, they should be told “Please enter a number between $min and $max” where $min and $max are the values of the parameters min and max.

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

(a)

// Program in Java with required function calculateScore

// Parent Class
public class Calculate {
// main function to call the calculateScore function
public static void main (String[] args)
{
// print the value returned by calculateScore method
System.out.printf("The score of entered sting \"Shubham Batham\" is: %d\n", calculateScore("Shubham Batham"));
}
  
//static method calculateScore to calculate the score of the word passed as a parameter
static int calculateScore(String word)
{
// convert the word to lower case
word = word.toLowerCase();
// initialize the score to zero
int score = 0;
// iterate over each character
for(int i=0; i<word.length(); i++)
{
// get each character in a character variable
char character = word.charAt(i);
// use switch to calculate the value of the word
switch(character)
{
case ' ' : break;
case 'e' :
case 'a' :
case 'i' :
case 'o' :
case 'n' :
case 'r' :
case 't' :
case 'l' :
case 's' :
case 'u' : {score++; break;}
case 'd' :
case 'g' : {score+=2; break;}
case 'b' :
case 'c' :
case 'm' :
case 'p' : {score+=3; break;}
case 'f' :
case 'h' :
case 'v' :
case 'w' :
case 'y' : {score+=4; break;}
case 'k' : {score+=5; break;}
case 'j' :
case 'x' : {score+=8; break;}
case 'q' :
case 'z' : {score+=10; break;}
default : break;
}
}
// return the score from the function
return score;
}
}

// Output

The score of entered sting "Shubham Batham" is: 30

// Screenshot of Code, input & Output (for reference to indentation)

(b)

// Program in Java with required function displayMenu   
import java.util.Scanner; //for scanner to get input from the user

// Parent Class
public class Calculate {
// main function to call the displayMenu function
public static void main (String[] args)
{

// menu string to store the menu
String menu = "\n\t\t-----MENU-----\n\t1.Choice 1\n\t2.Choice 2\n\t"
+ "3.Choice 3\n\t4.Choice 4\n\t5.Choice 5\n\t6.Choice 6\n\tEnter choice: ";
// call the displayMenu function and display the choice
System.out.printf("Entered choice is: %d\n", displayMenu(menu,1,6));
}

// static function displayMenu that takes the menu , minimum choice & maximum choice as parameters // and returns the choice entered by the user after checking the choice value internally
static int displayMenu(String message, int min, int max)
{
// scanner object to get the user input
Scanner sc = new Scanner(System.in);
// variable to store the input
int input;
// display the menu
System.out.print(message);
// get user input
input = sc.nextInt();
//check for correct input
while(input < min || input > max)
{
// display error prompt for wrong input
System.out.printf("Please enter a number between %d and %d\n", min , max);
// display menu again
System.out.print(message);
// get input again
input = sc.nextInt();
}
//return the choice of the user
return input;
}

}

// Output

       -----MENU-----
   1.Choice 1
   2.Choice 2
   3.Choice 3
   4.Choice 4
   5.Choice 5
   6.Choice 6
   Enter choice: 8
Please enter a number between 1 and 6

       -----MENU-----
   1.Choice 1
   2.Choice 2
   3.Choice 3
   4.Choice 4
   5.Choice 5
   6.Choice 6
   Enter choice: 4
Entered choice is: 4

// Screenshot of Output, Input & Code (for reference to indentation)

Add a comment
Know the answer?
Add Answer to:
Methods: Net beans IDE Java two methods. Identifier: calculateScore(String word) Parameters: word – the word for...
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
  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • 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...

  • pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the...

    pls help java ASAP!!!!!!! Topic String Tokenizer Static Methods Static Variables Primitive Arrays Description Enhance the last assignment by providing the following additional features: (The additional features are listed in bold below) Class Statistics In the class Statistics, create the following static methods (in addition to the instance methods already provided). • A public static method for computing sorted data. • A public static method for computing min value. • A public static method for computing max value. • A...

  • Write four overloaded methods called randomize. Each method will return a random number based on the...

    Write four overloaded methods called randomize. Each method will return a random number based on the parameters that it receives: randomize() - Returns a random int between min and max inclusive. Must have two int parameters. randomize() - Returns a random int between 0 and max inclusive. Must have one int parameter. randomize() - Returns a random double between min and max. Must have two double parameters. randomize() - Returns a random double between 0 and max. Must have one...

  • !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random...

    !!!!!!!Java!!!!! When you are confident that your methods work properly and that you can generate random text with my generateText method, you can move on to the second step. Create a third class called Generator within the cs1410 package. Make class. This class should have a main method that provides a user interface for random text generation. Your interface should work as follows: Main should bring up an input dialog with which the user can enter the desired analysis level...

  • java

    You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here.   Step 1Develop the following class:ClassName: CollegeDegreeAccess Modifier: publicInstance variablesName: majorAccess modifier: privateData type: StringName: numberOfCoursesAccess modifier: privateData type: intName: courseNameArrayAccess modifier: privateData type: String [ ]Name: courseCreditArrayAccess modifier: privateData type: int [ ]Static variablesName: maximumNumberOfCoursesAccess modifier: privateData type: intInitial Value: 40ConstructorName: CollegeDegreeAccess modifier: publicParameters: none...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

  • 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...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

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