University.java
University.java
java
1 /* Problem Statement
2
3 In a University, Student s1 is working on a set of projects.
4 Student s2 also works on same set of projects as s1 except for the second project, which s2
replaces with another project.
5 Write a program that defines two classes Student and University. Define a copy constructor to
create s2 from s1 such that changing the values of instance variables of either s2 or s1 does
not affect the other one. The program accepts name of student s2 and the new book contributed
by s2 as input.
6
7 • Class Student has/should have the following members.
8
9 Private instance variables String name and String[] projects to store student's name and
projects of the student respectively.
10 Required constructor(s)
11 Accessor methods getName() and getProject(int) to get the student name and the book at a
specific index respectively.
12 Mutator methods setName(String) and setProject(int,String) to set the name of the student and
the project at a specific index respectively.
13 • Class University has method main that does the following.
14
15 Two objects of Student s1 and s2 are created. s2 is created using s1.
16 name of Student s2 and the second project of s2 are updated by taking the input
17 Finally, name of s1, s2 and second project of s1 and s2 are printed
18 What you have to do
19
20 Define a constructor to initialize the instance variables in class Student.
21 Define a copy constructor in class Student to create s2 from s1.
22 Java documentation can be accessed at: https://docs.oracle.com/en/java/javase/11/docs/api
23
24 Note:
25 It has been observed that when there are several concurrent submissions, sometimes we get
ClassNotFoundExcepti
on. In this case, please refresh, try again and try submitting multiple
times.
26 */
27
28 import java.util.*;
29
30 class Student {
31 private String name;
32 private String[] projects;
33
34 // Constructor to initialize instance variables
35 public Student(String name, String[] projects) {
36 this.name = name;
37 this.projects = new String[projects.length]; // Create a new array to avoid aliasing
38 for (int i = 0; i < projects.length; i++) {
39 this.projects[i] = projects[i];
40 }
41 }
42
43 // Copy constructor
44 public Student(Student other) {
45 this.name = other.name;
46 this.projects = new String[other.projects.length]; // Create a new array to avoid
aliasing
47 for (int i = 0; i < other.projects.length; i++) {
48 this.projects[i] = other.projects[i];
49 }
50 }
51
52 public void setName(String name) {
53 this.name = name;
54 }
55
56 public void setProject(int index, String project) {
57 this.projects[index] = project;
58 }
59
60 public String getName() {
61 return name;
62 }
63
64 public String getProject(int index) {
65 return projects[index];
66 }
67 }
68
69 public class University {
70 public static void main(String[] args) {
71 Scanner sc = new Scanner(System.in);
72
73 String[] projects = {"Data Exploration", "Imaging", "MRI"};
74 Student s1 = new Student("Ranjit", projects);
75
76 Student s2 = new Student(s1); // Use copy constructor
77 s2.setName(sc.next());
78 s2.setProject(1, sc.next());
79
80 System.out.println(s1.getName() + ":" + s1.getProject(1));
81 System.out.println(s2.getName() + ":" + s2.getProject(1));
82 }
83 }