Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Simple Array Problem

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Simple Array Problem

    I can't figure out the answer to this homework problem:

    Write a static method named countEmptyBowls, to be added to the Bowl class, which is passed an array of Bowl objects, and returns the (int) number of Bowl objects in the array that are empty.

    Bowl Class:

    public class Bowl {
    	private double weight;
    	private boolean empty;
    	private String origin; // country of manufacture
     
    	public Bowl(double w, boolean e, String origin) {
    		weight = w;
    		empty = e;
    		this.origin = origin;
    	}
     
    	public double getWeight() {
    		return weight;
    	}
     
    	public boolean getEmpty() {
    		return empty;
    	}
     
    	public String getOrigin() {
    		return origin;
    	}
     
    	public void setEmpty(boolean emptyStatus) {
    		empty = emptyStatus;
    	}
     
    	public String toString() {
    		return ("from " + origin + " weight: " + weight);
    	}
     
    }

    This is my most recent try for the method:

    public int countEmptyBowls(int[] Bowl){
      int count = 0;
      for(k = 0;k<Bowl.length;k++){
        if(k.getEmpty()== true) count++;
        return count;}

    This didn't work. Ive tried a few other things but they didn't work either :'(
    Thanks for helping!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,641
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: Simple Array Problem

    You need to access the elements that are in the array. That is done using array notation with []s
    For example this accesses the element in theArray at theIndex:
     theArray[theIndex]

    The elements in an array are just the same as a variable of the same type.
      int[] anArray = {1,2,3};
      int anInt = anArray[0];  //  anInt and anArray[0] are both int variables
    anArray[0] can be used in the same places as the variable: anInt is used.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    The array needs to be an array of Bowl objects... The method signature should look like this

    public static int countEmptyBowls(Bowl[] bowls) {
    // method definition in here
    }

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Array Problem

    The question asks for a foreach solution

    public static int countEmptyBowls(Bowl[] balls){
    int sum=0;
    for(Bowl bowl:balls){
    if(bowl.getEmpty() == true){
    sum++;
    }
    }
    return sum;
    }

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    13
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    A thing of beauty

Similar Threads

  1. [SOLVED] Simple array code
    By azizmaiden in forum Java Theory & Questions
    Replies: 1
    Last Post: March 2nd, 2013, 05:54 PM
  2. [SOLVED] Array (simple).
    By 18107 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 25th, 2012, 04:59 PM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Error for a simple array
    By DudeJericho in forum Collections and Generics
    Replies: 4
    Last Post: April 25th, 2011, 02:46 PM
  5. logic error somewhere.. working with try & catch, simple array.. please help
    By basketball8533 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 9th, 2010, 11:40 AM