Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

HELP! I'm not really good at Java!!! PLEASE Example, i'm so lost. import java.util.Random; import java.util.Scanner; /** * Find the longest run in a sequence

HELP! I'm not really good at Java!!! PLEASE Example, i'm so lost. import java.util.Random; import java.util.Scanner; /** * Find the longest run in a sequence of die tosses. * * This is a possible solution to Programming Exercise P6.13 * * "Write a program that generates a sequence of 20 random die tosses in an * array and that prints the die values, marking only the longest run, * like this: * 1 2 5 5 3 1 2 4 3 (2 2 2 2) 3 6 5 5 6 3 1 * * If there is more than one run of maximum length, mark the first one." */ public class FindLongestRunLab { public static void main(String[] args) { // int[] sequence = {1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, // 5, 6, 3, 1}; Random rng = new Random(); Scanner inp = new Scanner(System.in); while(true) { System.out.print("Enter the number of die tosses: "); int numTosses = inp.nextInt(); if(numTosses <= 0) { break; } // int[] sequence = {1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, // 5, 6, 3, 1}; int[] sequence = generateRandomDieTosses(numTosses, rng); int runStart = findLongestRun(sequence, sequence.length); int runLen = findRunLength(sequence, sequence.length, runStart); int runEnd = runStart + runLen - 1; printSequenceAndMarkRun(sequence, runStart, runEnd); } } /** * Print the sequence of integers and mark the run with () * * @param sequence sequence of integers * @param runStart index for the start of run * @param runEnd index for the end of run. */ private static void printSequenceAndMarkRun(int[] sequence, int runStart, int runEnd) { //TODO implement method body } /** * Returns the starting index of the longest run in a sequence of integers. * * @param data sequence of integers * @param numData sequence length * @return starting index of longest run. */ private static int findLongestRun(int[] data, int numData) { // TODO implement method body return 0; } /** * returns the length of a run in a sequence of integers * * @param data sequence of integers * @param numData sequence length * @param runStartIdx starting index of the run * @return length of run */ private static int findRunLength(int[] data, int numData, int runStartIdx) { // TODO implement method body return 0; } /** * Generate a sequence of random die tosses. An die has 6 sides that are * labeled 1, 2, 3, 4, 5, 6. * * @param numDieTosses number of die tosses to simulate * @param rng Random Number Generator * @return array that contains the die tosses. */ private static int[] generateRandomDieTosses(int numDieTosses, Random rng) { int[] dieTosses = new int[numDieTosses]; for(int k = 0; k < numDieTosses; ++k) { dieTosses[k] = rng.nextInt(6) + 1; } return dieTosses; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

Students also viewed these Databases questions

Question

=+5 How does HRM relate to efforts to increase innovation?

Answered: 1 week ago