• Post Reply Bookmark Topic Watch Topic
  • New Topic

Python Code to Java  RSS feed

 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

First time here, exploring this forum.

I have 2 questions:

1) Can somebody help me out converting the following python code to Java.

from random import randint

count =  0
QAQC = 0
POST = 0

while count < 1000:
    result = randint(0,100)
    if result >= 1 and result <= 20:
        QAQC += 1

    else:
        POST += 1
       
    count += 1
      
print(QAQC)
print(POST)

2) We use a web based application tool to process enrollments and each enrollment goes through a multiple Work Flow Step Process. The main problem I am trying to solve here is to kick off a particular Workflow Step only 20% of time or probably every N+4 time and the remaining could go into the regular Work Flow Step. So basically only 20% of the enrollment needs to have that extra WFS. The above python script uses random function I believe, not sure how helpful would that be.

Any ideas or suggestions would be really appreciated.

Thanks
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to write any Java programs?

There are some building blocks to work on in Java:
- variable declaration and assignment.
- looping (while)
- condition statements (if / else)
- conditional logic (and)
- printing to the console

How are you with any of those in Java?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Maybe it is a bad idea to convert code from one language to another. Maybe it is better to write down what the code is supposed to do and then code it from scratch in the target language. I am not sure how you are going to get to your workflow system, I am afraid.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I agree. If you want to modify some Java code so that some part of it (the "extra workflow step") is called randomly 20% of the time, then you don't need all that code. You just need some code like this:

Create a random number between 0 and 1
If it's less than 0.2 then do the extra workflow step.

However if you want to modify it to call the extra workflow step for every fifth run, that would be a different thing. Probably not a harder thing, though. So it would help to clarify your requirements before you start coding.
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a newbie with Java. On each workflow step we can 'Enable conditional Step creation with a formula(must evaluate either True or False)' snapshot attached. We can get to the Workflow Step as follows, pic attached. I want this step to be only created 20% of the time or 1 in 4 of every enrollment that comes along. I am unable to write this in Java.

Thanks for all your help



1.JPG
[Thumbnail for 1.JPG]
2.JPG
[Thumbnail for 2.JPG]
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... you have some kind of a workflow designer tool there? How does it work? Do you just type Java code into the "Formula" box, or is there more to it than that?
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats right we can configure each Work Flow Step, we just have to type a VB or Java logic/formula in the box.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty straightforward, then. And you have to code the workflow step in Java as well? Seems like that could be a problem since you haven't started to learn Java yet.
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, all the Work Flow Steps have already been coded by the vendor, we just need to add a formula for our specific purpose.

 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "formula"? What's that?

Sorry if I seem to be dragging this process out. But I think you should identify your problem before you start working on it, and I'm not sure you have done that yet. You think you can type some Java code into that box (at least I think you think that) but then there's this "formula" idea instead. I'm putting "formula" in quotes because that isn't a Java term.

Perhaps you could look at whatever other people have done in other places in this workflow, and see if anything looks like something you could imitate.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muffy Paine wrote:I want this step to be only created 20% of the time or 1 in 4 of every enrollment that comes along.


20% is 1 in 5.
1 in 4 would be 25%.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muffy Paine wrote:. . . 'Enable conditional Step creation with a formula(must evaluate either True or False)' snapshot attached. . . .
Writing that in simple words would be a good start.
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give you guys more information so it is better understood. All the Work Flow Steps are already configured.  Here how it works, an enrollment has to go through the following Work Flow Steps: Application, Installation, Post Inspection and Payment. Whenever each step is completed another one automatically kicks off. We created another Work flow step 'QA QC' that comes after Installation and before Post Inspection. We only want QA QC work flow step to be created only 20 % of the time or 1 out of 5 enrollments that come in Installation Workf flow step needs to have a QA QC Work flow step, the rest can directly go to Post Inspection WFS. We have been told it can be done through a java formula.

So basically every n, n+4 enrollment needs to have a QA QC work flow step and the rest does not.

Let me know if that makes sense.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that design makes sense. It's just the environment you're working in which nobody here seems to know much about.
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by the 'environment' can you explain
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Environment is everything involved in your computer and programming systems.  I think the biggest question is how Java will be used and how will it be integrated into your system.

Also, just so you know, since this is the Beginning Java forum, we normally don't just give out programming code.  But this situation might be different.
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info. How would a code in general look like for a problem I am trying to solve.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muffy Paine wrote:what do you mean by the 'environment' can you explain


That picture you posted.
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muffy Paine wrote:Thanks for the info. How would a code in general look like for a problem I am trying to solve.


It would look like this, in general:

Create a random number between 0 and 1
If it's less than 0.2 then do the extra workflow step.


Only you'd translate that into Java code.

It looks like you're doing this work for some kind of business organization. I have to say, I'm surprised that they assigned somebody who knows nothing about Java to type Java code into that box. That seems like a risky thing for them to do, which is why I'm surprised. Did they not offer to give you some instruction in Java before you tried this project?
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. Our programming guy has left and we won't have another one till next month. I am all database and no programming. I am just trying to help the team. I was also thinking that we might have to use 'random' here. So how would we translate into Java the following 'Create a random number between 0 and 1
If it's less than 0.2 then do the extra workflow step' ?

