Create a new Java project called inlab4. b) Create a package called model and a
ID: 3574169 • Letter: C
Question
Create a new Java project called inlab4. b) Create a package called model and a package called test. c) In the model package, add the following class: package model; import java.util.ArrayList; public abstract class Person {private int id; private String name; private static ArrayList persons=new ArrayList(); public Person(int id, String name) {this.id = id; this.name = name; persons.add(this);} public int getId() {return id;} public void setId(int id) {this.id = id;} public String getName() {return name;} public void setName(String name) {this.name = name;} public static ArrayList getPersons() {return persons;} public abstract String getClassName(); public String toCSV() {return id + ", " + name +", "+getClassName();}}Explanation / Answer
Please follow the code and comments for description :
CODE :
package model; // required package
import java.util.ArrayList; // required imports
public abstract class Person { // abstract class
private int id; // instance variables
private String name;
private static ArrayList<Person> persons = new ArrayList<>();
public Person(int id, String name) { // argument constructor
this.id = id;
this.name = name;
persons.add(this); // add the data to the list
}
// getters and setters for the instance variables respectively
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static ArrayList<Person> getPersons() {
return persons;
}
public abstract String getClassName(); // method that return the class name
public String toCSV() { // method that return the data required
return id + "." + name + "," + getClassName();
}
}
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.