• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Editing ArrayList object boolean value

 
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I've been searching for simillar questions/forums and couldn't really find the answer for my problem therefore had to create my first forum account.
So I have this homework for school where i have to create a hospitals informational system.
I have created ArrayList for doctors and ArrayList for patient
Patients have this boolean ''isHospitalised" and my problem starts there. I need to create method addPatientToHospital which means i have to change the value of the boolean that's inside the ArrayList



I have been working around this code for hours now but nothiing seems to work
I am confident that i should return something different or the paramater for the method should be different. Not quite sure about anything anymore

Maybe someone could give me some tips ?
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
isHospitalized is a methodcall that return true or false?
you can't set hospitalized with isHospitalized it's a getter method, you need the setter method.
And your loop is gonna set  everyone that isn't hospitalized to hospitalized, you need a way to know who to hospitalize
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the response

I have to create many ways of how to add patients to hospital. 1 of the methods need to be like adding every, then theres a method thats gonna add by ID and a method thats gonna add by name;
isHospitalized is a methodcall that return true or false?  - I dont have the knowladge to answer to this question but i guess it is
I just tried with the setIsHospitalised - it works.
is there a way to change  isHospitalised permenantly? Right now i can print it out but when i start again they again isHospitalised = false

 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it should be something more like:

 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:Thank you for the response

isHospitalized is a methodcall that return true or false?  - I dont have the knowladge to answer to this question but i guess it is
I just tried with the setIsHospitalised - it works.



Based on what your're trying to do it seems like the isHospitalized is the getter method for your boolean.
As said before you can't set a variable by it's getter method, or you actually could, but it's not done.

Let us see you patientClass, you should also have a method setHospitalized;
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:
is there a way to change  isHospitalised permenantly? Right now i can print it out but when i start again they again isHospitalised = false


There is, if you set hospitalized to private, asign it's value in the constructor and don't provide a setter method.
But keep in mind, then you would never be able to discharge the patient.

But I think you mean more to save your changes, not by putting them in an arrayList, that's for sure.
You need some kind of database,  real or embedded that writes to a textfile
 
Marshal
Posts: 79178
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Which is the right place for a good answer

Does that code actually compile? Have you really got a method and field of the same name? It would be usual to call the field hospitalised and the method isHospitalised(). If you have such a field, it should have private access, so you would only set or acccess it via a method. Maybe the patient would have methods like goIntoHospital() comeOutOfHospital() and die(), which all alter the value of that field. Whichever way it works, the boolean field is in the Patient object, not the List.
Also, never write == true or == false. Those are poor style and very error‑prone. Every now and again we see somebody write = instead of == and now they have two errors for the price of one.What you are doing is putting all your patients into hospital. If your goIntoHospital() method is idempotent, i.e. nothing strange happens if you call it twice, all you have to do is iterate the entire List and send every patient into hospital. But the name you have given the method suggests that isn't what you want. It suggests that this is an instance method of the hospital class, and you would pass a Patient reference to the method, adding the patient to the hospital's complement of patients. And you can set the hospitalised field at the same time.

Now I can see problems. Is a patient in hospital because they appear in the List or because their hospitalised field is set? What if you admit the same patient twice, even better to two different hospitals simultaneously? What if a patient is discharged from hospital 1 and not from hospital 2? Do you really want Lists (remembering that Lists permit duplicates)? Do you want that field in the Patient object at all? What have you been told about that field?
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does seem much more advanced, yes


I updated my code, but something still doesnt seem to right. I can post the code of my main/Patient class too if needed.

I get

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method addNewPatientToHospital(Patient) in the type Hospital is not applicable for the arguments (ArrayList<Patient>)

at md1.Hospital.main(Hospital.java:155)



If i do put in the ArrayList<Patient> allPatients then I get the following :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method addNewPatientToHospital(ArrayList<Patient>) from the type Hospital

at md1.Hospital.main(Hospital.java:155)

 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do apoligise in advance for all the facepalms I might make during this thread for you.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . You need some kind of database,  real or embedded that writes to a textfile

Be careful to work out what the OP wants to do before suggesting lots of implementation details.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Be careful to work out what the OP wants to do before suggesting lots of implementation details.



