numbercube in java

Solution for numbercube in java.

package com.NextGen4IT;

import java.util.*;

public class NumberCube {

    Random r;

    public NumberCube() {
        r = new Random();
    }

    public int toss() {
        return r.nextInt(6) + 1;
    }

    // Solution to part (a)
    public static int[] getCubeTosses(NumberCube cube, int numTosses) {
        int[] ans = new int[numTosses];
        for (int i = 0; i < numTosses; i++)
            ans[i] = cube.toss();
        return ans;
    }

    // Solution for calculate longest run
    public static int getLongestRun(int[] values) {

        // Initialize all of our necessary variables.
        int curMaxIndex = -1;
        int curRunLength = 1;
        int curStartIndex = 0;
        int curValue = values[0];
        int maxRun = 1;

        // Loop through each die roll.
        for (int i = 1; i < values.length; i++) {

            // Our run continues.
            if (values[i] == curValue)
                curRunLength++;

            // Reset our max variables for the first seen run.
            if (curRunLength > 1 && curMaxIndex == -1) {
                curMaxIndex = curStartIndex;
                maxRun = curRunLength;
            }

            // Or, update this information if this is the longest run we've
            // seen.
            if (curRunLength > maxRun) {
                curMaxIndex = curStartIndex;
                maxRun = curRunLength;
            }

            // If there is no match, we have to reset our current variables.
            if (values[i] != curValue) {
                curValue = values[i];
                curStartIndex = i;
                curRunLength = 1;
            }

        }

        // This is the answer they want.
        return curMaxIndex;
    }

    // For testing purposes.
    public static void print(int[] vals) {

        for (int i = 0; i < vals.length; i++)
            System.out.print(vals[i] + " ");
        System.out.println();
    }

    // Test these methods.
    public static void main(String[] args) {

        // Test for part (b)
        NumberCube myDie = new NumberCube();
        int[] rolls = { 1, 5, 5, 4, 3, 1, 2, 2, 2, 2, 6, 1, 3, 3, 5, 5, 5, 5 };
        System.out.println("The longest run for the sample starts at index " + getLongestRun(rolls));

        // Test for part (a) and (b)
        rolls = getCubeTosses(myDie, 40);
        print(rolls);
        System.out.println("The longest run for this array starts at index " + getLongestRun(rolls));

    }
}

1 comments:

Had no experience on that before and it's nice to know how to deal with it so far, thank you for that! To get this very clear, I have a little commercial experience in java and programming at all, because I'm new to that, but I've done with all the essential fundamentals for good so far, I would say. May it sounds quite arrogant, but I'd tested my skills out answering some most common java interview questions get redirected if you want to take a look on some too, because you know, you can never be too thorough. So, I'd passed something about 2 tons of questions and my results were pretty impressive, I would say. So it gave me some confidence in order to start solve more directed tasks


EmoticonEmoticon