Dear all,

I have a test tomorrow and I just wanted to double check on my answer for this review question.

Write a method negativePositive that takes an array of integers, and returns an array that contains 3 integers. The first value in the returned array is the number of negative integers in the array, the second value is the number of zeroes in the array, and the third value is the number of positive integers in the array.
negativePositive({4, 90, -3, -1, 0, 0, 88, -5, -2}) -> [4, 2, 3]

public static int[] negativePositive(int[] arr)
    {
        int[] ret = new int[3];
        int i = 0, e = arr.length;
        while (i < e)
        {
            if (arr[i] < 0)
            {
                ret[0]++;
                i++;
            }
            if (arr[i] == 0)
            {
                ret[1]++;
                i++;
            }
            if (arr[i] > 0)
            {
                ret[2]++;
                i++;
            }
        }

Thank you.

You are missing a return statement. Also, to make it more effecient, use "else if" statements.

public static int[] negativePositive(int[] arr)   {
        int[] ret = new int[3];
        int i = 0, e = arr.length;
        while (i < e)
        {
            if (arr[i] < 0)
            {
                ret[0]++;
                i++;
            }
            else if (arr[i] == 0)
            {
                ret[1]++;
                i++;
            }
            else if (arr[i] > 0)
            {
                ret[2]++;
                i++;
            }
            return ret;
        }

Thank you! I have another question:

1. Write a method that takes an integer n, and returns a String. Assume the integer n is an odd integer between 1 and 9. If the returned String is printed, it should display a box like the one below, when n = 9. The integers in the last row will be n.

-----1-----
----333----
---55555---
--7777777--
-999999999-

What I have so far has some problems that I can't figure out how to fix:

import java.util.*;

public class NumberFigure 
{
    public static String dashes(int num)
    {
        String theDashes = "";
        for (int i = 0; i < num; i++)
        {
            theDashes += "-";
        }
        return theDashes;
    }
    
    public static String numbers(int number)
    {
        String theNumbers = "";
        for (int i = 0; i <= number; i++)
        {
            theNumbers += number;
        }
        return theNumbers;
    }
    
    public static void main(String[] args)
    {
        System.out.println("Enter 1, 3, 5, 7, or 9");
        Scanner scan = new Scanner(System.in);
        
        int num = scan.nextInt();
        String display = box (num);
    }
    
    public static String box(int n)
    {
        String theBox = "";
        for (int i = 1; i <= n; i+=2)
        {
            theBox += dashes((11-i) / 2);
            theBox += numbers(i);
            theBox += dashes((11-i) / 2);
        }
        return theBox;
    }
}

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.