Newbie - If and if/else statements

Discussion in 'Java' started by raver, Apr 26, 2005.

  1. raver

    raver Guest

    Hi,

    I am having a go at learning java programming!

    For one of my projects i made a Date class & DateProgram class, Now i have
    got to add a public method called setDay()that takes in an int and returns
    void.

    The method should check the parameter to ensure that it is a valid day (1
    through 31).

    If it is, assign it to the member variable day.

    If it's not, display a message using System.out.println() stating that the
    given day is not valid.

    public void setDay(26)
    {
    int day(day > 1 && day < 31);

    return void;
    System.out.println("the day is not valid! " + day);
    }
    }

    Could some one tell me if this is even right! or give me little help!

    Thank you,
    Tony
     
    raver, Apr 26, 2005
    #1
    1. Advertising

  2. raver wrote:
    > Hi,
    >
    > I am having a go at learning java programming!
    >
    > If it's not, display a message using System.out.println() stating that the
    > given day is not valid.
    >
    > public void setDay(26)
    > {


    hmm. 26?

    > int day(day > 1 && day < 31);
    >


    Your logic is about right, but your syntax is wrong. Your logic would
    exclude 1 and 31 however. Try the >= (greater than or equal to) and <=
    (less than or equal to) operators.

    > return void;


    void is not a valid return value.

    > System.out.println("the day is not valid! " + day);
    > }
    > }
    >
    > Could some one tell me if this is even right! or give me little help!
    >
    > Thank you,
    > Tony
    >
    >


    I've intentionally used different names to give you a chance to think
    through what I've put together.

    public class myClass {
    private int instanceVariable;
    public void methodName(int parameter) {
    if ((parameter >= 1) && (parameter <= 31)) {
    instanceVariable = parameter;
    } else {
    System.err.println(day + " is not valid.");
    }
    }
    }


    Notice the lack of a return statement. I thought it worth mentioning
    that, for a method that returns void, you simply call "return;" (no
    quotes). Also, if "parameter" and "instanceVariable" have the same name,
    you can refer to "instanceVariable" as "this.instanceVariable".


    --
    Peter MacMillan
    e-mail/msn:
     
    Peter MacMillan, Apr 27, 2005
    #2
    1. Advertising

  3. Peter MacMillan coughed up:
    > raver wrote:
    >> Hi,
    >>
    >> I am having a go at learning java programming!
    >>
    >> If it's not, display a message using System.out.println() stating
    >> that the given day is not valid.
    >>
    >> public void setDay(26)
    >> {

    >
    > hmm. 26?
    >
    >> int day(day > 1 && day < 31);
    >>

    >
    > Your logic is about right, but your syntax is wrong. Your logic would
    > exclude 1 and 31 however. Try the >= (greater than or equal to) and <=
    > (less than or equal to) operators.
    >
    >> return void;

    >
    > void is not a valid return value.
    >
    >> System.out.println("the day is not valid! " + day);
    >> }
    >> }
    >>
    >> Could some one tell me if this is even right! or give me little help!
    >>
    >> Thank you,
    >> Tony
    >>
    >>

    >
    > I've intentionally used different names to give you a chance to think
    > through what I've put together.
    >
    > public class myClass {
    > private int instanceVariable;
    > public void methodName(int parameter) {
    > if ((parameter >= 1) && (parameter <= 31)) {
    > instanceVariable = parameter;
    > } else {
    > System.err.println(day + " is not valid.");
    > }
    > }
    > }


    *NOTE* to the OP (raver).

    This example was posted with hard tabs instead of spaces. Since you are
    using Outlook Express, chances are fairly good that you will see no
    indentation at all.

    This is a known pain in the ass bug in OE.

    Peter, there are soooo many users out there using OE, and that is not likely
    to stop soon. Please consider using spaces for tabs. They're (IMO) more
    commonly used anyway, but it's of course up to you.


    > Notice the lack of a return statement. I thought it worth mentioning
    > that, for a method that returns void, you simply call "return;" (no
    > quotes). Also, if "parameter" and "instanceVariable" have the same
    > name, you can refer to "instanceVariable" as "this.instanceVariable".




    --
    Unix users who vehemently argue that the "ln" command has its arguments
    reversed do not understand much about the design of the utilities. "ln
    arg1 arg2" sets the arguments in the same order as "mv arg1 arg2".
    Existing file argument to non-existing argument. And in fact, mv
    itself is implemented as a link followed by an unlink.
     
    Thomas G. Marshall, Apr 28, 2005
    #3
  4. Thomas G. Marshall wrote:
    >
    > *NOTE* to the OP (raver).
    >
    > This example was posted with hard tabs instead of spaces. Since you are
    > using Outlook Express, chances are fairly good that you will see no
    > indentation at all.
    >
    > This is a known pain in the ass bug in OE.
    >
    > Peter, there are soooo many users out there using OE, and that is not likely
    > to stop soon. Please consider using spaces for tabs. They're (IMO) more
    > commonly used anyway, but it's of course up to you.
    >
    >


    Wow, I had *no* idea. I wrote it directly in compose and, well... tab is
    easier to hit than 4 spaces. I'll keep that in mind in the future. I'm
    more used to hard tabs in programming because I once had a professor who
    would fail you if you didn't. Anal? yeah. *mumble grumble* :)

    --
    Peter MacMillan
    e-mail/msn:
     
    Peter MacMillan, Apr 28, 2005
    #4
  5. Peter MacMillan coughed up:
    > Thomas G. Marshall wrote:
    >>
    >> *NOTE* to the OP (raver).
    >>
    >> This example was posted with hard tabs instead of spaces. Since you
    >> are using Outlook Express, chances are fairly good that you will see
    >> no indentation at all.
    >>
    >> This is a known pain in the ass bug in OE.
    >>
    >> Peter, there are soooo many users out there using OE, and that is
    >> not likely to stop soon. Please consider using spaces for tabs. They're
    >> (IMO) more commonly used anyway, but it's of course up to
    >> you.

    >
    > Wow, I had *no* idea. I wrote it directly in compose and, well... tab
    > is easier to hit than 4 spaces. I'll keep that in mind in the future.
    > I'm more used to hard tabs in programming because I once had a
    > professor who would fail you if you didn't. Anal? yeah. *mumble
    > grumble* :)


    Not "anal". /Stupid/.

    Besides, I'm hard pressed to think up an editor that doesn't allow you the
    ability to have spaces input automatically when you press the tab key.

    But it gets worse than this. For reasons that are horribly convoluted, if
    you cut and paste from many IDE's out there, eclipse included last time I
    tested this (3.1M3?), you will get thrown out indents sometimes with OE
    unless you past first into microsoft word or even notepad and then CnP from
    there to OE.


    --
    "So I just, uh... I just cut them up like regular chickens?"
    "Sure, just cut them up like regular chickens."
     
    Thomas G. Marshall, Apr 28, 2005
    #5
  6. Thomas G. Marshall <> scribbled the following:
    > Peter MacMillan coughed up:
    >> Wow, I had *no* idea. I wrote it directly in compose and, well... tab
    >> is easier to hit than 4 spaces. I'll keep that in mind in the future.
    >> I'm more used to hard tabs in programming because I once had a
    >> professor who would fail you if you didn't. Anal? yeah. *mumble
    >> grumble* :)


    > Not "anal". /Stupid/.


    I agree. *My* professor for fail students for not using *any*
    indentation, or using nonsensical indentation like this:

    if (some_condition)
    {
    do_something();
    }
    else
    {
    do_some_other_thing();
    }

    but as long as the indentation made sense, and was consistent, he
    couldn't care less if it was in tabs or spaces.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "I am lying."
    - Anon
     
    Joona I Palaste, Apr 28, 2005
    #6
  7. Joona I Palaste <> scribbled the following:
    > Thomas G. Marshall <> scribbled the following:
    >> Peter MacMillan coughed up:
    >>> Wow, I had *no* idea. I wrote it directly in compose and, well... tab
    >>> is easier to hit than 4 spaces. I'll keep that in mind in the future.
    >>> I'm more used to hard tabs in programming because I once had a
    >>> professor who would fail you if you didn't. Anal? yeah. *mumble
    >>> grumble* :)


    >> Not "anal". /Stupid/.


    > I agree. *My* professor for fail students for not using *any*
    > indentation, or using nonsensical indentation like this:


    Typo: I meant "...would fail students".

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "'It can be easily shown that' means 'I saw a proof of this once (which I didn't
    understand) which I can no longer remember'."
    - A maths teacher
     
    Joona I Palaste, Apr 28, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Devin Panchal

    if and else statements.

    Devin Panchal, Dec 9, 2003, in forum: Java
    Replies:
    2
    Views:
    444
    Devin Panchal
    Dec 9, 2003
  2. Harry George
    Replies:
    6
    Views:
    391
    Bart Nessux
    Feb 23, 2004
  3. tobiah
    Replies:
    22
    Views:
    574
    Ben Finney
    Sep 1, 2006
  4. Replies:
    5
    Views:
    634
    David T. Ashley
    Jan 15, 2007
  5. John Crichton
    Replies:
    6
    Views:
    267
    John Crichton
    Jul 12, 2010
Loading...

Share This Page