Java - Seasons - need help

Questions/Tutorials for any programming language not covered in other sections.
5 posts Page 1 of 1
Contributors
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Java - Seasons - need help
Jessica
For an assignment, I currently have this code:
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.
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Java - Seasons - need help
comathi
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:
Code: Select all
        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";
        }
This uses the && AND operator instead of the || OR operator, making sure both conditions are fufilled in order to return the value.

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;
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Java - Seasons - need help
CodenStuff
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".
Code: Select all
(inputTemp >= 90)
Try:
Code: Select all
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";
        }
Could work :?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Java - Seasons - need help
comathi
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.
User avatar
Jessica
x Girl Power x
x Girl Power x
Posts: 35
Joined: Thu Feb 21, 2013 1:52 am

Re: Java - Seasons - need help
Jessica
comathi got it; but thanks all :)
5 posts Page 1 of 1
Return to “Misc”