Java - Seasons - need help
Questions/Tutorials for any programming language not covered in other sections.
5 posts
Page 1 of 1
For an assignment, I currently have this code:
Rubric:
Set up a new method called determineSeason. This method should include all of your conditionals (if/else statements). It should take in the inputTemp variable as a parameter and it should return a String. The String should be the season. This method is already called from the println method in the main method. You must use an else in your determineSeason method. The determineSeason method must return a value. Since if statements may never be never be executed, it is imperative that you have a return statement in an else or outside the conditionals all together to ensure that a value is returned. (5 points)
EDIT: All looks okay now; but need you guys to check it. Not sure if it's supposed to be "return null"; I'm a bit worried I will lose pts for that. :/
Code: Select all
/* Jessica Chen
* Wednesday 6/19/13
* Java 1.7.0_21 and NetBeans 7.3
* Guesses the season depending on temperature entered
*/
import javax.swing.*;
public class Season {
public static void main(String[] args) {
int inputTemp;
String response = JOptionPane.showInputDialog(null, "Enter the "
+ "temperature","Probable season",1);
inputTemp = Integer.parseInt(response);
String message = "Based on the temperature of " + inputTemp + " it is "
+ "most likely " + determineSeason(inputTemp);
JOptionPane.showMessageDialog(null, message);
}
public static String determineSeason (int inputTemp){
if (inputTemp > 130 || inputTemp < -20){
return "invalid";
}
else if (inputTemp >= 90){
return "summer";
}
else if (inputTemp >= 70 && inputTemp < 90){
return "spring";
}
else if (inputTemp >= 50 && inputTemp < 70){
return "fall";
}
else if (inputTemp < 50){
return "winter";
}
return null;
}
}
Rubric:
Set up a new method called determineSeason. This method should include all of your conditionals (if/else statements). It should take in the inputTemp variable as a parameter and it should return a String. The String should be the season. This method is already called from the println method in the main method. You must use an else in your determineSeason method. The determineSeason method must return a value. Since if statements may never be never be executed, it is imperative that you have a return statement in an else or outside the conditionals all together to ensure that a value is returned. (5 points)
EDIT: All looks okay now; but need you guys to check it. Not sure if it's supposed to be "return null"; I'm a bit worried I will lose pts for that. :/
Last edited by Jessica on Fri Jun 21, 2013 5:25 pm, edited 1 time in total.
Not familiar with Java, but I do know || is the logical OR operator, and && is the logical AND operator.
Looking at your code, it seems it will return "spring" if the temperature is greater or equal to 70 OR less than 90. When you pass the value 37 to your function, it evaluates >= 70 as false and < 90 as true. Since you're using an OR, only one of the conditions has to be true, hence the returned value.
So far as I can tell, the correct way of testing the value of inputTemp would be the following:
Also, instead of using the final else if to return "invalid", you could add the upper temperature limit as a condition in your first test, and the lower temperature limit as a condition in your second to last test (I've already added this in the code above).
Your last branch then becomes an else which returns "invalid", so you meet the requirements you posted (have an else and return a value in it).
Hope this helps cooll;
Looking at your code, it seems it will return "spring" if the temperature is greater or equal to 70 OR less than 90. When you pass the value 37 to your function, it evaluates >= 70 as false and < 90 as true. Since you're using an OR, only one of the conditions has to be true, hence the returned value.
So far as I can tell, the correct way of testing the value of inputTemp would be the following:
Code: Select all
This uses the && AND operator instead of the || OR operator, making sure both conditions are fufilled in order to return the value. if (inputTemp >= 90 && inputTemp<130){
return "summer";
}
else if (inputTemp >= 70 && inputTemp < 90){
return "spring";
}
else if (inputTemp >= 50 && inputTemp < 70){
return "fall";
}
else if (inputTemp < 50 && inputTemp>-20){
return "winter";
}
else{
return "invalid";
}
Also, instead of using the final else if to return "invalid", you could add the upper temperature limit as a condition in your first test, and the lower temperature limit as a condition in your second to last test (I've already added this in the code above).
Your last branch then becomes an else which returns "invalid", so you meet the requirements you posted (have an else and return a value in it).
Hope this helps cooll;
This is Java right? I have no idea lol.
Maybe reverse them because you have this at the top which is checking if the temp is higher or equals to 90, so if it's higher than 90 it will return "winter".
Maybe reverse them because you have this at the top which is checking if the temp is higher or equals to 90, so if it's higher than 90 it will return "winter".
Code: Select all
Try:(inputTemp >= 90)
Code: Select all
Could work :?if (inputTemp > 130 || inputTemp < -20){
return "invalid";
}
else if (inputTemp < 50){
return "winter";
}
else if (inputTemp >= 50 && inputTemp < 70){
return "fall";
}
else if (inputTemp >= 70 && inputTemp < 90){
return "spring";
}
else if (inputTemp >= 90){
return "summer";
}
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Your instructions don't seem to specify what value should be returned, so I think you'll be alright with the return "invalid in the else.
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023