0

I am a beginner in Java and I have an assignment that i need to find bugs in the code written below and i have trouble getting started. I have no idea how to approach this or how to start looking for bugs.

public class DayOfYear {

    public static int dayOfYear(int month, int dayOfMonth, int year) {
        if (month == 2) {
            dayOfMonth += 31;
        } else if (month == 3) {
            dayOfMonth += 59;
        } else if (month == 4) {
            dayOfMonth += 90;
        } else if (month == 5) {
            dayOfMonth += 31 + 28 + 31 + 30;
        } else if (month == 6) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31;
        } else if (month == 7) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30;
        } else if (month == 8) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31;
        } else if (month == 9) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
        } else if (month == 10) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
        } else if (month == 11) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
        } else if (month == 12) {
            dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
        }

        return dayOfMonth;
    }
}

Don't know how to get started.

  • 2
    Call the method with arguments, assert the return value is correct. Think of edge cases. Consider if there should be argument validation. – Slaw Apr 12 '23 at 15:55
  • What Slaw is saying is basically "unit testing" (look that up). By trying different inputs with your code and testing the output for an expected result, you can find and correct many problems with your code. – markspace Apr 12 '23 at 15:57
  • And in addition to using a debugger (the duplicate link), putting print statements in your code to test values calculated along the way can also be very valuable. Personally I find using print statements to be faster than using a debugger. (Although sometimes a debugger is necessary.) Once done debugging, consider turning some of your debug print statements into logging calls. – markspace Apr 12 '23 at 15:58
  • My apologisies i phrased my question kind of wrong, the problem is that I am having a hard time thinking of ways to test the output. – Samuel Karim Apr 12 '23 at 15:59
  • 2
    Do you think the `year` argument could influence the expected result sometimes? – teapot418 Apr 12 '23 at 15:59
  • Both using a debugger and Slaw's comment are excellent ways of testing output, Samuel. – markspace Apr 12 '23 at 15:59
  • @teapot418 i would assume so? – Samuel Karim Apr 12 '23 at 16:05
  • Having trouble coming up with adequate @test "questions" to ask in order to find the bugs – Samuel Karim Apr 12 '23 at 16:06
  • One way to find test question is to do branch flow analysis. Basically look at all the `if` statements in your code, and make sure to test for both the case where the statement is true, and the statement is false. For example, the first `if` tests for `month` equals 2. Make sure to test for both 2 and for month being something else. There's two tests (at least) that you can run right now. – markspace Apr 12 '23 at 16:15
  • Another way to find test cases is to test for beginnings and ends. Often you will calculate incorrectly when you are doing things at the extreme edges of your code. The very start of a range (for example, month = 1, day = 1, and year = ... not sure, what's the minimum year this will work for? start with 1900 for now). Likewise the very end cases (month = 12, day = 31, year = ... again, nothing defined here, pick something). You also want to test just above and below those ranges for an appropriate result (for example throwing an error if month = -1). – markspace Apr 12 '23 at 16:19
  • the assignment requires to do the testing from a separate testclass – Samuel Karim Apr 12 '23 at 16:26
  • So? Call the method from a separate class. That won't change anything I wrote. – markspace Apr 12 '23 at 16:38
  • @markspace could you give a code example, I apologise i'm very new to java and this assignment is difficult for me. – Samuel Karim Apr 12 '23 at 16:42
  • I can't because the question is closed. If you don't know how to call a method from another class, I think you need to talk to your instructor, there's something very basic you are missing. – markspace Apr 12 '23 at 17:25

0 Answers0