Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Newbie - If and if/else statements (http://www.velocityreviews.com/forums/t143050-newbie-if-and-if-else-statements.html)

raver 04-26-2005 10:55 PM

Newbie - If and if/else statements
 
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






Peter MacMillan 04-26-2005 11:18 PM

Re: Newbie - If and if/else statements
 
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@writeopen.com

Thomas G. Marshall 04-28-2005 12:19 AM

Re: Newbie - If and if/else statements
 
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.



Peter MacMillan 04-28-2005 12:29 AM

Re: Newbie - If and if/else statements
 
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@writeopen.com

Thomas G. Marshall 04-28-2005 12:43 AM

Re: Newbie - If and if/else statements
 
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."



Joona I Palaste 04-28-2005 05:18 AM

Re: Newbie - If and if/else statements
 
Thomas G. Marshall <tgm2tothe10thpower@replacetextwithnumber.hotmail. com> 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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am lying."
- Anon

Joona I Palaste 04-28-2005 05:19 AM

Re: Newbie - If and if/else statements
 
Joona I Palaste <palaste@cc.helsinki.fi> scribbled the following:
> Thomas G. Marshall <tgm2tothe10thpower@replacetextwithnumber.hotmail. com> 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 (palaste@cc.helsinki.fi) ------------- 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


All times are GMT. The time now is 09:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.