Can someone help me with this homework question please? 1. Write a Java class na
ID: 666079 • Letter: C
Question
Can someone help me with this homework question please?
1. Write a Java class named Person with the following components(Total 30 pts):
- A private data variable 'name' of type String (1 pts)
- A private data variable 'email' of type String (1 pts)
- A private data variable 'phone' of type String (1 pts)
- A no-arg constructor (2 pts)
- A constructor with String name as a paramater (2 pts)
- Provide accessors and mutators for all data variables (18 pts)
- Provide a toString() method to print the state of the object(5 pts).
AND
2. Write a Java class named Driver to accomplish the following(Total 20 pts):
- Declare and create an array reference for the following 3 persons(8 pts):
1. name = Joseph, phone = 404.123.4567, address = jos1@student.gsu.edu
2. name = Jane, phone = 404.123.8910, address = jan1@student.gsu.edu
3. name = Jose, phone = 404.123.8765, address = jos2@student.gsu.edu
- After creating the array above, change Joseph's name to John using the correct mutator(2 pts).
- Travese through the array, printing the state of each Person object (10 pts)
NOTE: Must write Java code.
Explanation / Answer
Person.java
import java.util.Scanner;
public class Person{
private String name;
private String email;
private String phone;
public Person(){
}
public Person(String name){
}
public Person(String name, String phone, String email) {
this.name=name;
this.phone=phone;
this.email=email;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getEmail(){
return email;
}
public String getPhone(){
return phone;
}
public void setEmail(String email)
{
this.email=email;
}
public void setPhone(String phone){
this.phone=phone;
}
public String toString(){
return "Name:"+name+" email:"+email+" phone:"+phone;
}
}
Driver.java
public class Driver {
public static void main(String[] args) {
Person[] s=new Person[3];
s[0]=new Person("Joseph","404.123.4567","jos1@student.gsu.edu");
s[1]=new Person("Jane","404.123.8910","jan1@student.gsu.edu");
s[2]=new Person("Jose","404.123.8765","jos2@student.gsu.edu");
s[0].setName("John");
for(int i=0;i<3;i++){
System.out.println(s[i].toString());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.