Some Clarification

  • Thread starter Hypnos_16
  • Start date
  • #26
298
2
The following is the only problem I still see in your code (congrats!). As I said a few times, it's not needed, nor is it used. The month number is enough to define the entire state of the object.
Code:
	private String monthName;
Hypnos_16 said:
Alright! hopefully now, this is good. Now the testing of this is kind of tricky, because the way my prof wants it done is that she can input the variables ie. Month name and number. so i know i use the
Code:
import java.util.Scanner
but i'm having some issues with writing the body of it.

Hm, let's see. Is this right? She wants to be able to choose whether to input the month name OR the month number. Then, she wants that to be set in a Month object, and then to see printed out the current value of the Month object?

Hypnos_16 said:
i tried the one that was given here, just to make sure everything worked well

Code:
public class MonthTester {
	public static void main(String [] args) {
        	boolean testOk = true;
        	Month aMonth = new Month("mArCh");
		System.out.println("Month(String) constructor and getMonthNumber() test: ");
       		if (aMonth.getMonthNumber() == 3)
            		System.out.println("OK");
        	else
           		System.out.println("FAIL!");
            		testOk = false;
        	// Did any of the tests fail?
        	if (testOk)
            		System.out.println("All tests passed.");
        	else
            		System.out.println("One or more tests failed!");
        
	}
}
i got an answer of "OK" but also that "One or more tests failed!" i hope it's only a minor error, but my Terminal isn't saying anything is "wrong" with either program
Ah! Best we clear up this problem. It's really important and a common source of errors. It's the reason I generally use curly braces for the body of if statements. I made an exception there, and see what happens? lol

If you don't put in curly braces, only the next single line will be part of the if (or else, for loop, while loop, etc). The problem is, in the else clause, you need those braces because you have 2 lines in there. Remember, indentation is for readability. The compiler doesn't care one bit. What you did was really this:
Code:
       		if (aMonth.getMonthNumber() == 3)
            		System.out.println("OK");
        	else
           		System.out.println("FAIL!");
            	testOk = false;
So testOk is always set to false. I would always use curly braces to avoid these kinds of errors. I've seen even experienced developers make that mistake. I would always write it this way, personally:
Code:
       		if (aMonth.getMonthNumber() == 3)
                {
            		System.out.println("OK");
        	} else {
           		System.out.println("FAIL!");
            	        testOk = false;
                }
Of course, this is also acceptable:
Code:
       		if (aMonth.getMonthNumber() == 3)
            		System.out.println("OK");
        	else
                {
           		System.out.println("FAIL!");
            	        testOk = false;
                }
 
  • #27
153
1
FANTASTIC!
THANK YOU! FINALLY GOT IT TO WORK PROPERLY.
JUST GOT TO REVIEW IT A COUPLE TIMES TILL IT ALL MAKES SENSE.
I appreciate the help and effort you put in to helping me. Thank you again.
 
  • #28
298
2
Excellent! Yeah, reviewing it is well worth it. There's a lot of good stuff in that class.

And you're welcome.
 

Related Threads on Some Clarification

Replies
7
Views
747
Replies
9
Views
1K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
2
Views
874
  • Last Post
Replies
1
Views
1K
Replies
4
Views
558
Replies
3
Views
674
Replies
2
Views
1K
Replies
7
Views
2K
Replies
1
Views
801
Top