QuestionQuestion

The task is to figure out the % probality,to the nearest percent, that a triangle can be formed after breaking a piece of spaghetti in 2 random places along its length.To make it more challenging, you are allowed to nibble on one of the pieces before trying to make triangle.
Input formatAn int representing the maximum percent of the chosen piece that can e nibbled away
Output Format
An int is an integer from 0 to 100,the output is an integer from 0 to 100.
Sample input 15
Sample Output 75

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

/*
* Class to perform Monte Carlo analysis of
* breaking a length in 3 to make a triangle
*/
public class MonteCarloTriangle {

    // Number of Monte Carlo simulations to run
    final static int NUM_TRIALS = 100000;
   
    /*
    * Main method to run program
    */
    public static void main(String[] args) {
       // Use console input to get percent of spaghetti which can be nibbled away
       Scanner s = new Scanner(System.in);
       System.out.print("Enter maximum percentage of piece which can be nibbled away: ");
       int nibblePercent = s.nextInt();
       s.close();
       // Store the number of successful outcomes
       int successes = 0;
       for (int i = 0; i < NUM_TRIALS; i++) {
            // Simulate triangle through Monte Carlo
            if (simulateTriangleMonteCarlo(nibblePercent))
                successes++;
       }
       // Calculate percent of outcomes successful
       long percent = Math.round(successes * 100.0 / NUM_TRIALS);
       // Print percent to console
       System.out.println(percent);
    }
   
    /*
    * Method to simulate forming triangle through Monte Carlo
    */
    public static boolean simulateTriangleMonteCarlo(int nibblePercent) {
       // Randomly generate three side lengths by splitting
       // lengths away from a total length of 1.0
       double a = Math.random();
       double b = (1.0 - a) * Math.random...

By purchasing this solution you'll be able to access the following files:
Solution.PNG and Solution.java.

$45.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats