Beginners Programs :

Java Program For Profitable Gamble | Java Programs

Java code for a profitable gamble, here you can see a detailed java program for a gamble java program.

Create a function that takes in three arguments (prob, prize, pay) and returns true if prob * prize > pay; otherwise return false

To illustrate, profitableGamble(0.2, 50, 9) should yield true, since the net profit is 1 (0.2 * 50 – 9), and 1 > 0.

For this problem, our input requirements are probability of winning (prob), prize and pay. Our expected output would be either true, if it’s profitable or false, if it is not profitable. After this, we decide on the logic and set of statements to be used.

To read the required inputs at runtime, we can use Scanner class in Java. This is very useful to read any primitive datatype input at runtime.

For this, we first have to create an object instantiating it and then invoke the necessary method based on our input type by using the object.

Here, probability of winning (prob) is a deciman value so we use the nextDouble() method and both the pay and prize are integers only so, we call the nextInt() method. This can be done as shown below:

int prize,pay;

Scanner sc=new Scanner(System.in);        //creating object of Scanner class

System.out.println(“Enter probability of winning:”);

double prob=sc.nextDouble();                //reading the probability of winning (prob)

System.out.println(“Enter prize:”);

prize=sc.nextInt();                                   //reading the prize

System.out.println(“Enter pay:”);

pay=sc.nextInt();                                    //reading the pay

After getting all our inputs, we will make a method call for a static method (profitableGamble) by passing all our three inputs as parameters or arguments. This method returns a boolean value true if the gamble is profitable else it returns false.

static boolean profitableGamble(double x,int y,int z )

In this method, we will check whether the product of probability of winning and prize is greater than the pay or not. We do so because, it is a profitable gamble only when, we get more than the amount we spend. So the formula used to determine this would be,

prob*prize > pay       (or)       (prob*prize) – pay > 0

If the above condition satisfies then, it would be a profitable gamble. Hence, we return true. Else, if this condition does not satisfy, it means that the gamble is not profitable. So, we return false.

if(x*y>z)

return true;

return false;

This returned value is displayed on the console screen by the main method by making use of the System.out method called the println() which displays everything and goes to the new line after displaying.

System.out.println(“Is The game (“+prob+”,”+prize+”,”+pay+”) is profitable —>”+profitableGamble(prob,prize,pay));

Output – 1:

Here, prob=0.2, prize=50 and pay=9.

prob*prize > pay i.e., 0.2*50 > 9 –> 10 > 9.

This condition satisfies so, the gamble is profitable. Hence, we return true.

Output – 2:

prob=0.9, prize=1 and pay=2

prob*prize > pay i.e., 0.9*1 > 2 –> 0.9>2

This condition is false so, the gamble is not profitable. Therefore, we return false.