ScholarMatic | 24/7 Homework Help

ScholarMatic Will Help You Write Your Essays and Term Papers

Answered » You can buy a ready-made answer or pick a professional tutor to order an original one.

Java Help to code scenarios w5

by | Nov 29, 2023 | questions

1. Code a try statement that catches the NumberFormatException that may be thrown when the following statement is executed:

int count = Integer.parseInt(countString);

If a NumberFormatException occurs, print the message “Not a valid integer.” to the console.

2. Code a try statement that catches an IOException and an EOFException. If an IOException occurs, print the message “An I/O error occurred.” to the console. If an EOFException occurs, print the message “End of file.” to the console. You don’t need to code anything within the try block.

Code example 13-3

private static double calculateDiscount(int quantity)

{

if (quantity

throw new IllegalArgumentException(

“Quantity must be greater than zero.”);

double discountPct = 0.0;

if (quantity > 10 && quantity

discountPct = 0.1;

else if (quantity >= 20 && quantity

discountPct = 0.2;

else

discountPct = 0.3;

return discountPct;

}

3. (Refer to code example 13-3.) Code a try statement that calls the calculateDiscount method and assigns the return value to a double variable named discountPct. If an IllegalArgumentException occurs, the message contained in the exception object should be displayed at the console. Regardless of whether an IllegalArgumentException occurs, print the message “calculateDiscount method called.”

4. (Refer to code example 13-3.) Code a try statement that parses a string variable named quantityString to an int variable and then calls the calculateDiscount method and assigns the return value to a double variable named discountPct. Code a catch block for a NumberFormatException that prints the message “Invalid integer” to the console. Don’t code a catch block for the IllegalArgumentException. Write code that prints the message “Try statement completed” to the console regardless of whether a NumberFormatException or an IllegalArgumentException occurs. And write code that prints the message “No illegal argument” if a NumberFormatException occurs, but not if an IllegalArgumentException occurs.

5. Write the code for a method named getFile that accepts a file name and returns a file created from the RandomAccessFile class. This method should catch the FileNotFoundException if it occurs and print the message “File not found.” at the console. It should not catch an IOException. Use this statement to open the file:

file = new RandomAccessFile(fileName, “r”);

6. Code a statement to test a catch block that catches an IOException.

7. Code a catch block that catches an IOException, prints the message “An I/O exception occurred.” to the console, and then throws the exception to the calling method.

8. Write the code for a method named calculateTax that accepts a double parameter named subtotal and a boolean parameter named isFresno and calculates and returns the sales tax. If isFresno is false, the sales tax should be .0775. If it’s true, the sales tax should be increased by .001. If the subtotal is less than or equal to zero, an IllegalArgumentException should be thrown with the message “Subtotal must be greater than zero.”

9. Code a catch block that catches a NumberFormatException and then prints the stack trace.

10. Code a catch block that catches a NumberFormatException and then prints the name of the class that the exception was created from along with the message associated with the exception to the standard error output stream.

11. Write the code for a custom exception class named CustomerDAOException that defines a checked exception. This class should include the two standard constructors for exception classes.

12. Write the code for a public static method named addCustomer that throws a custom exception named CustomerDAOException. This method should accept a Customer object and return a boolean value that indicates if the add operation was successful. To add the customer, call a method named writeCustomer that accepts the Customer object and returns a boolean value. If an IOException occurs, the addCustomer method should throw a CustomerDAOException with an appropriate message

ScholarMatic: Explanation & Answer

Your ready answer from a verified tutor is just a click away for as little as $14.99


  

Click Order Now to get 100% Original Answer Customized to your instructions!

HOME TO CERTIFIED WRITERS

Why Place An Order With Us?

  • Certified Editors
  • 24/7 Customer Support
  • Profesional Research
  • Easy to Use System Interface
  • Student Friendly Pricing

Have a similar question?

PLAGIRAISM FREE PAPERS

All papers we provide are well-researched, properly formatted and cited.

TOP QUALITY

All papers we provide are well-researched, properly formatted and cited.

HIGHLY SECURED

All papers we provide are well-researched, properly formatted and cited.

ScholarMatic: Get Started

Assignment Writing Service

Feel safe and secure when placing an order on our portal!
Fruitful cooperation begins with solid guarantees, and we are professional enough to promise perfect results. Let’s get it started!