Question: public class Person { public String firstName; public String lastName; public int idNum; public String city; public Person() { firstName = ; lastName = ;
public class Person { public String firstName; public String lastName; public int idNum; public String city;
public Person() { firstName = ""; lastName = ""; idNum =0; city =""; }
public Person(String firstName, String lastName, int idNum, String city) { this.firstName = firstName; this.lastName = lastName; this.idNum = idNum; this.city = city; }
@Override public String toString() { return "Person [firstName=" + firstName + ", lastName=" + lastName + ", idNum=" + idNum + ", city=" + city + "]"; }
public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getIdNum() { return idNum; }
public void setIdNum(int idNum) { this.idNum = idNum; }
public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
in java code finish this
import java.util.ArrayList;
/** * ITEC 3150 - ITEC 2140 and 2150 Review * * @author wjin * */
public class PersonTestUsingArrayList {
/** * Method: main * @param args */ public static void main(String[] args) { //Create an empty arrayList of Person and assign it to the variable people ArrayList
/* Create Person objects for the following people and * add them to the array list people. * * Id is the 1st item on each line, then first name last name and city. 1, Nana Agyeman, Buford 2, Joseph Anderson, Duluth 3, Kyle Brooks, Lawrenceville 4, Joshua Broughton, Dacula 5, Demetri Clark, Lilburn */ Person person1 = new Person("Nana", "Agyeman", 1, "Buford"); people.add(person1); Person person2 = new Person("Joseph", "Anderson", 2, "Duluth"); people.add(person2); Person person3 = new Person("Kyle", "Brooks", 3, "Lawrenceville"); people.add(person3); Person person4 = new Person("Joshua", "Broughton", 4, "Dacula"); people.add(person4); Person person5 = new Person("Demetri", "Clark", 5, "Lilburn"); people.add(person5);
/* * Print the whole array list relying on the toString method of Person. * Please do not use any loop. */ System.out.println("The array list of people:"); System.out.println(people);
/* * Print the array list using a loop. Print each object in the following format: 1: Nana Agyeman @ Buford 2: Joseph Anderson @ Duluth 3: Kyle Brooks @ Lawrenceville 4: Joshua Broughton @ Dacula 5: Demetri Clark @ Lilburn */ System.out.println(" The people from the file are:"); for (Person p: people) { System.out.println(p.getIdNum() + ": " + p.getFirstName() + " " + p.getLastName() + " @ " + p.getCity()); } /* * Add another person into the array list: * 6, Joni Elshani, Atlanta */
/* * Print all the people in the array list */ System.out.println(" After adding the sixth person, the people are:"); for (Person p: people) { System.out.println(p.getIdNum() + ": " + p.getFirstName() + " " + p.getLastName() + " @ " + p.getCity()); } /* * Remove the first element from the array list: */ /* * Print all the people in the array list */ System.out.println(" After removing the first person, the people are:"); for (Person p: people) { System.out.println(p.getIdNum() + ": " + p.getFirstName() + " " + p.getLastName() + " @ " + p.getCity()); } }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
