Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree1Likes

Thread: Boolean Method Problem

  1. #1
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Boolean Method Problem

    Hi all, I'm new to the forum and this is my first post.
    I'd need help with a boolean method as the title says.
    Here is the code I'm working on:

    Java Code:
    import java.util.Random;
    public class Parking
    {
    	private Car[] spaces;
    	private int totalSpaces;
    	private int freeSpaces;
    
    	public Parking(int totalSpaces)
    	{
    		Car[] spaces = new car[totalSpaces];
    	}
    
    	public boolean green()
    	{
    		for (int i=0; i<totalSpaces; i++)
    		{
    			if (spaces[i] == null);
    			return true;
    		}
    		return false;
    	}
    }
    And here's the tester:

    Java Code:
    public class ParkingTester
    {
    	public static void main(String[] args)
    	{
    		Parking testPark = new Parking(10);
    		System.out.println(testPark.green());
    	}
    }
    Car is a class I already made.
    What I want the code to do is to return true in the green method if there are null valors in the testPark array.
    If the testPark array is full i.e. there are no nulls then the green method should return false.
    With the code as it is, the testPark array should have only null valors, and the green method should return true, but i get false instead. What am I doing wrong?
    Thanks for every piece of imput you'll give me.

    EDIT: some lines like the import one and the freeSpaces one are there for future methods.
    Last edited by Bonfy; 01-22-2012 at 06:59 PM.

  2. #2
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: Boolean Method Problem

    Hello!

    Firstly your line number 10 in Parking class should give you an error since you are using Car and car either it will have an uppercase or lowercase "c" depending
    upon your "car" or "Car" class.

    Secondly i do not see any testPark array in your code otherwise it would have been Classname[] testPark =new Classname[size];
    Last edited by Rameshwar Soni; 01-22-2012 at 07:32 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by Rameshwar Soni View Post
    Secondly i do not see any testPark array in your code otherwise it would have been Classname[] testPark =new Classname[size];
    ???

    No this is not correct. There is no need for an array such as this.

    You are correct though that the OP should change

    Java Code:
    Car[] spaces = new car[totalSpaces];

    to:

    Java Code:
    Car[] spaces = new Car[totalSpaces];

    I'm surprised that your code would even compile with this error.
    Last edited by Fubarable; 01-22-2012 at 07:36 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    Java Code:
    			if (spaces[i] == null);
    That trailing semi colon should go.

    kind regards,

    Jos
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    ???

    No this is not correct. There is no need for an array such as this.
    So using Parking testPark = new Parking(10); as done in the above is creating an array???

    Actually i am also a newbie in Java, i thought this is just passing the value 10 to the parameterized constructor.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by Rameshwar Soni View Post
    So using Parking testPark = new Parking(10); as done in the above is creating an array???
    Yes, it's created in the Parking class code, in its constructor.

    Actually i am also a newbie in Java, i thought this is just passing the value 10 to the parameterized constructor.
    You may want to only answer questions that you're sure of the answer, or if not tell the poster that you're not sure because your answer above is quite misleading.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by JosAH View Post
    That trailing semi colon should go.
    Dang... missed that. Either too many or not enough Grolsch's!

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Instance variable spaces should be of primitive type int, not object of Car class.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by diamonddragon View Post
    Instance variable spaces should be of primitive type int, not object of Car class.
    No, this also is untrue and potentially misleading. It should be an array of Car. Period. How could an primitive ever be null?

    what you're probably trying to say is that the constructor should instantiate the Parking class's totalSpace's field, i.e.,

    Java Code:
        this.totalSpaces = totalSpaces;

  10. #10
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    Yes, it's created in the Parking class code, in its constructor.


    You may want to only answer questions that you're sure of the answer, or if not tell the poster that you're not sure because your answer above is quite misleading.
    I will keep that in mind. Thanks

  11. #11
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    Yes, it's created in the Parking class code, in its constructor.


    You may want to only answer questions that you're sure of the answer, or if not tell the poster that you're not sure because your answer above is quite misleading.
    I will keep that in mind. Thanks

  12. #12
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    No, this also is untrue and potentially misleading. It should be an array of Car. Period. How could an primitive ever be null?

    what you're probably trying to say is that the constructor should instantiate the Parking class's totalSpace's field, i.e.,

    Java Code:
        this.totalSpaces = totalSpaces;
    Of course, primitive can not be null, so if (spaces[i] == null) should be replaced with if (spaces[i] == 0).

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by diamonddragon View Post
    OF course, primitive can not be null, so if (spaces[i] == null) should be replaced with if (spaces[i] == 0).
    So without seeing all of his code, without seeing his Car class (which he tells us in his initial post that he already has), you declare that he should not use this class, that he must use an array of int? Based on what information do you make this declaration?

  14. #14
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    So without seeing all of his code, without seeing his Car class (which he tells us in his initial post that he already has), you declare that he should not use this class, that he must use an array of int? Based on what information do you make this declaration?
    As You say, without seeing his Car class.

  15. #15
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Boolean Method Problem

    First of all thank to all of you for the answers.
    Now, regarding this
    Firstly your line number 10 in Parking class should give you an error since you are using Car and car either it will have an uppercase or lowercase "c" depending
    upon your "car" or "Car" class.
    I have to admit that I did not post the original code since all the variables were wrote in Italian. I did modify the variables, methods and classes names in English to make the post easyer to read. While doing that, I made a typing error. In the original code "car" is actually "Car".

    Regarding this
    So without seeing all of his code, without seeing his Car class (which he tells us in his initial post that he already has), you declare that he should not use this class, that he must use an array of int? Based on what information do you make this declaration?
    As You say, without seeing his Car class.
    I apologize for not posting all the infos needed.
    Here is my Car class:
    Java Code:
    public class Car
    {
    	private String numberplate;
    	private int paidHours;
    
    	public Car(String aNumberplate, int somePaidHours)
    	{
    		numberplate = aNumberplate;
    		paidHours = somePaidHours;
    	}
    
    	public String getNumberplate()
    	{
    		return numberplate;
    	}
    
    	public int getPaidHours()
    	{
    		return paidHours;
    	}
    
    	public void addHours(int addedHours)
    	{
    		paidHours = paidHours + addedHours;
    	}
    }
    I hope this will help.
    I still don't get where the error could be anyway :(

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    I still don't get where the error could be anyway :(
    Jos must again be posting his answers with invisible ink, a problem that we've tried to have corrected time and again, but he never listens. I'll warn him again not to do this. Until then, you may wish to re-read his answer above, since it shows you probably what is wrong -- an errant semicolon that is short-circuiting the if block.

  17. #17
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    This:
    public void addHours(int addedHours)

    is better to write like this:
    public void setPaidHours(int addedHours)

    regarding convention for get and set methods.

  18. #18
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    This:
    public void addHours(int addedHours)

    is better to write like this:
    public void setPaidHours(int addedHours)

    regarding convention for get and set methods.
    ...
    When asking:
    if (spaces[i] == null)

    what happens if there is no object of class Car created, but only a reference variable?

  19. #19
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    Jos must again be posting his answers with invisible ink, a problem that we've tried to have corrected time and again, but he never listens. I'll warn him again not to do this. Until then, you may wish to re-read his answer above, since it shows you probably what is wrong -- an errant semicolon that is short-circuiting the if block.
    I see what you're saying there, sorry for missing Jos' answer.
    After erasing that horrible semi colon I'm still having the same exact issue i.e. getting "false" on the screen.
    Thanks for the answers again.

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    I see what you're saying there, sorry for missing Jos' answer.
    After erasing that horrible semi colon I'm still having the same exact issue i.e. getting "false" on the screen.
    Thanks for the answers again.
    Have you followed my recommendation to set your class's totalSpaces field in the constructor? If not, you understand that totalSpaces currently == 0 right? So the for loop won't even be called.

Page 1 of 2 12 LastLast

Similar Threads

  1. problem on expressing a boolean method
    By b.m in forum New To Java
    Replies: 9
    Last Post: 12-12-2010, 07:17 PM
  2. Boolean and Method help on Homework
    By gto400no1 in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:12 AM
  3. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 02:32 PM
  4. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 03:36 AM
  5. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 03:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •