• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm trying to create an object-oriented elevator program dealing with an array.  RSS feed

 
Oye Benny
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[I copied over my java files to gist.](https://gist.github.com/OyeBenny/d9c1a67705271254ff43225fe6074ebc)     

    
I believe I'm in the last stretch of finishing this assignment however, I get this error when I run it right now.   
Would appreciate any help in correcting this issue.      
If there's anything else you need from me, please feel free to ask.      

   
I'm essentially trying to set up a class with instance variables for a name and a weight (Person).      
Set up an another class (PersonList) with an instance variable of an array of Persons.       
THEN use functions/methods to handle the array processing, especially a method to determine how many people can get on the elevator, and etc.    
THEN try to make methods for the sorts and a method to printout the array (toString() method) in the PersonList class.
       
But like I said, I get this error below:
       
  
     
     ----jGRASP exec: javac -g Elevator.java
    Elevator.java:18: error: constructor PersonList in class PersonList cannot be applied to given types;
            PersonList persons = new PersonList();              
                                 ^
      required: int
      found: no arguments
      reason: actual and formal argument lists differ in length
    Elevator.java:21: error: cannot find symbol
               persons.getPersons(0);
                      ^
      symbol:   method getPersons(int)
      location: variable persons of type PersonList
    Elevator.java:22: error: cannot find symbol
               persons.getPersons(1);                                 //17 more times.


 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the first thing I see is that your constructor requires an integer and the code you put in your message does not have an integer as a parameter when you are constructing persons.
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second thing I see is that if you pass an int less than 12 to your PersonsList then your going to get out of bounds errors because your array will be too small.
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The next thing I see wrong is in the constructor which messes up your count.  It appears count is the number entries in your array except you already enter 12 entries but count remains 0.
 
Oye Benny
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michelle Streeter wrote:the first thing I see is that your constructor requires an integer and the code you put in your message does not have an integer as a parameter when you are constructing persons.


How do I correct this?
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another error is that your method in PersonsList is getPerson.  But you are calling persons.getPersons(0).
 
Oye Benny
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michelle Streeter wrote:Another error is that your method in PersonsList is getPerson.  But you are calling persons.getPersons(0).


so what is the proper nomenclature?
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a school project?  The reason I ask is that I have pointed you in the right direction.  My hope is that you look at what I am saying and if you do not understand, then read about it to learn.  You can even google for answers about the suggested solutions.  If I give you the answer then you really wont learn.
 
Oye Benny
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michelle Streeter wrote:Is this a school project?  The reason I ask is that I have pointed you in the right direction.  My hope is that you look at what I am saying and if you do not understand, then read about it to learn.  You can even google for answers about the suggested solutions.  If I give you the answer then you really wont learn.


Yes, it is honestly. However, it's a group of us working on this and the 4 of us just simply do not understand how to properly call arrays or, even more so, how to debug this.
Hints would be very much appreciated.
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use the same words you used in your class.   In your PersonsList class you used getPerson(int size)   Not sure I would use size.  Maybe better would be index or ndx.  So when you call persons.getPersons(0)   the compiler looks for a method called getPersons but it only finds getPerson.  Since getPerson makes sense in your class.  then your call should reflect that with persons.getPerson(0)
 
Oye Benny
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michelle Streeter wrote:You have to use the same words you used in your class.   In your PersonsList class you used getPerson(int size)   Not sure I would use size.  Maybe better would be index or ndx.  So when you call persons.getPersons(0)   the compiler looks for a method called getPersons but it only finds getPerson.  Since getPerson makes sense in your class.  then your call should reflect that with persons.getPerson(0)


Like so?

https://gist.github.com/OyeBenny/d9c1a67705271254ff43225fe6074ebc

Something tells me I'm not following correctly with "index".
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have given you hints.  

As for the constructor for PeoplesList.  Its bad practice to put in data like you are.  But even if you dont mind that, your count needs to be set to 12.  Also, if your going to have data entered in at the constructor like this but your constructor allows you to enter the size of the array and you know you need at least 12 then you need to check if the size is greater than 12 and if not then you need to set size to 12.   Another way to have your constructor set count correctly would be to call the add function to add in the entries rather than entering them directly into the array.  Also, in your add function, you need to check to make sure count is not greater than the size of your array or it will crash.
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, some of your errors do have to do with the array but more so that you have not dealt with them yet.  Most of the errors you have in this project has nothing to do with the array but how Java works.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

* Do you use the parameter index?
* What method are you calling in the return line and why?
 
Oye Benny
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
* Do you use the parameter index?
* What method are you calling in the return line and why?


I'm afraid I'm just completely lost.
 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I have made the changes I was referring to.  I am not including everything you posted on GitHub.  The first fix I made was in the Person class in your sets.  Both were assigning the values backwards.
In the personlist, i fixed the getperson and the add.  I also added some checks to make sure you dont get errors. 
in the Elevator class in the main method.  use persons.getPerson(0)  and so on.



 
Michelle Streeter
Ranch Hand
Posts: 120
Eclipse IDE Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After quite a bit of effort trying to figure out what your code was suppose to do, I finally got this result.

I discovered your code is suppose to read from the elevator.txt file to build the array rather than how your code is doing it now.
Also, your text file does not have an Anne
Not sure why you have scanner in the person class
Also, I think your sorts are not correct.

Since I had no idea where to put the Anne data, I left it out.  So here is my output

You have exceeded the weight limit!
In the elevator 6 people got on.
The weight is 1055
After the people were sorted by weight 8 people got on.
The weight is 1000
After the people were sorted by name 6 people got on.
The weight is 830
The second method worked the best

If I add Anne as the first record, here is the results:

You have exceeded the weight limit!
In the elevator 7 people got on.
The weight is 1085
After the people were sorted by weight 9 people got on.
The weight is 1030
After the people were sorted by name 7 people got on.
The weight is 860
The second method worked the best

It looks like you have alot of reading ahead of you. 
I would suggest learning about constructors and constructor overloading
Learning how Methods work and how to call them.  Its important that the method names in your code match the ones in your class.
Learning how to read a text file using Scanner
Learn how to sort.

Good Luck
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oye Benny wrote:
Knute Snortum wrote:
* Do you use the parameter index?
* What method are you calling in the return line and why?


I'm afraid I'm just completely lost.

All right, let's start at the beginning.  Do you know how to pass an argument to a method?  If not, read this.

You have a parameter called index but you don't use it.  People using your method would expect that index would be an index into the persons array.  But instead it calls the method getpersons.  Notice the capitalization.  getpersons is different from getPersons in Java, that is, it is case sensitive.

But do you want to call getPersons from inside getPersons.  Probably not.  If you did, it would call getPersons which would call getPersons which would call getPersons... and this wouldn't stop until Java ran out of memory.  This is called recursion, and it can be a good thing but in your case it's not.

You probably wanted something like:

Things to think about: does this method really "get persons" or does it just get one person?  If the latter, then maybe the method would be better named getPerson.  Also, what happens if some passes an index that is greater than any that are in persons?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!