Run combination of methods based on user input? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Assuming I have 4 methods
methodA()
methodB()
methodC()
methodD()
Now I want the user to be able to input a number to run a combination of the methods.
So the user can choose if they want to run:
methodA() and methodB()
or:
methodB(), methodC() and methodD()
or just one of the methods or all of them.
Is there any other possibility than specifying any possible combination in if-statements?

You could use a bitmask. In that way one number could represent multiple flags that indicate which methods get called. A quick google search returned the following:
http://drumcoder.co.uk/blog/2010/jan/06/bitmasks-java/ Explains some solutions for this problem, which is what I think you're facing.
If you need to store multiple boolean flags for something, you can do
it using bits and storing that inside a single integer.

Related

Java - why would one use the state design pattern over saving the state in a variable? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I am very new to Java so excuse me if this sounds like a dumb question.
Why is such a big effort made when following the state-design-pattern (creating an interface, context and concrete sub-classes for each state) when you could just save the state of a given object in a variable and then make decisions based on switch and if-else statements later on?
if-else and switch statements encourage brittle code and responsibilities mixing.
Every time you add/remove/update a state, the same class and method has to be changed or a sub method invoked, so you increase the odds of introducing regressions in any state logic, whereas you would change one of them instead.
By separating the concerns, the states are not coupled; you could easily modify them without risking changes to any others. You could even validate this with unit tests.

I'm new at programming. I'm not sure how much to use methods [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, I just finished my first program, but I haven't use any methods in it, since I'm just beginning to learn how to use them. Here's the original code : http://codepad.org/JiBfJI8Q I started to fractionate it but realised that it would be a method inside another all the way down. Is that actually the way to do it, or did I get the idea wrong?
without having looked at your code:
The general idea of methods is to separate small
portions of code which might be used at multiple other places in your code.
so yes, calling methods from within other methods is a good thing to do.
ideally your so called "composed methods" read out like a little story:
public void transaction(){
openDatabaseConnection();
addRecordsToDatabase();
closeDataseConnection();
}

Best way to maintain a list in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a list of objects which get created by a factory class and for the time being I want to limit the list size to 10 objects. I also need to maintain the list through out the program as multiple classes will be referencing its values. I don't want to create it as a singleton and im not happy about having a master class maintain it, so is there another way? (Best Practice)
For setting the max size you can refer to this post here Any way to set max size of a collection?
Not sure what you mean by "maintaining" the list though. There is no way (that I know of) to make that list accessible to all classes without having it in a "master class". The only other option is passing it through functions.

where java main method is defined at the starting time of below class or at middle of programme [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example in some Java programs we use main method at below the class
in some Java programs we use main method after at writing methods what is the difference?
There is no difference if we write main method first or last. All the java programs starts the execution from the main class and it is independent from position (first or last).
there is no different, but you have to put all methods in a class...
you can read the manual :-) https://docs.oracle.com/javase/tutorial/getStarted/application/

Best way to run two task independently. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the best way run two block of code independently and join before third block.
Actually I m planning to design custom java based work flow.
And my requirement is to run two process independently and join at third process.
I don't want to create thread manually and join on it.
Any help is appreciated.
Are you using java 8? CompletableFuture is very handy:
import static java.util.concurrent.CompletableFuture.allOf;
import static java.util.concurrent.CompletableFuture.runAsync;
....
allOf(
runAsync(() -> {
// do first thing
}),
runAsync(() -> {
// do second thing
})
).join();
// do third thing
There are a lot of options, of course. For one, if you are doing IO, you would probably want to supply an executor to runAsync.

Categories