Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Instructions/Hints/question to make changes to code : First, Complete the constructor Person(String name) so that it sets the member variable name to the argument name.

Instructions/Hints/question to make changes to code :
First, Complete the constructor "Person(String name)" so that it sets the member variable "name" to the argument "name". Also, add the "public String toString()" method and then implement it (i.e., override the "Object#toString()" method) so that it returns the name of the Person instance on which the method is called.
When you finish this task, the following code in Person#main(String[]) will output "Emily":
new Person("John"); // construct a Person instance whose name is "John"
Person p = new Person("Emily"); // construct a Person instance whose name is "Emily"
System.out.println(p); // output the name of the last Person instance (i.e., "Emily")
Your code will also pass the unit test named "test1()" in "UnitTests.java".


Completing Student.java
-------------------------------
Then, Change the "Student" class so that it inherits the member variable "name" from the "Person" class.
Also, complete the the constructor "Student(String name, int studentID)" so that the "Student" instance being constructed can remember the "name" and "studentID" given to the constructor.
Finally, you need to add the "public String toString()" method so that it returns a string containing the name and student ID in the form of "[name], [studentID]" (e.g., "Ken, 2345").
When you finish this task, the following code in Student#main(String[]) will output "Ken, 2345":
new Student("Chloy", 1234);
Student s = new Student("Ken", 2345);
System.out.println(s); // output the name and student ID of the last Student instance (i.e., "Ken, 2345"))
Your code will also pass the unit test named "test2()" in "UnitTests.java".


Then, Change Student.java in order for the studentCount() method to return the number of Student instances constructed since the beginning of the Java program.
When you finish this task, the following code in Student#main(String[]) will output "2:
System.out.println(s.studentCount()); // output the number of Student instances constructed so far (i.e., 2)
Your code will also pass the unit test named "test3()" in "UnitTests.java".
-----------------------
Below are the class code to make changes :
*********************
Person.java
public class Person {
/**
* The name of this {@code Person}.
*/
String name;
/**
* Constructs a {@code Person} instance.
*
* @param name
* the name of the {@code Person}
*/
public Person(String name) {
}
/**
* The main method of the {@code Person} class.
*
* @param args
* the program arguments
*/
public static void main(String[] args) {
new Person("John"); // construct a Person instance whose name is "John"
Person p = new Person("Emily"); // construct a Person instance whose name is "Emily"
System.out.println(p); // output the name of the last Person instance (i.e., "Emily")
}
}
***************************
Student.java
public class Student {
/**
* The number of {@code Student} instances constructed so far.
*/
int studentCount = 0;
/**
* Constructs a {@code Student} instance.
*
* @param name
* the name of the {@code Student}
* @param studentID
* the ID of the {@code Student}
*/
public Student(String name, int studentID) {
studentCount++;
}
/**
* Returns the number of {@code Student} instances constructed so far.
*
* @return the number of {@code Student} instances constructed so far
*/
public int studentCount() {
return studentCount;
}
/**
* The main method of the {@code Student} class.
*
* @param args
* the program arguments
*/
public static void main(String[] args) {
new Student("Chloy", 1234); // construct a Student instance whose name is "Chloy" and student ID is 1234
Student s = new Student("Ken", 2345); // construct a Student instance whose name is "Ken" and student ID is 2345
System.out.println(s); // output the name and student ID of the last Student instance (i.e., "Ken, 2345"))
System.out.println(s.studentCount()); // output the number of Student instances constructed so far (i.e., 2)
}
}
************************
UnitTests.java
import static org.junit.Assert.*;
import org.junit.Test;
*/
public class UnitTests {
* Tests the Task 1 implementations.
*
* @throws Exception
* if an error occurs
*/
@Test
public void test1() throws Exception {
assertEquals("John", "" + new Person("John"));
assertEquals("Emily", "" + new Person("Emily"));
}
/**
* Tests the Task 2 implementations.
*
* @throws Exception
* if an error occurs
*/
@Test
public void test2() throws Exception {
assertEquals("Chloy, 1234", "" + new Student("Chloy", 1234));
assertEquals("Ken, 2345", "" + new Student("Ken", 2345));
}
/**
* Tests the Task 3 implementations.
*
* @throws Exception
* if an error occurs
*/
@Test
public void test3() throws Exception {
assertEquals(3, new Student("Tom", 3456).studentCount());
assertEquals(4, new Student("Jenny", 4567).studentCount());
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

Explain why you agree or disagree with this statement.

Answered: 1 week ago