Newbie - If and if/else statements

R

raver

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
 
P

Peter MacMillan

raver said:
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".
 
T

Thomas G. Marshall

Peter MacMillan coughed up:
hmm. 26?


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.


void is not a valid return value.


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.
 
P

Peter MacMillan

Thomas said:
*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* :)
 
T

Thomas G. Marshall

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/.

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.
 
J

Joona I Palaste

Thomas G. Marshall said:
Peter MacMillan coughed up:
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.
 
J

Joona I Palaste

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 ([email protected]) ------------- 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top