Sorry, didn't mean to push the op in a direction. Just wanted to make clear that without somekind of storage everytime the program starts previous maded changes will be lost
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP: please post your full project. It would be easier to figure out what's going out.
Like for example by the name isHospitalized I automaticly asumed it was a method, never crossed my mind that it's most likely a variable like campbell Ritchie pointed out
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel, huge thanks to you and don't be sorry for anything. Everyone has a head of their own and can choose to take the advice or not Just a big thumbs up for you.
I did think about making the databse, but that might just be too comlicated for the amount of time I have to get this done.
I guess I might just leave it like that where it changes the boolean value for the moment where I launch the OP.
However that might mean that I have to input alot of other things inside this method to make it worthwhile.

However, I might use the other advice you gave aswell where I might want to implement new methods inside the Patient Class

To Campbell, that is a great tip on how I should write my code, i will for sure keep that in mind and try to never use == true / == false ever again as you said that might cause a misspelling issue which is going to cause many other issues with my code.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How about for improving to this? (beware: example is non-compileable code)


 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:I do apoligise in advance for all the facepalms . . . .

You have done nothing needing apologising for; we see far worse
I see you are using an IDE which lets you run code before you have fixed all the compile time errors. Don't. Make sure you have got rid of all red marks before you try running something. The error message sugests you are in a static method when you shouldn't be. See this FAQ. Follow the object‑oriented suggestions.
Having had another look at your second version of the method, I can see that you can use the hospitalised field to prevent duplicate admissions. But you need to add one patient, not all the patients in the List. Just as you can take a Car to the Garage for repairs:-If the car doesn't need repairs, the method does nothing to the Garage object, but if it does need repairs, it adds the Car to the List and does the repairs. The return value from that method tells the user whether the Car was actually repaired. If you take the same Car to a different Garage, they will be honest and not take the Car in for repairs.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


At the moment I can manage to change the boolean and print out the Patients that are now isHospitalised(true)

I have a huge feeling that this is totally wrong for further use  //I could use it but I dont think its good practice
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:. . . Sorry, didn't mean to push the op in a direction.

Apology accepted

Just wanted to make clear that without somekind of storage everytime the program starts previous maded changes will be lost

As DB suggested, that may be beyond the scope of the project. If it is necessary to store such data, they will have told him.

DB: why are you casting numbers to (short)? Don't use shorts; use ints. That will save no end of trouble with casts.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


To why I'm using short not int for the officeNum
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:. . . Unresolved compilation problem:
The method addNewPatientToHospital(Patient) in the type Hospital is not applicable for the arguments (ArrayList<Patient>). . . .

Aaaaaaaaaaaaaaaaaaah! I thought the addNewPatient method should take a single patient, not the whole List.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, originally as seen in the broken picture(right click, open in a new tab for imgur posted picture) it is meant to be a single person, as far as I know. I will try to implement the method you gave with cars and see how that goes.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am blown away by how active and full of advice givers this forum is. Surely did not make a wrong turn coming here. Thank you all
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:As DB suggested, that may be beyond the scope of the project. If it is necessary to store such data, they will have told him.


I understand what you mean, but I don't fully agree. When learning something you shouldn't be limited by the scope of the course.
Scope is often something students use as an excuse not be able to solve something. I can't do this, I haven't learned it...
When I was still in school our assignments went way beyond the scope of the course, there were things you had to solve and things that were nice if you were able to figure it out...
Just a general remark, not having a go at anyone...
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:


How about for improving to this? (beware: example is non-compileable code)




You are ahead of time. This is what I will most likely need to do in order to implement the code Campbell posted in my hospital.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:I am blown away by how active and full of advice givers this forum is. Surely did not make a wrong turn coming here. Thank you all


I'm surprised that stack overflow is still considerd the nr1 forum for devellopers. Since I discovered coderanch, it was mentioned in the book I used to prep for my oca, I haven't post a single question on stackoverflow.
What's the point? They get spammed right away and the chances of your post ever being read or next to zero
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:

Campbell Ritchie wrote:As DB suggested, that may be beyond the scope of the project. If it is necessary to store such data, they will have told him.


...



You are completely correct. At least in my case.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:
To Campbell, that is a great tip on how I should write my code, i will for sure keep that in mind and try to never use == true / == false ever again as you said that might cause a misspelling issue which is going to cause many other issues with my code.



if you want to check if a boolean is true or false you can simply use: for true or for false
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


