Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Need Help Replace Random Array with Read in from file

Hello,

I have my class in question below. The structure is what I want, however, I am having a hard time figuring out how to declare my own array instead of the random array. Any help would be greatly appreciated. I am thinking I have to modify the bottom scramble method some how.

Background of the class: This class calls 3 specific sorting algorithms classes. Depending on which one the user picks, it will call that specific class and it will animate the sorting.

package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import java.util.Scanner;

public class Launch extends AlgorithmAnimator {

    protected SortAlgorithm theAlgorithm;
    protected AlgorithmFactory algorithmFactory;

    protected void initAnimator() {

        //Get Algorithm Choice From User
        System.out.println("Please select your Algorithm: "
                + "[1] BubbleSort   [2] QuickSort   [3] SelectionSort");
        Scanner scanner = new Scanner(System.in);
        String inputAlgorithm = scanner.nextLine();


        if ("1".equals(inputAlgorithm)) {
            algName = "BubbleSort";
        } else if ("2".equals(inputAlgorithm)) {
            algName = "QuickSort";
        } else if ("3".equals(inputAlgorithm)) {
            algName = "SelectionSort";
        }else {
            algName = "BubbleSort";
        }
        System.out.println("Your selection is: " + algName);

        //Set Algorithm Type
        String at = getParameter("alg");
        if (at != null) {
            algName = at;
        }

        algorithmFactory = new StaticAlgoFactory(this);
        theAlgorithm = algorithmFactory.makeSortAlgorithm(algName);

        setDelay(20);
        scramble();

    }

    protected void algorithm() {
        if (theAlgorithm != null)
            theAlgorithm.sort(arr);
    }

    //scramble()
    protected void scramble() {

        arr = new int[getSize().height /2];
        for (int i = arr.length; --i >= 0;) {
            arr[i] = i;
        }
        for (int i = arr.length; --i >=0;) {
            int j = (int)(i * Math.random());
            swap(arr, i , j);
        }
    }

    protected void paintFrame(Graphics g) {
          Dimension d = getSize();
          g.setColor(Color.black);
          g.fillRect(0, 0, d.width, d.height);
          g.setColor(Color.green);
          int y = d.height - 1;
          double f = d.width / (double) arr.length;
          for (int i = arr.length; --i >= 0; y -= 2) {
                  g.drawLine(0, y, (int)(arr[i] * f), y);
          }

        }

     //swap()
     private void swap(int a[], int i, int j) {
           int temp = a[i]; a[i] = a[j]; a[j] = temp;
        }

    protected int arr[];
    protected String algName;

}

1 Answer

When you say "declare your own array", do you mean you want the user to enter the values as the program is running?

Your scramble method creates an array with a list of numbers, then uses the swap method to scramble the values. You need to replace that with a method that asks the user for input and initializes the array.

Declare a new method inputArray(). Inside this method, ask the user to first input the size of the array and initialize the arr variable with a new array of that size. Then use a for loop to iterate through each index value of the array. At each index value, ask the user for input and store the input inside the array at the given index. User Integer.parseInt() to convert input String in an int before storing it.

Then all you need to do is call inputArray() instead of Scramble() inside your initAnimator() method.

Next you can add another feature, and offer the user the choice of either using his own array or the random one. You can call either scramble() or inputArray() depending on the answer :-)