• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Magic !

 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faiz:

Why not just use an enum for this?

John.
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to do this:

vacation class:



tester class:

 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats what i have done.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I did not think that way. I am sorry, will keep it in mind.
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it was not his intention


hey, I am a girl.
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same here.
 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also a very simple and short way to get this exercise done.
put the validation inside the constructor




 
Faiz Abdelhafid
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another Alternative

 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic