programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# Array Magic !

Ranch Hand
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

Author
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?

Rancher
Posts: 600
Faiz:

Why not just use an enum for this?

John.

Faiz Abdelhafid
Ranch Hand
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
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
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
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
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
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
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.

Rancher
Posts: 3742
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

Faiz Abdelhafid
Ranch Hand
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.

Ranch Hand
Posts: 504
Are you trying to do this:

vacation class:

tester class:

Faiz Abdelhafid
Ranch Hand
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
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
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
Posts: 504
thats what i have done.

David Newton
Author
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
Posts: 504
ok, I did not think that way. I am sorry, will keep it in mind.

Faiz Abdelhafid
Ranch Hand
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
Posts: 504

I guess it was not his intention

hey, I am a girl.

Faiz Abdelhafid
Ranch Hand
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
Posts: 504
same here.

Faiz Abdelhafid
Ranch Hand
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
Posts: 32
Another Alternative

Marshal
Posts: 69813
277
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.

 and POOF! You're gone! But look, this tiny ad is still here: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton