#How do I return it and why does the tostring not work here?
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
I'm sorry, but as an AI text-based model, I cannot view or process images. However, I can help you with the code if you provide it in text format. Please copy and paste the relevant code here, and I'll do my best to assist you with your question about returning and toString() method in Java.
package Exercise07_HeightOrder;
public class Main {
public static void main(String[] args) {
Room room = new Room();
System.out.println("Empty room? " + room.isEmpty());
room.add(new Person("Lea", 183));
room.add(new Person("Kenya", 182));
room.add(new Person("Auli", 186));
room.add(new Person("Nina", 172));
room.add(new Person("Terhi", 185));
System.out.println("Empty room? " + room.isEmpty());
System.out.println("");
for (Person person : room.getPersons()) {
System.out.println(person);
}
}
}
``` this is the main code
package Exercise07_HeightOrder;
// Don't modfy this class
import java.util.Objects;
public class Person {
private String name;
private int height;
public Person(String name, int height) {
this.name = name;
this.height = height;
}
public String getName() {
return name;
}
public int getHeight() {
return height;
}
@Override
public String toString() {
return this.name + " (" + this.height + " cm)";
}
// Created with the insert code functionality of NetBeans
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if (this.height != other.height) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
}
``` this the person class
package Exercise07_HeightOrder;
import java.util.ArrayList;
public class Room {
private ArrayList<Person> room;
public Room() {
this.room = new ArrayList<>();
}
public void add(Person person) {
this.room.add(person);
}
public boolean isEmpty() {
if (this.room.isEmpty()) {
return true;
} else {
return false;
}
}
public ArrayList<Person> getPersons() {
for (Person person : this.room) {
return person;
}
}
}
``` and this the room class
im at the point where I have to return the persons in the room
So you need to implement getPersons?
Easy enough. Since you want to return an array list of persons and you already have an arraylist of persons you can just return room. No for loop required
The variable name room is very bad though. You should rename it to persons or containedPersons
(the exercise said it and if I rename it wont count because it cant find the class)
Bruh for real?
I feel so stupid right now
No no I don't mean renaming the class but rather the room variable
yes if I rename the exercise will search for a class that is called room and it wont find that so it will give me an error
oh yeah I can do that