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}
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.