vendredi 4 décembre 2015

Hailstone Program in Java

I have the following program to write:

An interesting (yet unsolved) question in mathematics is called "hailstone numbers". This series is produced by taking an initial integer and if the number is even, dividing it by 2. If the number is odd, multiply it by 3 and add 1. This process is the repeated. For example: An initial number of 10 produces: 10, 5, 16, 8, 4, 2, 1, 4, 2, 1... An initial value of 23 produces: 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1... Note that both numbers eventually reach the 4, 2, 1, 4, 2, 1... cycle. Create an application that offers the user three different ways to run this program.
Option 1: Print the hailstone numbers for a single entry and its length Example: Input> 10 10, 5, 16, 8, 4, 2, 1 Length 7
Option 2: Print all of the hailstone numbers from 4 to a given entry. Example: Input> 6 4, 2, 1 Length 3 5, 16, 8, 4, 2, 1 Length 6 6, 3, 10, 5, 16, 8, 4, 2, 1 Length 9
Option 3: Print out the number with the maximum number of iterations need to reach the cycle and which starting number produces this maximum from 4 to the number entered. Example: Input> 6 Longest: 6 Length: 9
In writing this program you must implement the following method... /** * * @param num Number that a hailstone chain will be generated * @param showNumbers true if list of numbers is shown to screen * @return Count of the numbers in the num hailstone chain. */ private static int hailStone(int num, boolean showNumbers)

This is the code I've written so far:

public static void main(String[] args) {
        int a = getInt("Give a number: ");

        System.out.print("How would you like to run the program? Option 1 prints hailstone numbers for a single entry and its length." +
                "Option 2 prints all the hailstone numbers from 4 to a given entry. Option 3 prints the number with the maximum number" +
                "of iterations needed to reach the 4, 2, 1 cycle.");
        int option = console.nextInt();

        boolean showNumbers = (option == 1 || option == 2);

        hailStone(a, showNumbers);
    }

    public static int getInt(String prompt) {
        int input;

        System.out.print(prompt);
        input = console.nextInt();

        return input;
    }

    private static void hailStone (int a, boolean showNumbers) {
        if (showNumbers == true) {
            if (a % 2 == 0) {
                for (int i = 0; i < 50; i++) {
                   for (int j = 0; j <= i; j++)
                    a /= 2;
                    System.out.print(a + " ");
                    a *= 3;
                    a += 1;
                    System.out.print(a + " ");
                }

            } else {
                for (int i = 0; i != a; i++) {

                }
            }
        } else {

        }
    }

I feel like I've hit a brick wall because I have no idea how to implement all these options in the method my teacher is requiring us to use. Plus, I can't seem to get even the basic hailstone chain to print. Help?

Aucun commentaire:

Enregistrer un commentaire