ITS ALIVE!!! Thanks Campbell
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the same homework I have this other thing that i can't wrap my head around for a few days now.
There's this thing int[] indentificator. All patients and docotrs are Persons, in order to assign a unique ID to each person do I have to store each person in the array in order to get the ID? It would be nice to have it auto increment.
I totally need this ID because I will need this ID in order to delete patients from the hospital by ID.
Yes, there I will need to make some IF statements that ensure that the person indeed is a patient so i don't delete the doctor and all. But thats a different story.
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you were using a database you could autoIncrement the id, for now you'll have to manually set it.
A work around could be to each time take the lenght of your list and pass that to the constructor as id, since an listIndex starts at zero, lenght will give you the next available spot
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Daniel, so if i have a list of doctors AND a list of patients I should first put both of them inside a person list and then pass the lenght of the person list further and then somehow extract the ID, right?

/Not looking for an actual answer on how to do it, just wanna know if im on the right road
 
Daniel Demesmaecker
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a personsList, you dont need a doctor- or patientlist, since they're both persons. You  could use the instanceOf method to check of it the object is a patiënt or a doctor.
When using the lenght of the list as id keep in mind that the verry First time, so for the first object in the list,  you'll have to manully set the id to 0
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Bloomberg wrote:. . . ITS ALIVE!!!

Congratulations, Dr Frankenstein.

Thanks Campbell

That's a pleasure I presume you have managed to change my Garage and Car example so nobody will know you used it
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:If you have a personsList, you dont need a doctor- or patientlist, since they're both persons. You  could use the instanceOf method to check of it the object is a patiënt or a doctor.

Not convinced. I always get suspicious if I see casts, instanceof, or similar. What if the doctor falls ill and has to become a patient? I think I would stick to multiple Lists.


When using the lenght of the list as id keep in mind that the verry First time, so for the first object in the list,  you'll have to manully set the id to 0

You mean you would program the first index to be 0. And I still think the ID should be an int. We couldn't see the image; something went wrong with it.

DB: Afraid there are other things to say. Probably the most serious problem is the frequent use of the keyword static. The only plave I can see nowhere where static would be correct is in the heading for the main() method.
Another serious error is having non‑private fields.
The main method is too long. Look here. Most of that code should be moved elsewhere.
Your indentation is inconsistent; some code uses Allman indentation and some K&R. Choose one convention and stick to it throughout.
Your comments before methods are the wrong format; use /** comments */ instead. I don't have the time to teach you about that sort of comment, but look here. Also precede each of those comments with one empty line. A bit of whitespace improves legibility.
You are still using == true and == false.
Line 13 etc. Lines too long. More details about long lines here, also §4.2.
Lines 30‑31: Don't write the number equivalents of the chars. Use 'A', 'Z', 'a' and 'z'. Since chars are numbers, you can apply < > etc. to them. What happens if your patient is called Paddy O'Riley? Don't try applying a default value if your test fails: throw an IllegalArgumentException. You never use a zero char in Java®, only in other languages.
A lot of instances where you are using a for loop might benefit from a for‑each loop officially called enhanced for.
Line 78. If you get that test to succeed, something has gone wrong and you have violated your invariants.
Line 113. Your delete method should do the opposite of the add method. It isn't removing the patient from the List.
Don't use java.util.Date. That class is an abomination. Use the new classes e.g. LocalDate.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie, thank you for the many suggestions.
There are plenty of things I need to improve with my code-writing abilities. So far I have taken your suggestions in mind and implemented them in my code as good as my knowledge let me. There are still some issues I'm running in, like the massive one you mentioned - getting rid of "Static". I attempted to make everything but the main non static and transfer most of the code to the appropiate locations, for example :  all code about Patients transported to Patient Class, Doctors to Doctor class and so on. However then I started to get frusturated as nearly none of my code worked But that's okay, ill try to figure that out soon.
Nearly all of my fields are now private, so thats a good thing.
Inconsistend identation - things happen when you try to rush I tend implement the code as it should be only when it works, not good practice, but yeah.
Never use zero char - roger that. Using A, Z, a,z  is much easier and thats one great tip as I was confused with the ASCII and even know when i understand it - I dislike it.
Line 78 - wasn't working, wasn't even there a few moments after I posted, wanted to update the forum as quick as I could because who knows, you guys might of ran off to some kind of personal business.
Line 113 - Delete is doing the opposite, just changing the boolean from true to false.

I will post the updated code whenever I feel like its worth doing so. Now im heading back to attempting to get rid of ''static''.

And once again , huge THANK YOU for the tips. And apoligies for the late response.
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dave Bloomberg
Greenhorn
Posts: 26
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reply
    Bookmark Topic Watch Topic
  • New Topic