Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Homework question

  • 16-07-2013 9:27pm
    #1
    Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭


    Question 1.public boolean validDate (int day, int month, int year)
    The month can only be in the range 1-12 and a month must have the correct
    number of days eg 30.2.1997 is invalid because February can only have 28 days, or 29 days if the year is a leap year.This method must return true if the date is valid and false if the date is .invalid

    I know im not far off, but all im getting is red lines on Eclipse. Can anyone help.
    package oop;

    public class Assignment {


    // TODO Auto-generated method stub

    public static boolean is valid (int n);
    if (n >0 && n < 13){
    return false
    {else}
    return false

    public static boolean validDate (int day, int month, int year);
    if (day > 32 ¦¦ month > 12){
    return false;

    }

    if(month == 2 && (day > 28 && ! leap Year())){
    return false;


    }

    if ((month == 9 ¦¦ month == 4 ¦¦ month == 6 ¦¦ month == 11))&& day > 30){
    return false;
    {else}
    return true


Comments

  • Registered Users, Registered Users 2 Posts: 1,082 ✭✭✭Feathers


    Question 1.public boolean validDate (int day, int month, int year)
    The month can only be in the range 1-12 and a month must have the correct
    number of days eg 30.2.1997 is invalid because February can only have 28 days, or 29 days if the year is a leap year.This method must return true if the date is valid and false if the date is .invalid

    I know im not far off, but all im getting is red lines on Eclipse. Can anyone help.


    You're in the wrong thread mate — this is for people to complain about bad code other people have written that they've had to interact with ;)

    If you start a new thread, I'm sure someone would be happy to help, so long as you tell people what you've tried so far & specifically what errors you're getting.


  • Closed Accounts Posts: 1,235 ✭✭✭returnNull


    not sure what language it is but this doesn't look right.Check your curly brackets
    if ((month == 9 &#166;&#166; month == 4 &#166;&#166; month == 6 &#166;&#166; month == 11))&& day > 30){
    return false;
    [B]{[/B]else[B]}[/B]
    return true
    


  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    I'd have guessed this was Java due to Eclipse,the package keyword and it's homework. If that is the case your syntax is all over the place and there is very good reason you are getting all those red lines. So assuming it is Java:

    Your methods should be of the form:
    public static boolean isvalid (int n)
    {
        // your code contained in here
    }
    

    Note the first line declaring your method does not end in a semi colon and the method name cannot have spaces in it. The logic contained within the method needs to be inside the curly brackets.

    I also don't see where you defined the leapYear() method and this method would need to have the year passed to it and not have a space in the name either.

    Also even though it is not in the homework question you probably want to create a main() method where you enter a date and it uses your validDate method to check if it is valid. In this way you will know if your method functions correctly.


  • Registered Users, Registered Users 2 Posts: 291 ✭✭Seridisand


    Question 1.public boolean validDate (int day, int month, int year)
    The month can only be in the range 1-12 and a month must have the correct
    number of days eg 30.2.1997 is invalid because February can only have 28 days, or 29 days if the year is a leap year.This method must return true if the date is valid and false if the date is .invalid

    I know im not far off, but all im getting is red lines on Eclipse. Can anyone help.

    Your code is pretty much all over the place:
    public static boolean is valid (int n);
    if (n >0 && n < 13){
    return false
    {else}
    return false
    Your Logic here(ignoring the syntax errors) is that you can either evaluate as false or false ??

    You could implement the other method something like this:
    public static boolean validDate (int day, int month, int year){
     if(month != 2 && day == 31 || day == 30){
        return True;
     }
    else if(month == 2 && day == 28 || day == 29){
           return true;
    }
    else {
        return false;
    }
    }
    

    It looks like you only posted some of the code, I'd suggest posting all of it if your still stuck.


  • Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭The_Kew_Tour


    Cheers guys. I am really struggling with this stuff even though I have put some time into it. Thanks for help and I seem to be getting somewhere now


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 586 ✭✭✭Aswerty


    Feel free to throw up another iteration once you feel you've gotten a bit further along.

    I think everyone no matter how well they took to programming wrestled with mismatched squigly brackets and missing semi colons at the begining. Or even wishing your code into doing what you want instead of logically planning out each individual step!


  • Registered Users, Registered Users 2 Posts: 34,460 ✭✭✭✭The_Kew_Tour


    Aswerty wrote: »
    Feel free to throw up another iteration once you feel you've gotten a bit further along.

    I think everyone no matter how well they took to programming wrestled with mismatched squigly brackets and missing semi colons at the begining. Or even wishing your code into doing what you want instead of logically planning out each individual step!

    I have just put up another thread "Homework question again" on here and it goes bit further hopefully.

    I really am struggling with this. The rest of course so far has been fine, but have assignment Friday week on this stuff and finding it tough going. I am trying even if getting knowwhere


  • Closed Accounts Posts: 1,235 ✭✭✭returnNull


    Another tip is compile your code regularly,that way you might only have one or 2 errors to fix whereas if you write 40 lines you'll think your pc is about to explode with the amount of errors :)


Advertisement