Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Stuck on this code!

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Stuck on this code!

    I am in a low level class and I get stuck right after this:

    This is my Tester class:import java.util.Scanner;

    /**
    This program calculates the season for a given month and day.
    */
    public class Seasons
    {
    public static void main(String[] args)
    {
    String season;
    Scanner in = new Scanner(System.in);

    System.out.print("Please enter a date (month and day): " );
    int month = in.nextInt();
    int day = in.nextInt();
    Season s = new Season(month, day);
    System.out.println("Season: " + s.getDescription());
    }
    }

    And this is my main class "season"

    class Season{
    private int month;
    private int day;

    public Season(int aMonth, int aDay)
    {
    int month = aMonth;
    int day = aDay;
    }

    public String getDescription()
    {
    {
    String r;
    if (month==1){r = "Winter"; }
    else if (month==2){r = "Winter"; }
    else if (month==3){r = "Winter"; }
    else if (month==4){r = "Spring"; }
    else if (month==5){r = "Spring"; }
    else if (month==6){r = "Spring"; }
    else if (month==7){r = "Summer"; }
    else if (month==8){r = "Summer"; }
    else if (month==9){r = "Summer"; }
    else if (month==10){r = "Fall"; }
    else if (month==11){r = "Fall"; }
    else if (month==12){r = "Fall"; }
    else {r = "Not valid";}
    return r;
    }
    }
    }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Stuck on this code!

    Quote Originally Posted by bankoscarpa View Post
    I am in a low level class and I get stuck right after this:
    Hello bankoscarpa!
    Where are you stuck? Do you have a specific question?

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Stuck on this code!

    Try to be more descriptive in the future with what your problem is, and wrap your code in code tags. I can tell your problem is with this part of your code:
    private int month;
    private int day;
     
    public Season(int aMonth, int aDay)
    {
    int month = aMonth;
    int day = aDay;
    }

    Welcome to World of Scope! Lots of people have trouble with this as beginners. You have the class variables month and day, and then you have the local variables month and day. In order to set the class variables (which I assume is what you are trying to do), you should not create local variables. The statement: int month = aMonth; creates a local variable named month, that immediately gets trashed as soon as the Constructor finishes. To set the month class variable instead, simply get rid of the int declaration: month = aMonth;
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. The Following User Says Thank You to aussiemcgr For This Useful Post:

    bankoscarpa (May 2nd, 2012)

  5. #4
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Stuck on this code!

    That makes sense! I was not sure what I was doing wrong. When the code runs, it has no limit on what the day input is. If i put that the month is 2 and the day is 78, it will still return what i had for the month. How would i go about fixing that? Do i just add another if statement?
    Thank you!!!!

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Stuck on this code!

    The if/else statements would be the basic way of doing it. However, if you wanted to do it while also embracing the already provided java libraries, you could say:
    day = ? //day variable provided
    month = ? //month variable provided
    year = ? //year variable provided
     
    Calendar calendar = Calendar.getInstance();
    calendar.set(year, month, 1);
    if(day>calendar.getActualMaximum(Calendar.DAY_OF_MONTH)) {
         //tell the user there is a problem or something
    }

    There is nothing inheritly *wrong* with the above code, but it does take up significantly more memory than just the if/else statements and will run a few milliseconds slower. On a program this small, memory usage and wasting a few milliseconds isn't a problem, but it does become a problem if you have REALLY REALLY large and memory-heavy programs.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    bankoscarpa (May 2nd, 2012)

  8. #6
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Stuck on this code!

    I see, well I have a specific way of being graded. (on wiley plus) That means I should probably stick to if/else statements. I do not get the part where i have two parameters in the get method. I still am stuck on the " day " statement. I attached it. Thank you!
    wiley.jpg

  9. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Stuck on this code!

    I can't see the picture, it is too small. Keep in mind that when calculating the days of the month, you have to consider that some months have 30 days and some have 31 days, and some years, February has 28 days and other years it has 29 days.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. The Following User Says Thank You to aussiemcgr For This Useful Post:

    bankoscarpa (May 3rd, 2012)

Similar Threads

  1. Ok Im stuck where has my code went wrong
    By gudwindavids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2011, 03:08 AM
  2. completely stuck on a simple code
    By disp in forum Loops & Control Statements
    Replies: 6
    Last Post: June 30th, 2011, 12:02 PM
  3. Help!, newbie, interested in Java, Very simple code, completely stuck!
    By munkybunky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 30th, 2011, 07:25 AM
  4. Im stuck, please help
    By bigsmoke101 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 12th, 2011, 04:34 PM
  5. Help I'm stuck on code.
    By GreenM in forum Loops & Control Statements
    Replies: 3
    Last Post: January 29th, 2010, 09:27 PM