Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

control flow, factoring out repeating elements

 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

My program creates a basic math quiz.
It works but is unsightly because it essentially does same thing several times, more if I add division and modulo operation.



I was hoping to factor out the repeating elements in the following but it does not work, apparently because initialized question objects myProb must have different names for different classes.
(naturally, switch does not work either)



I welcome all suggestions.

Thank you.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a good candidate for polymorphism.

Do these different types of problems subclass (or implement) a more general type? For example...

Then you could declare myProb as the supertype, and instantiate it as needed. For example...

(Just make sure the methods you need are declared in the supertype, and then overridden in the subtypes.)
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:This looks like a good candidate for polymorphism.

Do these different types of problems subclass (or implement) a more general type? For example...

Then you could declare myProb as the supertype, and instantiate it as needed. For example...

(Just make sure the methods you need are declared in the supertype, and then overridden in the subtypes.)


Fantastic, Thank you Marc!
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am off to learn about polymorphism.

In the meantime, I made the original solution even more unsightly when I added additional controls to offer second chances for wrong answers:



 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor Mechnikov wrote:I am off to learn about polymorphism...

Excellent! You might start here: How my dog learned polymorphism.
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc weber wrote:
Igor Mechnikov wrote:I am off to learn about polymorphism...

Excellent! You might start here: How my dog learned polymorphism.[/quote

Very interesting story.

The code looks MUCH better now, but there is a problem: I now need to figure out how to have a DIFFERENT question of a certain kind each time.
As is I am getting all questions of a certain type are identical. In other words, I need a new object each interation of the loop.


polymorphied code:



The code below now works, thank you again.


 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor Mechnikov wrote:... I now need to figure out how to have a DIFFERENT question of a certain kind each time.
As is I am getting all questions of a certain type are identical. In other words, I need a new object each interation of the loop...

You can create and assign your new objects inside the loop, so "thisProblem" can be different every time. Maybe something like this...


Edit: Whoops, I see this is basically what you're already doing in your second example above. I think your issue depends on how these objects are instantiated. For example, when you create a new AdditionProblem(), do you get a "new" problem each time?
 
Igor Mechnikov
Ranch Hand
Posts: 100
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you again Marc. My code above now works.
The problems I had before were 1) did not instantiate myProblem correctly first -- forgot () after myProblem = new AdditionProblem();
and 2) when instantiating with if, else if, else if, I did not have 'else' at the end to cover all the possibilities, just else if. I am impressed the compiler picks on that.

marc weber wrote:
Igor Mechnikov wrote:... I now need to figure out how to have a DIFFERENT question of a certain kind each time.
As is I am getting all questions of a certain type are identical. In other words, I need a new object each interation of the loop...

You can create and assign your new objects inside the loop, so "thisProblem" can be different every time. Maybe something like this...


Edit: Whoops, I see this is basically what you're already doing in your second example above. I think your issue depends on how these objects are instantiated. For example, when you create a new AdditionProblem(), do you get a "new" problem each time?


Edit: Here is the final version, I am really glad it is much trimmer and legible than the original:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic