Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help on this.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help on this.

    I have these two problems I need to do for a class and am having the hardest time figuring out where to start on them.

    1. Write a program that prompts the user to enter a month and year. and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that of February 2000 has 29 days. (it also talks about remembering about Leap Year.)

    2. An ISBN consists of 10 digits d1d2d3d4d5d6d7d8d9d10. The last digit is a checksum, which is calculated from the other nine digits using the following formula:
    (d1 X 1 + d2 X 2 + d3 X 3 + d4 X 4 + d5 X 5 + d6 X 6 + d7 X 7 + d8 X 8 + d9 X 9) % 11
    If the checksum is 10, the last digit is denoted X according to the ISBN convention. Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros) Your program should red the input as an integer.

    I started a bit of number one, but have no idea where to even begin with 2. Help!

  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help on this.

    once you parse month and year as ints, 1) should go easily. I assume that 2000, as you say but without looking it up, is a leap year and ignore the adjustment that they throw in once in a great that's a bit more complicated than february just having 29 days every 4 years--i.e., i'll just assume that feb has 29 days every 4 years:

    if (month == 2) {
       if (year % 4 == 0) {
          System.out.print(29);
       else {
          System.out.print(28);
       }
    }
    else if (month <= 7) {
       if (month % 2 == 1) {
          System.out.print(31);
       }
       else {
          System.out.print(30);
       }
    }
    else {
       if (month % 2 == 1) {
          System.out.print(30);
       }
       else {
          System.out.print(31);
       }
    }
    There are also formulas out there for making interesting date calculations, but if you just need number of days in the month with no more complications than dealing with normal leap years, that should get you there.

    On 2) all you really need to realize is that (a + b) % n == ((a % n) + (b % n)) % n
    Last edited by aisthesis; February 16th, 2011 at 08:34 PM.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Help on this.

    About #1, Do you have to actually do this yourself or are you allowed to utilize JAVA like it is intended? I ask because there are JAVA standard classes in place to do this sort of thing for you. Specifically, the Calendar (Calendar (Java Platform SE 6)) or GregorianCalendar (GregorianCalendar (Java Platform SE 6)) classes.

    Example code to find the number of days in a month using the GregorianCalendar object is below:
    int month, year;
     
    //February
    month = 1;
    //Year 2000
    year = 2000;
     
    GregorianCalendar cal = new GregorianCalendar(year,month,1);
    System.out.println("There are "+cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH)+" days in this month");
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/