Two Laptop Bag*
The moose likes Java in General and the fly likes Array Magic ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array Magic !" Watch "Array Magic !" New topic
Author

Array Magic !

Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Hello Everybody

I wrote a code for an exercise which return the number of vacation days upon the employee experience for instance:
0 to 1 year (10 days)
1 to 3 years (15 days)
etc until 6 years and over with 25 days.

The idea of the exercise is to use an array as the number of days.

My concern is that if I use the Switch instead of If/If else the code does not work properly.

The Vacation Class




The Test Class




the setter method with if/else statement (which actually works)



Thank You
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, the logic of the switch statement and if statement are completely different--you're doing entirely different things.

I'm not sure what the array is for, though--why not just return the number of days?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Faiz:

Why not just use an enum for this?

John.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
David Newton wrote:Well, the logic of the switch statement and if statement are completely different--you're doing entirely different things.

I'm not sure what the array is for, though--why not just return the number of days?



Hello David,


Well the purpose of the exercise is to use an array to store the number of days like the following:
emplacement[0]=10;
emplacement[1]=10;
etc..

What I have to do to make my switch code works?

Thanks
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
John de Michele wrote:Faiz:

Why not just use an enum for this?

John.



Hello John,

We worked this example in class and I tried it using if statement and now i am trying to use switch instead, just to see the difference.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

At the very least, add the array entries missing--do you see that the values and indices you're using in the two methods are very different?
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
David Newton wrote:At the very least, add the array entries missing--do you see that the values and indices you're using in the two methods are very different?


in my code the number after the case keyword refers to the years of experiences, and the number added to the array is the allowed days off.
I know I am skipping something here :banghead:

Thanks David.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Faiz:

Well, it's pretty clear that nbVacation should not be an array. At least, not the way you're using it.

John.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
John de Michele wrote:Faiz:

Well, it's pretty clear that nbVacation should not be an array. At least, not the way you're using it.

John.


Indeed, John. It's just an useless classroom exercise for a specific purpose about arrays.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Faiz Abdelhafid wrote:
David Newton wrote:At the very least, add the array entries missing--do you see that the values and indices you're using in the two methods are very different?


in my code the number after the case keyword refers to the years of experiences, and the number added to the array is the allowed days off.
I know I am skipping something here :banghead:

Thanks David.



I sort it out like this

The Setter Method





And the code in the Main Class





To make it work the numbers should match
3 years as parameter in the getter means the value of 20 days in the setter.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Faiz Abdelhafid wrote:
The idea of the exercise is to use an array as the number of days.

My concern is that if I use the Switch instead of If/If else the code does not work properly.



Were you actually told that you had to initialise your array using an if/else statement or a switch statement ?
Your setVacation method could easily be written as

or not bother with a setVacation method at all and initialise your array when you declare it


Joanne
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Joanne Neal wrote:
Faiz Abdelhafid wrote:
The idea of the exercise is to use an array as the number of days.

My concern is that if I use the Switch instead of If/If else the code does not work properly.



Were you actually told that you had to initialise your array using an if/else statement or a switch statement ?
Your setVacation method could easily be written as

or not bother with a setVacation method at all and initialise your array when you declare it



Hello Joanne,

Yes the idea was to put the validation inside the setter. Is it possible to put it in the constructor? because in my example I used it to create a new instance of the array without overwriting the previous one.
I guess I have to try alternative solutions and see the difference.

Faiz.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Are you trying to do this:

vacation class:



tester class:



SCJP 1.6 96%
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Neha Daga wrote:Are you trying to do this:

vacation class:



tester class:




Hi Neha,

This example assumes that the student did not know the I/O API yet. I know it's more realistic when you prompt the user for a value.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
so, apart from i/o usage is it what you were trying to do? because those 3 lines you can change anyways.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Neha Daga wrote:so, apart from i/o usage is it what you were trying to do? because those 3 lines you can change anyways.



I was trying to use the switch instead of if/else statement in the setter.

Thanks
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
thats what i have done.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Neha: please be wary of posting full solutions. In general, we really strive not to do that, to allow the person asking the question to develop their own solution. It's almost always more educational that way.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
ok, I did not think that way. I am sorry, will keep it in mind.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
David Newton wrote:@Neha: please be wary of posting full solutions. In general, we really strive not to do that, to allow the person asking the question to develop their own solution. It's almost always more educational that way.


Hi David

I guess it was not her intention because the whole example is ambiguous and I admit that it's not easy from the first approach.

Best Regard.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I guess it was not his intention


hey, I am a girl.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Neha Daga wrote:
I guess it was not his intention


hey, I am a girl.


Oupppps ok Neha sorry for the error.

Nice to meet you.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
same here.
Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
There is also a very simple and short way to get this exercise done.
put the validation inside the constructor




Faiz Abdelhafid
Ranch Hand

Joined: Nov 21, 2009
Posts: 32
Another Alternative

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39833
    
  28
Sounds like something which can be done with simple arithmetic, Math.min(25, 10 + years / 2 * 5). Then you don't need the arrays, switch or anything.
 
Don't get me started about those stupid light bulbs.
 
subject: Array Magic !