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

2. Write an exception class named InvalidTestScore. Modify the TestScores class

ID: 3668575 • Letter: 2

Question

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-1asterisks, 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 2asterisks, 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 nidenticalcharacters 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 ndollars 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.

Explanation / Answer

public class TestScores
{
private static final int MAX_SCORE = 100;
private int[] scores;
public TestScores(int[] scores)
{
this.scores = scores;
}
private int computeAverage() throws IllegalArgumentException
{
int sum = 0;
for (int i = 0; i < scores.length; i++)
{
int score = scores[i];
if (score < 0 || score > MAX_SCORE)
{
throw new IllegalArgumentException("Score (" + score + ") is not in the range 0-" + MAX_SCORE);
}
sum += scores[i];
}
int average = sum / scores.length;
return average;
}
public static void main(String[] args)
{
TestScores testScores = new TestScores(new int[] { 50, 70, 81 });
try
{
int averageScore = testScores.computeAverage();

char letterGrade;
if (averageScore < 60) letterGrade = 'F';
else if (averageScore < 70) letterGrade = 'D';
else if (averageScore < 80) letterGrade = 'C';
else if (averageScore < 90) letterGrade = 'B';
else letterGrade = 'A';
System.out.println(" Your average score is " + averageScore + " and your grade is an " + letterGrade);  
}
catch (IllegalArgumentException illegalArgumentException)
{
System.out.println(illegalArgumentException.getMes sage());
}
}
}

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