aspose file tools*
The moose likes Beginning Java and the fly likes  control flow, factoring out repeating elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " control flow, factoring out repeating elements" Watch " control flow, factoring out repeating elements" New topic
Author

control flow, factoring out repeating elements

Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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.


String knock = "\u042F \u0418\u0433\u043e\u0440\u044c";
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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

Joined: Feb 13, 2011
Posts: 100

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

Joined: Aug 31, 2004
Posts: 11343

Igor Mechnikov wrote:I am off to learn about polymorphism...

Excellent! You might start here: How my dog learned polymorphism.
Igor Mechnikov
Ranch Hand

Joined: Feb 13, 2011
Posts: 100

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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Feb 13, 2011
Posts: 100

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:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: control flow, factoring out repeating elements
 
Similar Threads
Do while error, I'm going crazy
String arguments
Problems w/ a HashMap
Exception Handling
SAX unable to locate DTD from XML