tags:

views:

108

answers:

5

I must create a program in which the output would show the season accroding to these ranges:

  • 12/16-3/15 is winter
  • 3/16-6/15 is spring
  • 6/16-9/15 is summer
  • 9/16-12/15 is fall

Here is my code thus far:

public class season1 {
  public static void main(String[] args) {
      System.out.println("Between 12/16 and 3/15 is " + season());
      System.out.println("Between 3/16 and 6/15 is " + season());
      System.out.println("Between 6/16 and 9/15 is " + season());
      System.out.println("Between 9/16 and 12/15 is " + season());
  }

  public static int season(int a, int b){
      if (x=12 a <= 16 && b >= 15) (x=3 b>=15)){
         return "winter";
      }
      else if (

  if (x=3 a <=16 && b>=15) (x=6 b>=15)){
   return "spring";
  }

  if (x=6 a <=16 && b>=15) (x=9 b>=15)){
   return "summer";
  }

  if (x=9 a <=16 && b>=15) (x=12 b>=15)){
   return "fall";
  }
 }

 }
+1  A: 

Well, I guess you may misinterpretered several things:

First, your program would expect an date entry and, based on its month and its date in month, would then respond with which season it belongs to...

Also, your code won't compile, for instance, the season() method is invalid as in your method declaration, season expects two arguments.

Okay, I think for homework, a pseudo code would be better:

Part 1: find a way to accept user entry from keyboard (standard input). Reject faulty entries with error message.

Part 1 (bonus): validate the entries to make sure there are only two integers, one for month, the other for date in month.

Part 2: use a method to check the two integers passed in its argument, based on the requirement, return the corresponding string to indicate the season. Then output it to terminal.

Edit:

No, you don't need a while loop, simply run the programming from command-line like this:

seasonCalculation 3 14

which should output "Spring" as the answer.

But please double check this with your actual requirements.

Michael Mao
I got 22 compile errors. I just need to have the output telling me what season it is based on the dates given. I know I have to put two parameters in there for both the day and the month. I just don't know how to implement it on the code.
Makurian1
So do I delete everything from the main and start over using while loops?
Makurian1
Sorry, I actually have to return a String indicating the season for the day and the month assuming that the month is an integer between 1 and 12 and the days are numbered between 1 and 31. So if the date falls between 6/16-9/15, it should return summer and so on. I don't think I have to accept user entry I just need to return the season.
Makurian1
+2  A: 

You provided code- that's a start, and much better than most of these homework questions. But you didn't provide a question, which I'm going to assume is "why won't my code compile?" because this won't. Your logic is a good start- except that you're using an undefined x variable- but you have several problems with the syntax:

  • When you call a method, you have to pass the parameters that are defined for that method. season() is defined with 2 int parameters, so it won't work without them: season(x, y)
  • There is a difference between = (assignment) and == (equality). The former sets the value of a variable, while the latter checks to see if two values are equal. For if conditions, you need to use equality so that the expression will evaluate to true or false.
  • You got logical AND && correct, but forgot logical OR ||. You need to put || between two conditions if you want to check for either one of them.
  • Finally, check for balanced braces. For each { there must be exactly one } or you will get errors. If you're using an IDE like Eclipse it should help you by highlighting matching pairs.

Those tips should point you in the right direction. You're well on your way, you just need to get the syntax correct.

Travis Christian
+2  A: 

There are many problems with your code. I suggest you see a sample java code and how it works, for example:

  1. You are calling season() in your main() method, but season method actually also takes in 2 int parameters for example, you should be calling: season(12, 3).

  2. Understand how if..else works. For example, this is your first if:

    if (x=12 a <= 16 && b >= 15) (x=3 b>=15)) { return "winter"; }

Now, see the problems:

2.1. What does x=12 a<=16 mean, can you make sense out of it?
2.2. What is happening between (x=12 a....) (x=3 ...)?
2.3. You have not declared x any where, i.e. you havn't written int x any where, so how can Java understand what type x really is?
2.4. You are returning "winter" which is a string, where your method season(int a, int b) has a return type of int. That will never work out. You need a String return type.

Let me give you an example of if..else: Say: a=12 and b=3 where a and b are months (not taking days into consideration and no data validation), so the return should be winter

public static String season(int a, int b)
{
   if((a>=12) && (b<=3))
   {
     return "Winter";
   }
}

Do not expect the exact answer here. We can just help you understand the objective. You will need to code.

zengr
Thank you for the explanation. I know where to start over now.
Makurian1
Please accept the answer which you feel answers your question. :)
zengr
A: 

import java.io.*;

class Season { public static void main(String []args) throws IOException { int month, day; BufferedReader brk = null; // To read input from keyboard. int s; String []season = {"Winter", "Spring", "Summer", "Fall"};

    brk = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Enter the day: ");
    day = Integer.parseInt(brk.readLine());
    System.out.print("Enter the month: ");
    month = Integer.parseInt(brk.readLine());

//Value of s = 0: winter, 1: spring, 2: summer, 3: fall. if((month % 3) == 0) { if(day <= 15) { s = (month / 3) - 1; } else { s = month / 3; s = s % 4; } } else { s = month / 3; }

    System.out.println(season[s]);  
}

}

theorko
You need to edit this so that it is at least formatted correctly. Also, giving the code to solve a homework question doesn't help the poster learn. Consider offering tips or implementation suggestions next time.
Anna Lear
A: 

How come the alignment of the program turned out so bad? I just copied the program from gedit, where it looked great?

Any suggestions regarding how to get the alignment right?

theorko
syntax is all wrong and this post should be a comment, not an answer.
zengr
You need to indent all the code you pasted in (select it all and press Ctrl+K or use the button at the top of the answer textbox) in order for it to be properly formatted.
Anna Lear