v Use Java programming Thank you very much. Programing requirement for: Safety A
ID: 3792126 • Letter: V
Question
v
Use Java programming Thank you very much.
Programing requirement for: Safety Alert System Version 01 I. Project and class name Create a new project name: yourfirst lastname program01 (you must include your name in the project name) Create a new class name: Safety Alert vo 1 II. Requirement 1. Input Prompt appropriate messages that asking use toenter: city name, season, temperature, and raining. Input validation: your program has to valid the user input and prompt specific message when user enter invalid input. Base on following validation: City name: only can be string of letters, not containing number. Maximum length is 20 Season: only can be either SUMMER or WINTER Temperature is in double Raining is in integer number if the program found an invalid input, prompt a specific message that tell which input is invalid and what is the correction format of that input should be, then STOP the program. That Page 2 of 4Explanation / Answer
JAVA CODE:
import java.util.*;
public class Safety_Alert_V0_1{
String city ;
String season;
Double temperature;
int raining;
final double min_temp = -20;
public static void main(String []args){
System.out.println("Enter the ");
Safety_Alert_V0_1 sav1 = new Safety_Alert_V0_1(); // creating object
sav1.detailsEntry(); // calling the method
System.out.println("Data is entered Sucessfully!! ");
sav1.processData(); // calling the method
}
// this method is for entering data and checking valid data is entered or not.
public void detailsEntry(){
Scanner s = new Scanner(System.in);
System.out.println("Enter the city name : ");
city = s.next();
if(!city.chars().allMatch(Character::isLetter) || city.length() > 20){ // java 8 syntax to check string has only characters.
System.out.println("Please enter the correct city name");
System.exit(0);
}
System.out.println("Enter the Season name (summer/winter)): ");
season = s.next();
if( !season.equals("summer") && !season.equals("winter")){
System.out.println("Please select the correct season option");
System.exit(0);
}
System.out.println("Enter the temperature(in double) : ");
try{
String temp = s.next();
temperature = Double.parseDouble(temp);
}catch(Exception e){
System.out.println("Please enter valid temperature");
System.exit(0);
}
System.out.println("Enter the Raining(in integer) : ");
try{
raining = s.nextInt();
}catch(Exception e){
System.out.println("Please enter valid Raining data");
System.exit(0);
}
}
// this method is for processing data which the user entered.
public void processData(){
double t =temperature;
int r = raining;
int option=0;
if(t< 32 && r>0){
option = 1;
}
else if(t<5||r>250){
option = 2;
}
else if(t<-15 || r>500){
option = 3;
}
switch(season){
case "winter" : switch(option){
case 1 : System.out.println("Road Icing "); break;
case 2 : System.out.println("Stay Inside"); break;
case 3 : System.out.println("Call for help (y/n)");
String help = new Scanner(System.in).next();
if(help == "y"){
System.out.println("Call 911 now !");
}
break;
default : System.out.println("normal day"); break;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.