• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing if statements  RSS feed

 
ivanka ivanova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I am doing a project for the university, I´ve written all the program and it execute(The program is about Finite State Machine) but I have to rewrite the code and change all for and if statements and loops. I need a help because my deadline is tomorrow. Please!
This is a part of the code:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivanka ivanova wrote:I´ve written all the program and it execute(The program is about Finite State Machine) but I have to rewrite the code and change all for and if statements and loops.

Erm...why?

I need a help because my deadline is tomorrow. Please!

Fraid that's your problem, not ours. If you could give us some more information as to how (or why) you have to change these statements, it would sure help us though.

Winston
 
ivanka ivanova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to change the if statements, this way to avoid repetitions of the same statements. If I don´t, I won´t pass the online Tester of the program. I know its my problem, I´m asking for some help.
Thank you.
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivanka ivanova wrote:I have to change the if statements, this way to avoid repetitions of the same statements. If I don´t, I won´t pass the online Tester of the program. I know its my problem, I´m asking for some help.
Thank you.

Maybe it's just me, but I really don't see what is wrong with your code...
 
ivanka ivanova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing wrong I know, I just have to rewrite the code
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivanka ivanova wrote:I have to change the if statements, this way to avoid repetitions of the same statements. If I don´t, I won´t pass the online Tester of the program. I know its my problem, I´m asking for some help.

And we're trying to help, but I can't see any repeated statements. What I do see is a possible redundancy between d.transitions() and d.states(), but I have no idea if that's what your Tester would be complaining about.

Winston
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Which if statements are repetitions?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivanka ivanova wrote:There is nothing wrong I know, I just have to rewrite the code

So we come back to my initial question...why?

I suspect we'll need a bit more code than what you've supplied (for example, your estadosConexos variable and DFA class - but just the relevant bits, please ).

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivanka ivanova wrote:There is nothing wrong I know, I just have to rewrite the code

Either there is nothing wrong, OR you have to rewrite the code. Both can't be true.

So, WHY do you think you need to rewrite the code? Does the 'online tester' give you feedback? does it say "You need to re-write your if statements BECAUSE..."?

The ONLY thing I see that you MIGHT want to change is the nested IF statements...they could potentially be combined into one...but even that is questionable as to a NEED to do that.

 
ivanka ivanova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I pass the tester in Eclipse, but the code is too long and messy, so I need to do it better instead, I´ll fail. I really apologize guys and thank you
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:So we come back to my initial question...why?

Actually, I do see one possible cause for complaint: there's no need for nesting. What you've got could be re-written as:but apart from the first 'if', it's simply a matter of style - I hate lots of negative logic in my 'if's, but you could just as easily write:The last 'if' isn't necessary, because Set.add() will only add an element if it doesn't already exist.

'state0' and 'stateD' are only used in one place, so you don't really need to define them - and if you do, they should preferably be defined where they're used (ie, inside the loop).

HIH

Winston
 
ivanka ivanova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! I made what you show me and works, I tried in other methods and the code is changed. Thank you!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ivanka ivanova wrote:Thank you so much! I made what you show me and works, I tried in other methods and the code is changed. Thank you!

You're welcome, but next time try to explain your problem a bit better; otherwise we're simply guessing at the answer.

It's also worth mentioning that none of what you wrote was wrong; except, arguably, that 'if (!set.contains(obj)) set.add(obj)' statement.

But you're not the first to do it; and you definitely won't be the last.

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!