As a practical joke for your birthday, your best friend gave you some
single-dimensioned arrays, each full of boolean values. She didn’t tell
you how many items are stored in these arrays.
As if that weren't bad enough, your friend told you that each array is
filled almost entirely with false values, except for a small sequence of
consecutive trues contained somewhere in the array.
Overwhelmed by the novelty of all this, you decided (actually, we've
decided for you) to write a Java method named fullOfBool that
accepts an array of boolean values as its only argument, and will print
Ø the location of the first true in the boolean array
Ø the location of the last true in the boolean array
Ø the total number of true values in the string of consecutive
true values.

Your main method should call on fullOfBool using the following
arrays to test it out:
boolean [] test1 = { false, true, true, true} ;
boolean [] test2 = { true} ;
boolean [] test3 = { true, true, true, true, false} ;

public class Bool {

    boolean presents = fullOfBool();
    boolean surprises = new boolean[presents];

    public boolean fullOfBool(boolean[] p)
    {
        surprises = boolean[] p;

        for( int i = 0; i< presents.length ; i++)
        {   
            if(surprises.contains(true));
            {
                System.out.print(surprises[i]);
            }
        }
    }

}

I have so much done but eclipse is not liking it, anyone could advice how do i solve this question?
Thank u so much..

Recommended Answers

All 2 Replies

The reason why your code is wrong is because:
a) you say boolean presents = fullOfBool(); , you can't do that becuase fullOfBool does not return anything.
b)public boolean fullOfBool(parameters){...}, this method does not return anything, so just call it void.
c)if(suprises.contains(true)), no,no,no you can't do this becuase it's not a String, its a bool.

Here is some of the solution, I forgot to add the location of the last boolean value. Here is the code:

public class Bool
{
    static int trueCount = 0;
    static boolean first  = false;

    public static void fullOfBool(boolean p[])
    {
        for(int i = 0; i < p.length; i++)
        {
            if(first == false)
            {
                if(p[i] == true)
                {
                    first = true;
                    System.out.println("The first true value is p[" + i + "]");
                    trueCount++;
                }
            }

            else if(p[i] == true)
            {
                trueCount++;
            }


        }

        System.out.println("The consecutive amount of true is: " + trueCount);

    }


    public static void main(String[] args)
    {
        boolean presents[] = {true, true, true, false};
        fullOfBool(presents);
    }

}

if(first == false) is a long way of saying if(! first)
and
if(p[i] == true) is a very silly way to say if (p[i])

commented: Yea I kinda wrote this while I was tired. +0
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.