Add 2 constructors to the Person class. One that takes no arguments and initializes the data to all 0’s and “” (empty strings). And one constructor that takes all 4 arguments, one argument for each property and then sets the properties to these arguments that are passed in. Lastly change the main to use these new Constructors. You will not need to call the set functions any more, but do not remove the set functions from your class.
Main Code -->
Person p1;
p1 = new Person("Rodney","Duncan","70 Bowman St. South Windsor, CT 06074","rduncan@gmail.com");
p1.display();
If there any mistakes in my code can you give me suggestions of how to fix them
Here my original code for Person Class:
public class Person {
private String FirstName;
private String LastName;
private String Address;
private String Email;
public void setFirstName(String fn) { FirstName = fn; }
public String getFirstName() { return FirstName;}
public void setLastName(String ln) { LastName = ln; }
public String getLastName() { return LastName;}
public void setAddress(String a) { Address = a; }
public String getAddress() { return Address;}
public void setEmail(String e) { Email = e; }
public String getEmail() { return Email;}
public String toString() {
return FirstName + ":" + LastName + ":" + Address + ":" + Email;
}
public void display() {
System.out.println("First Name = " + getFirstName());
System.out.println("Last Name = " + getLastName());
System.out.println("Address = " + getAddress());
System.out.println("Email = " + getEmail());
}
public static void main(String args []) {
Person p1;
p1 = new Person();
p1.setFirstName("Rodney");
p1.setLastName("Duncan");
p1.setAddress("70 Bowman St. South Windsor, CT 06074");
p1.setEmail("rduncan@gmail.com");
p1.display();
System.out.println(p1);
}
}
What I have tried:
Here the modify version of Person Class:
public class Person {
private String FirstName;
private String LastName;
private String Address;
private String Email;
Person(String FirstName, String LastName, String Address, String Email) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Address = Address;
this.Email = Email;
}
Person() {
this.FirstName = "";
this.LastName = "";
this.Address = "";
this.Email = "";
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public void setAddress(String Address) {
this.Address = Address;
}
public void setEmail(String Email) {
this.Email = Email;
}
public String getFirstName() {
return this.FirstName;
}
public String getLastName() {
return this.LastName;
}
public String getAddress() {
return this.Address;
}
public String getEmail() {
return this.Email;
}
void display() {
System.out.println("First Name : "+this.FirstName);
System.out.println("Last Name : "+this.LastName);
System.out.println("Address : "+this.Address);
System.out.println("Email : "+this.Email);
}
public String toString() {
return "FirstName: "+getFirstName() + "\nLastName: "+getLastName() + "\n" +this.Address.toString()
+"\nEmail: "+getEmail();
}
public static void main(String args []) {
Person p1;
p1 = new Person("Rodney","Duncan","70 Bowman St. South Windsor, CT 06074","rduncan@gmail.com");
p1.display();
}
}