-2

I am a beginner when it comes to java and am having some issues(been sitting in front of this all day). I have searched far and wide for a solution, yet to no avail. My professor has asked me to fill out the blank methods, and I am especially having trouble with the toString method(although I am not feeling to good about the rest either), any help would be appreciated.

import java.util.Random;

public class SmartArray {
    // declare an array of ints

    int[] list;
    Random r = new Random();
    int pos = 0;

    /**
     * Creates and initializes an array of size n. It does not initialize the
     * contents of the array.
     */
    public SmartArray(int n) {
        list = new int[n];

    }

    /**
     *
     * Initializes the contents of the array with random
     *
     * non-negative* numbers.
     *
     */
    public void initRandom() {
        int i = 0;
        int hold = 0;
        while (i < list.length) {
            hold = r.nextInt();
            if (hold % 2 == 0) {
                list[i] = hold;
                i++;
            } else {
                hold = hold + 1;
                list[i] = hold;
                i++;
            }

        }
    }

    /**
     *
     * Allows client code to add an element to the array
     *
     * at the given position. Throws an ArrayIndexOutOfBounds
     *
     * exception with a message if the
     *
     * position is out of bounds.
     *
     */
    public void insert(int value, int pos) {
        if (list.length > pos) {
            list[pos] = value;
        } else {
            throw new ArrayIndexOutOfBoundsException("The position of your value is greater than the array");

        }
    }

    /**
     *
     * Returns the position of target if target is
     *
     * one of the values in the array, otherwise -1.
     *
     * Implemented with a loop.
     *
     */
    public int find(int target) {
        int position = 0;
        for (int i = 0; i < list.length; i++) {
            if (list[i] == target) {
                position = i;
            } else {
                position = -1;
            }
        }
        return position;

    }

    /**
     *
     * Same as the find method, except that it's implemented
     *
     * using recursion. (Hint: use a helper method.)
     *
     */
    public int recursiveFind(int pos, int target) {
        if (pos >= list.length) {
            return -1;
        } else if (list[pos] == target) {
            return pos;
        } else {
            return recursiveFind(pos + 1, target);
        }

    }

    /**
     *
     * Returns the elements of the array, separated by
     *
     * spaces, with a newline after every 10 elements,
     *
     * so they can be easily displayed.
     *
     */
    public String toString() {
        String listString = "";
        int pos = 0;
        for (int i = 0; i < list.length; i++) {
            //list[i].toString();
            listString = listString + (String) list[i] + " ";
            pos++;
            if (pos > 9) {
                list = list + "\n";
                pos = 0;
            }
            return listString;
        }

    }
}
GlupiDebil
  • 53
  • 5
  • This is out of OP's question, does this code even running? Where is the main part? public static void main(String[] args) – The Coder Feb 01 '15 at 22:53
  • 1
    Could you post what output you get by running the current code and what you expect/need the correct output to be? – Cristian Vat Feb 01 '15 at 22:56

3 Answers3

0

How about:

public String toString() {
    String listString = "";
    for (int i = 1; i < list.length; i++) {
        listString += list[i - 1] + (i % 10 == 0 ? "\n" : " ");
    }
    return listString;
}
0

Your method looks almost fine.

To add the newline, you should check for the remainder of the index.

You're also returning the resulting String inside the loop. You should only return after it.

public String toString() {

    String listString = "";
    for (int i = 0; i<list.length; i++){
        listString = listString + (String)list[i] + " ";
        if (i%9 == 0){ // if remainder of i%9 = 0, then add a newline
            list = list + "\n";
        }

    }
    return listString;
}
Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28
0

I don't want to give too much away since it's an academic assignment, but instead of pos, consider mod (%), which you use, so I know you're familiar with it.

Consider starting i at 1 and iterating through all values (no need to take 10 at a time). But test for the need for a newline like this:

i % 10 == 0

Whenever the ith value is divisible by 10, insert a newline.

Also, consider using Integer instead of int. In this way, you could simply do this:

listString = listString + list[i].toString() + " ";

Take a look at this post on SO.

Good luck!

Community
  • 1
  • 1