Question: If you run the following program, what will be printed on the screen? You must use your own student id for the parameter yourstudentid and

If you run the following program, what will be printed on the screen? You must use your own student id for the parameter yourstudentid and your name for the string "yourname" in the following statement

SWEngStudent std1 = new SWEngStudent(yourstudentid, "yourname", courses);

which type of copy is used in this program? Explain

---

public class SWEngCourse implements Cloneable {

String course1; String course2;

public SWEngCourse(String crs1, String crs2)

{

this.course1 = crs1; this.course2 = crs2;

}

protected Object clone() throws CloneNotSupportedException

{

return super.clone();

}

}

public class SWEngStudent implements Cloneable{

int stdId;

String studentName;

SWEngCourse swengcourses;

public SWEngStudent(int stdid, String stdName, SWEngCourse coursename)

{

this.stdId = stdid; this.studentName = stdName; this.swengcourses = coursename;

}

protected Object clone() throws CloneNotSupportedException

{

SWEngStudent astd = (SWEngStudent) super.clone();

astd.swengcourses =(SWEngCourse) swengcourses.clone();

return astd;

}

}

public class Main {

public static void main(String[] args) {

SWEngCourse courses = new SWEngCourse("SW Project Management", "OOP");

SWEngStudent std1 = new SWEngStudent(yourstudentid, "yourname", courses);

SWEngStudent std2 = null;

try

{

std2 = (SWEngStudent) std1.clone();

}

catch (CloneNotSupportedException e)

{

e.printStackTrace();

}

std2.stdId=2019456;

std2.studentName="Mehmet";

std2.swengcourses.course2 = "Data Structures";

std1.swengcourses.course1="Discrete Mathematics ";

System.out.println("Id: "+std1.stdId);

System.out.println("Name: "+std1.studentName);

System.out.println(std1.swengcourses.course1);

System.out.println(std1.swengcourses.course2);

System.out.println(std2.swengcourses.course1);

System.out.println(std2.swengcourses.course2);

}

ATTENTION: The language is JAVA

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!