Knowee
Questions
Features
Study Tools

public class Person{             int age;             String name;             public Person(String n, int a){                 age = a;                 name = n;             }             public String toString(){                 return (name+ ":" + age );             }         }         public class Employee extends Person implements Cloneable {             public Employee(String n, int a){                 super(n, a);             }             public Employee clone()throws CloneNotSupportedException{                 return (Employee)super.clone();              }         }         public class Test {             public static void main(String[] args) {                 Employee[] e1 = {new Employee("Hari",30), new Employee("geeta",23)};                 Employee[] e2 = e1.clone();                 e2[1].name = "rani";                 System.out.println(e1[1] + ", " + e2[1]);              }          }

Question

public class Person{             int age;             String name;             public Person(String n, int a){                 age = a;                 name = n;             }             public String toString(){                 return (name+ ":" + age );             }         }         public class Employee extends Person implements Cloneable {             public Employee(String n, int a){                 super(n, a);             }             public Employee clone()throws CloneNotSupportedException{                 return (Employee)super.clone();              }         }         public class Test {             public static void main(String[] args) {                 Employee[] e1 = {new Employee("Hari",30), new Employee("geeta",23)};                 Employee[] e2 = e1.clone();                 e2[1].name = "rani";                 System.out.println(e1[1] + ", " + e2[1]);              }          }

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The given code is written in Java.

Step 1: Define a class named "Person" with two instance variables: "age" of type int and "name" of type String. The class has a constructor that takes two parameters, "n" of type String and "a" of type int, and assigns the values of these parameters to the instance variables.

Step 2: The class "Person" has a method named "toString()" which returns a String representation of the object. In this case, it returns the concatenation of the "name" and "age" variables, separated by a colon.

Step 3: Define a class named "Employee" which extends the "Person" class and implements the "Cloneable" interface. The "Employee" class has a constructor that takes two parameters, "n" of type String and "a" of type int. It calls the superclass constructor using the "super" keyword to initialize the "name" and "age" variables.

Step 4: The "Employee" class overrides the "clone()" method from the "Cloneable" interface. It calls the "clone()" method of the superclass using the "super" keyword and casts the result to an "Employee" object.

Step 5: Define a class named "Test" which contains the "main" method.

Step 6: Inside the "main" method, create an array of "Employee" objects named "e1" with two elements. The first element is initialized with the values "Hari" and 30, and the second element is initialized with the values "geeta" and 23.

Step 7: Create a new array of "Employee" objects named "e2" and assign it the value of "e1.clone()". This creates a shallow copy of the "e1" array.

Step 8: Modify the "name" variable of the second element in the "e2" array to "rani".

Step 9: Print the "name" and "age" of the second element in both the "e1" and "e2" arrays using the "System.out.println()" method.

This problem has been solved

Similar Questions

t answerWhich of these method of Object class can clone an object?

public class Employee { public int id; public String name; private int sal; public Employee(int id, String name , int sal) { this.id = id; this.name = name; this.sal = sal; } public void setSal(int sal) { this.sal = sal; } public int getSal() { return sal; } public static List<Employee> getEmpList() { List<Employee> list = new ArrayList<>(); list.add(new Employee(111, "A", 2000)); list.add(new Employee(222, "B", 3000)); list.add(new Employee(333, "C", 4000)); list.add(new Employee(444, "D", 5000)); return list; }}public class Appp { public static void main(String[] args) { List<Employee> list = Employee.getEmpList(); for(Employee e : list) { //line 1 e.setSal(e.getSal()*2); //line 2 } //line 3 }}Choose the appropriate option to replace code line 1 to 3 to get the same output from the program. listEmployees.forEach(e -> e.setSal(e.getSal()*2));for(Employee e : listEmployees) {e -> e.setSal(e.getSal()*2);}for(Employee e : listEmployees) {forEach(e -> e.setSal(e.getSal()*2));}for(Employee e : listEmployees) {e.forEach() -> e.setSal(e.getSal()*2);}SubmitPowered by Infosys Wingspan

what is the result of the following piece of code:public class Person{ public void talk(){ System.out.print("I am a Person"); }}public class Student extends Person{ public void talk(){ System.out.print("I am a Student"); }}public class Test{ public static void main(String args[]){ Person p = new Student(); p.talk(); }}I am a Person I am a Student I am a Person I am a StudentI am a Student I am a Person

B A class A  {    String name="A";    public String getName()  {        return name;    }      String greeting()  {        return "class A";    }}class B extends A  {    String name="B";          String greeting()  {        return "class B";    }  }public class Test   {    public static void main(String arg[])  {        A a=new A();        A b=new B();        System.out.println(a.greeting()+" has name "+a.getName());        System.out.println(b.greeting()+" has name "+b.getName());    }} Place the names "A" and "B" in the following output. class has name                            class has name

Select the correct answerWhich of these method of Object class can clone an object?Optionscopy()Objectcopy()Object clone()clone()

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.