• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Is this the only solution?

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys have this exercise to do:

Extra programming questions (to if statement)

1.Write a Java program that reads in a month number and outputs the number of days in that month.

Month                                          Number of Days
1, 3, 5, 7, 8, 10, 12                                31 days
4, 6, 9, 11                                                30 days
2                                                        28 days

now i know i could produce an if else if statement for every month 1 to 12

but i looked up if and could use an || or between 1 3 5 7 8 10 12
and same for 4 6 9 11
and 2 on its own

we havent covered the || statement at all and was wondering if there was any other handy way to resolve this
a simple push in the right direction would be suffice

thanks

 
author & internet detective
Posts: 40726
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
I've moved this to the "Beginning Java" forum. (Introductions is just for saying hi).

As far as your question, did you learn about switch statements yet? If so, that's a good solution too.
 
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . or you can use an array with the number of days in. A far better solution might be to go through the Java™ Tutorials and find the enum aboiut months.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks campbell will keep that in mind...we arent anywhere near arrays yet lol
 
Sheriff
Posts: 16578
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're not going to use if-statements, you can also use a switch statement. Still kind of the same thing though. I can write a switch statement with 8 lines of code, including the closing brace to do this, assuming your input is valid, of course. Add a few more lines of code to validate the input month.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you're not going to use if-statements, you can also use a switch statement. Still kind of the same thing though. I can write a switch statement with 8 lines of code, including the closing brace to do this, assuming your input is valid, of course. Add a few more lines of code to validate the input month.



hi Junilu yes i have used switches a lot...these exercises were specifically for if statements...im in college first year following the criteria...thanks
 
Junilu Lacar
Sheriff
Posts: 16578
277
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:we havent covered the || statement at all and was wondering if there was any other handy way to resolve this
a simple push in the right direction would be suffice


jon ninpoja wrote:yes i have used switches a lot...these exercises were specifically for if statements...im in college first year following the criteria...thanks


You wanted to know if there was another way, but then you don't  
Maybe it'll be easier to help you if you make up your mind what you want...  
 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use the Java Time API?  From the Date Classes Trail it's a reasonably simple way to get the number of days for a given month, without the need to resort to if-then-else logic.  

Respectfully,
Robert
 
Campbell Ritchie
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In real life, you would use something like the Month enum, but beginning programming classes aren't real life. They make you struggle and count days.
 
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way you can do it is by making 2 array lists of type integer. One with numbers 1 to 12, and one with 12 indices and the number of days for each month.

Then you can read and input, get the index of that item from the first list, and output the equal index-1 on the 2nd list.



That's a lot less work than all those if else statements, because there is a way to add multiple values to the list at a time too.
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my post above, forget the index-1 part, because you are taking the index from the first list, and want it to be equal to the index in the second.  I was thinking about it the wrong way.
 
Marshal
Posts: 8049
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

Another way is to create a Map<Integer, Integer> and do the mapping for each month. Anyway, you have February days fixed as 28 in your requirements, while actually can be a different number. What are you going to do about that?
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:@OP

Another way is to create a Map<Integer, Integer> and do the mapping for each month. Anyway, you have February days fixed as 28 in your requirements, while actually can be a different number. What are you going to do about that?



Maps work too, but I seem to be MapTose intolerant as I have a lot of difficulty using them and have since day 1.
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:@OP

Anyway, you have February days fixed as 28 in your requirements, while actually can be a different number. What are you going to do about that?



I honestly don't think that assignment is really calling for anything that tedious.  It seems as if the instructions say to output 28 for February and not account for leap year.
 
Liutauras Vilda
Marshal
Posts: 8049
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:I honestly don't think that assignment is really calling for anything that tedious.  It seems as if the instructions say to output 28 for February and not account for leap year.


Maybe. But then for such submitted assignment I'd allow to get only 69% and 100% would be only for those who think about the corner (if we even can call it a corner case) cases. Because programming is really about that - so the programs work and produce correct output, which wouldn't be the case here.

Other talk would be if the instructions *explicitly* tell February has 28 days. That would be more disappointing of course. Unless the next assignment asks: "find the bug and fix it". But really it could be just one.
 
Campbell Ritchie
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . I seem to be MapTose intolerant . . .

The correct medical term is Mactose Intolerance.

Java™ Tutorials links: 1 2.You can simply write the number, and it will be boxed in lines 2‑13. Line 17 is unboxing a null, so you should get an exception there.
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kenneth Milota wrote:. . . I seem to be MapTose intolerant . . .

The correct medical term is Mactose Intolerance.

Java™ Tutorials links: 1 2.You can simply write the number, and it will be boxed in lines 2‑13. Line 17 is unboxing a null, so you should get an exception there.




The assignment he posted doesn't require storing the names of the months.  Just the month number.
 
Liutauras Vilda
Marshal
Posts: 8049
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:The assignment he posted doesn't require storing the names of the months.  Just the month number.


I don't think Campbell meant to show this as an assignment solution. He was showing to you how to use it so the symptoms of mentioned disease would fade away.
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed I duplicated the variable name and the ArrayList name, and I don't know if java allows that, but I don't have a compiler on me.
 
Liutauras Vilda
Marshal
Posts: 8049
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the poor formatting and some other practices, I'd say it is misleading code and way more difficult to understand than it could be. Look what a complicated way to synchronise indices between the two lists.
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was more for the reason of sharing a different idea than a map or if/else statements.  
 
Campbell Ritchie
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:. . . I duplicated the variable name and the ArrayList name, and I don't know if java allows that . . .

I hadn't noticed. You shouldn't need a compiler to know whether you are or are not allowed two local variables with the same name in the same scope. Maybe the Java® Language Specification (=JLS) will help.
 
Liutauras Vilda
Marshal
Posts: 8049
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kenneth Milota wrote:It was more for the reason of sharing a different idea than a map or if/else statements.  


yeah, that's fine
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kenneth Milota wrote:. . . I duplicated the variable name and the ArrayList name, and I don't know if java allows that . . .

I hadn't noticed. You shouldn't need a compiler to know whether you are or are not allowed two local variables with the same name in the same scope. Maybe the Java® Language Specification (=JLS) will help.



I didn't think so, I just noticed after posting it that it didn't occur to me that I named 2 different types the same name, but I never tried that before so I wasn't certain.
 
Campbell Ritchie
Marshal
Posts: 73767
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yesterday, I wrote:. . . you should get an exception there.

. . . but I was mistaken. Sorry. I shall let you find out what actually happens.
 
reply
    Bookmark Topic Watch Topic
  • New Topic