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

in Java Precipitation enum * To change this license header, choose License Heade

ID: 3708132 • Letter: I

Question

in Java

Precipitation enum
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author kerlin
*/
public enum Precipitation
{None,Rain,Snow}

Problem 1: Precipitation I have created a Precipitation enum for you to use. Write a simple driver program with 2 methods. The main() method should: Ask the user what type of Precipitation is occurring right now. Send that data to a method called weather) If the method generates an exception, at the very least print out the message generated by the Exception The weather) method should: Take in an input String. Check the input String against the acceptable Precipitation values in the enum If the input String doesn't match an acceptable enum value, throw a InvalidPrecipitation Exception exception with the message "Exception: That is NOT a valid precipitation!" If the input String matches an acceptable Precipitation value, print the correct message (Precipitation value in bold, String message follows: None: "Enjoy dry sunny day! Rain: "Might be a good day to collect water?" Snow: "I hope you have a shovel ready!" Grading Breakdown: · 10-Correctly creating the Exception Class 10 Correctly generating/throwing the Exception · 10-Correctly trying and catching the Exception 10 - -Correctly working with enum

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


InvalidPrecipitationException.java
---------------------------
public class InvalidPrecipitationException extends Exception {
public InvalidPrecipitationException()
{

}

public InvalidPrecipitationException(String msg)
{
super(msg);
}
}


PrecipitationDriver.java
-----------------------
import java.util.Scanner;

public class PrecipitationDriver {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String type;

System.out.print("Enter the precipitation type (None, Rain, Snow): ");
type = keyboard.next();

try {
weather(type);
} catch (InvalidPrecipitationException e) {
System.out.println("Exception: " + e.getMessage());
}

}

public static void weather(String type) throws InvalidPrecipitationException
{
if(type.equals(Precipitation.None.name()))
System.out.println("None: Enjoy dry sunny day!");
else if(type.equals(Precipitation.Rain.name()))
System.out.println("Rain: Might be a good day to collect water?");
else if(type.equals(Precipitation.Snow.name()))
System.out.println("Snow: I hope you have shovel ready!");
else
throw new InvalidPrecipitationException("That is NOT a valid precipitation");
}

}


output
=====
Enter the precipitation type (None, Rain, Snow): Snow
Snow: I hope you have shovel ready!

-------
Enter the precipitation type (None, Rain, Snow): Sunny
Exception: That is NOT a valid precipitation