In: Computer Science
I have an issue this project
Background
The intensity of a hurricane is classified according to the Saffir-Simpson hurricane wind scale. The scale together with wind classifications of lower intensities is shown below:
Hurricane Wind Scale Classification |
Wind Speed Range (MPH) |
Not in scale |
0 – 38 mph |
Tropical storm |
39 – 73 mph |
Category One Hurricane |
74 – 95 mph |
Category Two Hurricane |
96 – 110 mph |
Category Three Hurricane |
111 – 129 mph |
Category Four Hurricane |
130 – 156 mph |
Category Five Hurricane |
157 mph or more |
Assignment
Write a Java program that asks the user to enter the wind speed. Use a series of nested if statements to determine the intensity of the storm using the above table. Output your result.
Validate your input to check for (invalid) negative wind speeds. For invalid wind speeds – output the classification: Invalid input.
Be sure to include a comment with your name, date, and a short description of the program.
Begin your program as follows:
import java.util.Scanner;
// TODO - add name, date, and purpose of
program here
public class Main {
public static Scanner
input = new
Scanner(System.in);
public static void
main(String[] args) {
long
speed;
String classification;
//
TODO - write your program here
}
}
Example Output
Enter wind speed (mph): 129
Classification: Category Three Hurricane
Additional Information
Do not include unnecessary if statements or unnecessary logical AND/OR clauses in your code.
should be written:
else if (speed <= 73) { // checking for category 2
My code
mport java.util.Scanner;
public class Main
{
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
long speed;
String classification="";
System.out.println("Enter wind speed (mph): ");
speed= Main.input.nextLong();
if (speed >= 0 && speed <= 38) {// cheking for
category 1
classification="Not in scale";
}
else if (speed >= 39 && speed <= 73) {// cheking for
category 2
classification="Topical storm";
}
else if (speed >= 74 && speed <= 95) {// cheking for
category 3
classification="Category One Hurricane";
}
else if (speed >= 96 && speed <= 110) {// cheking for
category 4
classification="Category Two Hurricane";
}
else if (speed >= 111 && speed <= 129) {// cheking
for category 5
classification="Category Three Hurricane";
}
else if (speed >= 130 && speed <= 156) {// cheking
for category 6
classification="Category Four Hurricane";
}
else if (speed >= 157) {// cheking for category 7
classification="Category Five Hurricane";
}
if(speed>=0)
{
System.out.print("Category is "+classification);
}
else
{
System.out.println("Invalid input.");
}
}
}
ANSWER:-
// NOTE : I removed all unnecessary if ,else statement. program working fine.if any doubt please comment
import java.util.Scanner;
public class Main
{
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
long speed;
String classification="";
System.out.println("Enter wind speed (mph): 129");
speed= Main.input.nextLong();
if(speed>=0)
{
if(speed<=38) // checking for category 1
{
classification="Not in
scale";
}
else if(speed<=73) // checking for category 2
{
classification="Topical
storm";
}
else if(speed<=95) // checking for category 3
{
classification="Category One
Hurricane";
}
else if(speed<=110) // checking for category
4
{
classification="Category Two
Hurricane";
}
else if(speed<=129) // checking for category
5
{
classification="Category Three
Hurricane";
}
else if(speed<=156) // checking for category
6
{
classification="Category Four
Hurricane";
}
else if (speed >= 157) { // checking for category
7
classification="Category Five Hurricane";
}
System.out.print("Classification:
"+classification);
}
else
System.out.print("Classification: Invalid input.");
}
}
// OUTPUT