Let me give you an example of a code which was used in the past within the same formula box for another purpose so we know the structure of the code that's been used.




We would be concerned with the following below Work Flow steps for our code: So basically create a random number between 0 and 1 when the enrollment is in WorkFlow Step = "Installation" , if <= 0.2 then do the Work Flow Step = "QA QC" else Work Flow Step = "Post Inspection" . Can I get some help to get started with the code?

Thanks Again for your help, really appreciate it.




4.JPG
[Thumbnail for 4.JPG]
5.JPG
[Thumbnail for 5.JPG]
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically, you'd use Random or ThreadLocalRandom in a normal Java program.

You'll have to take that information and pull out what you need so it works in your tool. Given what you've shared so far, I don't think anyone is sure how imports would work in your tool/environment, or whether you can even define methods like main() or if you just provide the code inside the main() method shown and your tool will take care of the rest of the scaffolding needed to turn it into executable Java bytecode.

Basically, we can show you what the incantations would be as a normal Java program but you have to figure out how to make it work in your tool's custom environment.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To complicate matters even more, this code that you showed isn't even valid Java code:

 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu, I believe that we just need to provide the code and not worry about the imports as the tool might take care of that. Also the code you mentioned as incorrect Java Code I would be interested to see what would be the accurate one, one of the formula requirements is that (it must evaluate to either tue or false).

So how do I insert the Random Program you gave into the following logic? create a random number between 0 and 1 when the enrollment is in WorkFlow Step = "Installation" , if <= 0.2 then do the Work Flow Step = "QA QC" else Work Flow Step = "Post Inspection"

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, we don't know enough about your environment to answer some of these questions you're asking. You won't even tell us what the name of this tool is. Is it a commercially available tool that your company bought? Is it as Software-As-A-Service type thing (web-based) that you subscribe to? Is that code I said is invalid Java actually considered valid in your tool? If not, then why give it as an example?

As for random, if you are able to use the java.util.Random and your goal is to just execute code 20% of the time, you don't need a number between 0 and 1.  You can use nextInt(100) so you're dealing with whole numbers and not floating point numbers. It works out the same either way but dealing with whole numbers seems much easier to me.

Lastly, you're going to have to learn how to program Java if you're going to do this while waiting for another developer guy to come in. People here are nice enough to give you code a few times but you're going to have to put in some effort of your own. That's your work after all and we're all just volunteers here.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you'll get closer to "20% of time" with a random number generator as your sample size increases. That is, the more numbers you generate, the more evenly distributed your results will be (Monte Carlo method)

If your sample size is only 20 numbers generated with a random number generator, you are likely to get anywhere from 5% to 40% of them less than 20.  That percentage gets closer to 20% as you increase your sample size, say to 2000 numbers generated.

The point is that there are other factors you need to consider, such as the inherent behavior of the random number generator you're using and your sample size. Don't just assume that you're going to get an even distribution right off the bat.

Play around with these programs a bit to see what I mean:
https://repl.it/OCSy/5  (sample size = 20)
https://repl.it/OCSy/6  (sample size = 2000)
https://repl.it/OCSy/7  (sample size = 20000)

(EDIT: fixed broken URLs)
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it's a third party online web based application processing tool that we are using. I am willing to learn Java and want to test out some codes, we do have a test environment where I can insert formulas and play around as well. That's the reason i asked if someone can provide me some codes so I can go in test and see what results i get. How do I complete the below code:

if (ThreadLocalRandom.current().nextInt(100) < 20) {step.WorkflowStep = "Installation"
  
}

I do appreciate all your help guys..
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muffy Paine wrote:How do I complete the below code:

if (ThreadLocalRandom.current().nextInt(100) < 20) {step.WorkflowStep = "Installation"
  
}

I don't know how to get this point across. Maybe an analogy will help. Imagine us as sailors who know how to operate a modern United States Navy submarine. Now, imagine putting us inside some unknown vessel, we don't even know if it's a submarine or what. We only know it might be a submarine because there aren't any portholes and it kind of looks like the environment we're familiar with on our US Navy subs. Except we don't see the same controls we have on our subs. And we don't know where the periscope is. Or how we're supposed to steer. And the instruction manuals missing. None of the instrument panels are labeled with terms we're familiar with either.

So your question of how to complete that code is kind of like asking us "How do we get this puppy underway?" inside this strange and unfamiliar vessel that might be a submarine. Do you see the dilemma we're in here?
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muffy Paine wrote:Let me give you an example of a code which was used in the past within the same formula box for another purpose so we know the structure of the code that's been used.



But that's JavaScript, not Java. Are you sure that Java code will work there, or should you maybe be writing JavaScript code?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:But that's JavaScript, not Java.

So, the plot thickens...

If it's actually JavaScript you're supposed to write, then neither of the methods mentioned before to generate random numbers are going to work. It's going to come back to what is available in your environment, under the covers of the tool's user interface that's ostensibly designed to make things easier. You're going to need to dig into the User Guide and Developer Guide for your tool, if those are available to you.
 
Muffy Paine
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked your Analogy Junilu, and I completely see there are multiple blind spots here, we have no idea how the before and after Work flow steps are coded. We have asked the vendor to code that for us. I appreciate all your help you all provided and would look for help in the future, but next time I would be more precise
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!