Welcome to Dream.In.Code
Become a Java Expert!

Join 150,357 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,877 people online right now. Registration is fast and FREE... Join Now!




Return season when an integer representing a month is input

 
Reply to this topicStart new topic

Return season when an integer representing a month is input, Also have to give (Opposite?) of season, see table

anorman
5 Feb, 2008 - 07:55 PM
Post #1

New D.I.C Head
*

Joined: 30 Jan, 2008
Posts: 14

Need to return both inNorthernHem and ! in NorthernHem
returning the season is simple enough but I haveno idea how to return what the season is not (!)

month_______inNorthernHemisphere_______! inNorthernHemisphere
12, 1, or 2____"Winter"__________________"Summer"
3, 4, or 5_____"Spring"__________________"Fall"
6, 7, or 8_____"Summer"________________"Winter"
9, 10, 11_____"Fall"____________________ "Spring"

Here is what I have so far

CODE
public static String season(int month, boolean inNorthernHemisphere){
            
        if (month > 12){
                        return null;}
        
        if (month < 1){
                        return null;}
        
        if (month ==12){
            return "Winter";}
            
        if (month <=2){
            return "Winter";}
        
        if (month >= 3 || 5 >= month){
            return "Spring";}
        
        if (month >= 6 || 8<= month){
            return "Summer";}
        
        if (month >= 9 || 11 >= month){
            return "Fall";}
    }  


Also I keep getting an error that this must return a string, not sure once I get the boolean ! inNorthernHemisphere if that will be resolved or something else needs to be changed
Any help would be great, thank you!

This post has been edited by anorman: 5 Feb, 2008 - 07:59 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Return Season When An Integer Representing A Month Is Input
5 Feb, 2008 - 11:50 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,933



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Rather then passing in a boolean parameter why not just have a function check for hemisphere then call for the month that returns the season:

CODE


//in main
boolean isNorthHemisphere;

//user input I imagine?
.....isNorthHemisphere = true;

if(isNorthHemisphere) {
     //function call to determine month
}
else {
    //function call to determine month
}


Hope that helps.

--KYA

This post has been edited by KYA: 5 Feb, 2008 - 11:51 PM
User is offlineProfile CardPM
+Quote Post

anorman
RE: Return Season When An Integer Representing A Month Is Input
6 Feb, 2008 - 08:25 PM
Post #3

New D.I.C Head
*

Joined: 30 Jan, 2008
Posts: 14

Well I messed around with it and got here. It works for testing (month, true) but if inNorthernHemisphere is false it still returns as if it is true (ie (12,false) returns "winter" intstead of "summer") Any ideas?

CODE
        public static String season(int month, boolean inNorthernHemisphere){
            
            if (inNorthernHemisphere = true){
            
                if (month ==12){
                    return "Winter";}
                
                else if (month <=2){
                    return "Winter";}
            
                else if (month >= 3 || 5 >= month){
                    return "Spring";}
            
                else if (month >= 6 || 8<= month){
                    return "Summer";}
            
                else if (month >= 9 || 11 >= month){
                    return "Fall";}
                else{
                    return null;}
            }
            else{
                if (month ==12){
                    return "Summer";}
                
                else if (month <=2){
                    return "Summer";}
            
                else if (month >= 3 || 5 >= month){
                    return "Fall";}
            
                else if (month >= 6 || 8<= month){
                    return "Winter";}
            
                else if (month >= 9 || 11 >= month){
                    return "Spring";}
                else{
                    return null;}}
            
                
            
            
            
        }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:11PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month