You will need to create your own project for this exercise.
For all assignments, please use the following Person
class (or your version of it!):
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Create a class Step1
and add the main method below. Next, implement the last missing line using forEach
and the customPrint
method. Note that it should just 1 line! Also, make sure you use the method operator to do so.
public class Step1 {
public static void main(String[] args) {
List<Person> values = Arrays.asList(new Person("Tom"), new Person("Jack"), new Person("Jill"));
// TODO: Print each person using "forEach" and the customPrint method.
}
public static void customPrint(Person p){
System.out.println(p.getName());
}
}
Create a class MyCustomPrinter
(or similar) and give it a basic implemention to behave as a printer:
public class MyCustomPrinter {
public void doPrint(Person p){
System.out.println(p.getName());
}
}
Next, create a class Step2
and add the following main method:
public static void main(String[] args) {
List<Person> values = Arrays.asList(new Person("Tom"), new Person("Piet"), new Person("Henk"));
MyCustomPrinter somePrinter = new MyCustomPrinter();
// TODO: Print each person using "forEach" and somePrinter.
}
Once again, make sure that you have used the method operator.
Finally, update your Person
class to include it’s own printing method (called doPrint()
). Create a class Step3
and include the following code:
public static void main(String[] args) {
List<Person> values = Arrays.asList(new Person("Tom"), new Person("Piet"), new Person("Henk"));
// TODO: Print each person using "forEach" the doPrint method located in the Person class.
}