UW CSE logo uw

CSE 142: Computer Programming I, Winter 2008

arrow CSE Home arrow About Us arrow Search arrow Contact Info

Homework 4 (Birthdays) FAQ

What should it do if the two people have the same birthday?
Can my main method contain ...?
Is it okay to have 20 if-else statements?
How do I use Scanner? How do I do returns?
I'm returning my variable. Why can't main see it?
How do I return two things from a method?
How do I round a percentage to one decimal point?

Q: What should it do if the two people have the same birthday?
A: It should print, "Wow! You share the same birthday!"
Q: Can my main method contain ...?
A: The training wheels are coming off, so you can now use println statements and take user input inside the main method! However, the main method should still be somewhat short and should not contain too large a share of your overall code. The main method should represent a reasonable summary of the overall program's execution. In other words, you can stretch out a little now, but remember that the main method should still be concise and summarize the program's execution.

It's a little scary to be given extra feedom but not know where the boundaries are, which is fine, because the boundaries are a little fuzzy anyway. Take faith in the specs and guidelines given to you: part of programming is learning to make design decisions based on those guidelines.
Lastly, do not ask questions like "Can my method have 4 println statements and 2 if statements?". The above is all you need to know.
Q: Is it okay to have 20 if-else statements?
A: In general, no. If you're using a huge nested if/else with ten branches or something, you probably aren't solving the problem the way we intended. Some large if/elses are better implemented with a loop, such as a cumulative sum.

When you do need an if/else that enumerates many cases, you should use logical operators to reduce and condense any redundant conditions into one larger conditional. For example:
if (x == 1) {
    System.out.println("X is 1 or 3 or 5");
} else if (x == 3) {
    System.out.println("X is 1 or 3 or 5");
} else if (x == 5) {
    System.out.println("X is 1 or 3 or 5");
} else if (x == 4) {
    System.out.println("X is 4");
} else {
    System.out.println("X is not 1, 3, 4, or 5");
}

This turns into:
if (x == 1 || x == 3 || x == 5) {
    System.out.println("X is 1 or 3 or 5");
} else if (x == 4) {
    System.out.println("X is 4");
} else {
    System.out.println("X is not 1, 3, 4, or 5");
}

For more in-depth examples, consult the textbook or lecture slides.
Q: How do I use Scanner? How do I do returns?
A: Look at the examples in the textbook. For examples of Scanner, look at the "case study" examples from Chapters 3 and (especially) 4. The case study in Ch. 4 is also good for understanding returns, with its getBMI and bmiFor methods (and examining how they are called and used from main).

You could also look at the lecture examples such as Factors2.
Q: I'm returning my variable. Why can't main see it?
A: Just returning a variable from your method doesn't mean main now can refer to that variable. The main must store the result into a variable and then examine that variable. An analogy: If you ask someone to go get you groceries, you still have to accept and "use" it when they come back. The groceries don't put themselves away; you have to put them somewhere.
Q: How do I return two things from a method?
A: You can't! You need to organize your program so you only need to return one thing from each method.
Q: How do I round a percentage to one decimal point?
A: You can either modify the round2() method presented in lecture or figure out how to use printf() to do what you want it to.