Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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 -->

Java
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:
Java
public class Person {
	
	//   ========================== Properties ===========================
	private String FirstName;
	private String LastName;
	private String Address;
	private String Email;
	
	//   ==========================  Behaviors  ==========================
	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;}
	
 	//Returning String
	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());
		
	} //end display()
	
	
	
	
	
	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();
		
		
		//Test out toString() method
		System.out.println(p1);
	} //end main
	
} //end class


What I have tried:

Here the modify version of Person Class:
Java
public class Person {
	
	//   ========================== Properties ===========================
	private String FirstName;
	private String LastName;
	private String Address;
	private String Email;
	
	//constructor with parameters
	Person(String FirstName, String LastName, String Address, String Email) {
		this.FirstName = FirstName;
		this.LastName = LastName;
		this.Address = Address;
		this.Email = Email;
	}
	
	//constructor with no parameters
	Person() {
		this.FirstName = "";
		this.LastName = "";
		this.Address = "";
		this.Email = "";
	}
	
	//   ==========================  Behaviors  ==========================
	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;
	}
	
	//method that displays person data
	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);
	}
	
	//overriding toString method
	public String toString() {
		return "FirstName: "+getFirstName() + "\nLastName: "+getLastName() + "\n" +this.Address.toString()
		+"\nEmail: "+getEmail();
	}
	
	//main method
	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();
	}
	}
Posted
Updated 20-Mar-18 21:12pm
Comments
Richard MacCutchan 21-Mar-18 5:11am    
What is the question?

1 solution

As per your question I just understand that you have 2 constructors are using.

1) Default Constructor &
2) 4 Parameterized

But I can not get what actual your problem is what?



Your Output of above coding is :

First Name : Rodney
Last Name : Duncan
Address : 70 Bowman St. South Windsor, CT 06074
Email : rduncan@gmail.com
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900