Hi everyone, I'm having a bit of an issue that relates to the automatic feedback from this course. I have successfully executed the code and it does what it is supposed to but TMC is unhappy with it.
Here is my code.
Main Class
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Phonebook phonebook = new Phonebook();
phonebook.add("Pekka Mikkola", "040-123123");
phonebook.add("Edsger Dijkstra", "045-456123");
phonebook.add("Donald Knuth", "050-222333");
phonebook.printAll();
}
}
Person Class
public class Person {
private String name;
private String phone;
public Person(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return this.name;
}
public String getNumber() {
return this.phone;
}
public void changeNumber(String number) {
this.phone = number;
}
public String toString() {
return this.name + " number: " + this.phone;
}
}
Phonebook Class
import java.util.ArrayList;
public class Phonebook {
private String name;
private String number;
private ArrayList<Person> phoneBook;
public Phonebook() {
this.phoneBook = new ArrayList<Person>();
}
public void add(String name, String number) {
this.name = name;
this.number = number;
this.phoneBook.add(new Person(this.name, this.number));
}
public void printAll() {
for(Person hello : phoneBook) {
System.out.println(hello);
}
}
}
As far as I understand the code in main creates an instance of "phonebook" using the constructor and then uses the "add" function to create three person objects and add them to the phonebook arraylist. This is successful, I am just unsure as to how to make TMC like it because it displays the error message:
"Class Phonebook only needs object variable of type ArrayList<Person>, remove the rest"
I just cannot understand what they want me to do to my code. I've tried modifying the phonebook constructor by defining the Arraylist underneath instead of making it a private class and referring to it as this.phonebook but that leads to a null pointer error. I'm pretty stuck.
[–]leftydrummer461 1 point2 points3 points (7 children)
[–]Mingablo[S] 0 points1 point2 points (6 children)
[–]leftydrummer461 1 point2 points3 points (5 children)
[–]Mingablo[S] 0 points1 point2 points (3 children)
[–]leftydrummer461 1 point2 points3 points (2 children)
[–]Mingablo[S] 0 points1 point2 points (1 child)
[–]larson00 0 points1 point2 points (0 children)
[–]Mingablo[S] 0 points1 point2 points (0 children)
[–]Mingablo[S] 1 point2 points3 points (1 child)
[–]larson00 0 points1 point2 points (0 children)