Reputation: 501
I am a beginner in the java programming language. I wasn't able to write the code for this program because I am not an expert in boolean operators. I just wanted to know how you people out there would program this question in the book because I am unable to figure out how to get this program working without the if/else branch. Help would be appreciated, sorry if you guys are annoyed with this question.
Question in book,
Write a program that asks the user to enter a month (1 for January, 2 for February, etc.) and then prints the number of days in the month. For February, print “28 days”.
Enter a month: 5
30 days
Use a class Month with a method public int getLength() Do not use a separate if/else branch for each month. Use Boolean operators.
Thanks!
*I dont know how to use switch statements, I just want to be able to do it like how it says it in the book,
Thanks
Upvotes: 0
Views: 2238
Reputation: 388
I whould do something like this:
public class Month
{
int month;
public Month(int _month)
{
this.month = _month;
}
public int getLength()
{
if(this.month == 2) { return 28 }
if(this.month<8)
{
if((this.month%2) == 1)
{
return 31
}
else
{
return 30
}
}
else
{
if((this.month%2) == 1)
{
return 30
}
else
{
return 31
}
}
} }
EDIT. After reading the updated question from the book i think they are looking for something like this.
public int getLength()
{
if(this.month == 2) {return 28;}
if(this.month == 1 || this.month == 3 || this.month == 5 || this.month == 7 || this.month == 8 || this.month == 10 || this.month ==12){ return 31;}
if(this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11){return 30;}
}
But the answers other have given is better in a real life situation.
Upvotes: 1
Reputation: 234795
Assuming you don't need to deal with leap years, your Month
class might look something like this:
public class Month {
private int monthNumber;
public Month(int monthNumber) {
if (monthNumber < 1 || monthNumber > 12) {
throw new IllegalArgumentException(
"Month number must be between 1 and 12");
}
this.monthNumber = monthNumber;
}
public int getLength() {
return monthLengths[monthNumber - 1]; // indexes start at 0
}
private static int[] monthLengths = {
31, // January
28, // February
31, // March
. . .
}
}
The rest of the code (prompting the user, getting input, error checking, printing the answer) is left as an exercise. :)
P.S. I can't imagine where Boolean
enters into this at all.
Upvotes: 2
Reputation: 124215
If you want to do something for specified numbers you can either use something like
if ( number == 1 ){
doSomething();
} else if ( number == 3 ){
doSomething();
} else if ( number == 5 ){
doSomething();
}
But since this approach is forbidden
Do not use a separate if/else branch for each month.
Use Boolean operators.
you need to use boolean OR ||
operator like
if (number==1 || number == 3 || number == 5){
doSomething();
}
Now try to use this for months.
Upvotes: 2
Reputation: 403
Use a map with the key being the integer value input from the user and the value being the number of days in that month. For example:
Hashmap<Integer, Integer> map = new Hashmap<Integer, Integer>();
map.put(1, 31);
...
map.put(12, 31);
Then ask for input and do something like:
int input = ...;
if (map.containsKey(input)) {
System.out.println(map.get(input));
}
else {
System.out.println("Invalid month input");
}
Upvotes: 0