Question: 1. Write a class named TestScores. The class constructor should accept an array of test scores as its argument . The class should have a

1. Write a class named TestScores. The class constructor should accept an array of test scores as its argument . The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to be counted, and then each individual test score. It should then make an array of those scores, create a TestScore object , and print the average of the scores. If an IllegalArgumentException is thrown, the main method should catch it, print "Test scores must have a value less than 100 and greater than 0." and terminate theprogram .

2. Write an exception class named InvalidTestScore. Modify the TestScores class you wrote in Exercise 1 so that it throws an InvalidTestScore exception if any of the test scores in the array are invalid. Test your exception in a program (in a Driver class located in the same file). Your program should prompt the user to enter the number of test scores, and then ask for each test score individually. Then, it should print the average of the test scores. If the average method throws an InvalidTestScore exception, the main method should catch it and print Invalid test score.

3. Assume the availability of a method named makeStars that can be passed a non-negative integer n and that returns a String of n asterisks. Write a method named printTriangle that receives a non-negative integer n and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 asterisks, and then a line of n-2 asterisks, and so on. For example, if the method received 5 it would print:

* * * * * * * * * * * * * * *

The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke makeStars to accomplish the task of printing a single line.

4. Assume the availability of a method named printStars that can be passed a non-negative integer n and print a line of n asterisks. Write a method named printTriangle that receives a non-negative integer n and prints a triangle of asterisks as follows: first a line of 1 asterisk, followed by a line of 2 asterisks, and then a line of 3 asterisks, and so on and finally a line of n asterisks. For example, if the method received 5 it would print: * * * * * * * * * * * * * * *

The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke printStars to accomplish the task of printing a single line.

5. Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identicalcharacters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If nhappens to be an even number, its value is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints a SYMMETRIC triangle of O's (the capital letter O) as follows: first a line of n O, followed by a line of n-2 O's (indented by one space), and then a line of n-4 O's (indented by two spaces), and so on. For example, if the method received 5,0 (or 4,0) it would print:

OOOOO OOO O

Note: in the above output , the first line contains 0 spaces before the first O, the next line 1 space, and so on.

Note: These instructions state what the method does when k is zero, but it is up to you, the programmer, to determine what it does when k is not zero and use it for your advantage.

The method must not use a loop of any kind (for, while, do-while) to accomplish its job. The method should invoke makeLine to accomplish the task of creating Strings of varying lengths.

6. Write a recursive, string -valued method , reverse, that accepts a string and returns a new string consisting of the original string in reverse. For example, calling reverse with the string goodbye returns the string eybdoog.

Reversing a string involves:

Nothing if the string is empty or has only 1 character (reversing a single character string does not change anything)

Otherwise concatenate the last character with the result of reversing the string consisting of the second through the next-to-last character , followed by the firstcharacter . In the above example, you would concatenate the 'e' (last character of goodbye) with the result of calling reverse on oodby (the string from the secondcharacter to the next-to-last), with the 'g' (first character ).

7. A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string ). Write a recursive,boolean -valued method , isPalindrome that accepts a string and returns whether the string is a palindrome.

A string , s, is a palindrome if:

s is the empty string or s consists of a single letter (which reads the same back or forward), or

the first and last characters of s are the same, and the rest of the string (i.e., the second through next-to-last characters ) form a palindrome.

8. Write the definition of a method named makeStarBucks that receives a non-negative integer n and returns a String consisting of n asterisks followed by n dollars signs. So, if the method received 5 it would print:

*****$$$$$ and if received 3 it would print ***$$$

The method must not use a loop of any kind (for, while, do-while) to accomplish its job.

9. Write the definition of a method named printPowerOfTwoStars that receives a non-negative integer n and prints a string consisting of "2 to the n" asterisks. So, if themethod received 4 it would print 2 to the 4 asterisks, that is, 16 asterisks:

**************** and if it received 0 it would print 2 to the 0 (i.e. 1) asterisks: *

The method must not use a loop of any kind (for, while, do-while) to accomplish its job.

10. Write a recursive boolean method named isMember. The method should accept two arguments : an int array and an int value . The method should return true if the value is found in the array , or false if the value is not found in the array . Demonstrate the method in a program that takes input from the user. First, the program should prompt the user to enter the length of the array . Then, it should prompt the user for every member of the array . Then, it should ask the user for a number to search for in the array , and print whether the number is a member of the array (either "true " or "false ").

11. Write a recursive method that accepts a string as its argument and prints the string in reverse order. Demonstrate the method in a program that takes a string as input from the user and prints out its reverse.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!