
Kevin J.
asked 10/23/20I need to Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter method
class Student
{
private int id;
private String name;
public Student()
{
id = 8;
name = "John";
}
public int getid()
{
return id;
}
public String getname()
{
return name;
}
public void setid(int newid)
{
id = newid;
}
public void setname(String newname)
{
name = newname;
}
public String toString()
{
String result = Integer.toString(id)+" "+name;
return result;
}
}
class TestStudent1
{
public static void main(String args[])
{Student s0=new Student();
System.out.println(s0);
Student s1=new Student();
s1.setid(312);
s1.setname("Patrick");
System.out.println("ID is " + s1.getid());
System.out.println("The student name is " + s1.getname());
}
}
1 Expert Answer

Patrick B. answered 10/24/20
Math and computer tutor/teacher
class Student
{
private int id;
private String name;
private String zipCode;
//constructor
public Student(int ID, String Name, String zip)
{
this.id = ID;
this.name = new String(Name);
this.zipCode = new String(zip);
}
//copy constructor
public Student (Student student)
{
this.id = student.getID();
this.name = new String(student.getName());
this.zipCode = new String(student.getZipCode());
}
//use camelCase for your getter and setter names to make them
//more readable... always returns COPIES of the strings, so
//no one else can change them !!
public int getID() { return id; }
public String getName() { return new String(name); }
public String getZipCode() { return new String(zipCode); }
public void setID(int newid) { id = newid; }
public void setName(String newname) { name = new String(newname); }
public void setZipCode(String zip) { zipCode = new String(zip); }
@Override public String toString()
{
return (
new String(
"Id=" + Integer.toString(id)+" Name=>" + name + "< zipCode =>" + zipCode + "<"
)
);
}
public String SerializeToCSV() //comma separted values
{
return(
new String(
this.id + "," + this.name+ "," + this.zipCode + "\n"
)
);
}
public void Display(String debugMsg)
{
if (debugMsg!=null)
{
System.out.println("*************************************************");
System.out.println(debugMsg);
}
System.out.println("*********************************************");
System.out.println(" ID = " + this.id);
System.out.println(" name =>"+ this.name+"<");
System.out.println(" zip code = >"+ this.zipCode +"<");
System.out.println(" Serialized as String :>"+this.toString() + "<");
System.out.println(" Serialized as CSV String :>" + this.SerializeToCSV() + "<");
}
public static void main(String args[])
{
Student student1 = new Student (8,"John","44526");
Student student2 = new Student (312,"Patrick","32824");
//here is YOUR student record
int yourID=123; // set your id # here !!!
String yourZipCode = new String("?????-????"); // set your zip code here inside the double quotes !!!
Student student3 = new Student (yourID,"Kevin J.",yourZipCode);
//Mick and Mack and twins!!!
Student studentTwin1 = new Student(111,"Mick","43210");
Student studentTwin2 = new Student(studentTwin1); //calls the copy constructor
studentTwin2.setName("Mack");
studentTwin2.setID(112);
student1.Display("1st studnet object");
student2.Display("my student object");
student3.Display("your student object");
studentTwin1.Display("Twin #1");
studentTwin2.Display("Twin #2");
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Source code in the RESOURCES section under this link10/24/20