In: Computer Science
Pre-Assignment
hom
Before you start, create a new Eclipse project and create one package in that project. The package should be called “HW3”
Problem 1
Create a class called “Person”. This class should have, at minimum, the following members and methods. I advise you to add others as you see fit.
// Members
firstName (string)
lastName (string)
age (int)
pAddress (Address) // This is composition! I suggest using the Address class we defined earlier!
pDOB (Date) // This is composition! I suggest using the Date class we defined earlier!
// Constructors
Person() // Default with age = 0, names null, and DOB and Address default for those classes
Person(firstName, lastName, age) // Address and DOB will be default for those classes
Person( Person x) // Copy constructor that does a DEEP COPY of the person object.
// Setters
setFirstName(String firstName)
setLastName(String lastName)
SetName(String firstName, String lastName)
setAge(int newAge)
setAddress(String houseNum, String street, String city, String state, String zip)
setDOB (int day, int month, in year)
// Getters
toString() // Print out the name, age, dob, and address (use toString from DOB and Address)
getAge() // return the age
getFirstName() // return the first name
getLastName() // return the last name
getDOB() // Return a COPY OF the date object HINT: Use the date copy constructor!
getAddress() // Return a COPY OF the address object HINT: Use the Address copy constructor!
Problem 2
Test your Person class by creating a new class (in the same “HW3” package and then use the methods to make sure they work properly. I provide no guidance on this since all you need to do is test the methods to make sure they work. Call each and make sure they don’t report errors or fail to execute correctly. Fix any bugs you find. Ask for help if you need it!!!
Problem 3
Create a new class file and call it “Student”. This class will EXTEND on the Person class by adding new data members and methods. Note that students are, in fact, people. This extension makes sense!
// Data members
stuID (String)
major (string)
level (int) // 0 = first-year, 1 = sophomore, 2 = junior, 3 = senior, 4 = post-grad
// Constructors
Student(String id, String lastName, String firstName, String major, int level)
Student( Student x) // Copy constructor that makes a DEEP COPY of Student x object
// Setters
Provide one setter for major, one setter for stuID, and one setter for level. Note that the “level” method should check the input to make sure it makes sense. If something besides a 0, 1, 2, 3, or 4 is provided, the setter should write a warning to screen and refuse to make the change.
// Getters
Provide one getter for major, one getter for stuID, and one getter for level.
// Provide an overrided toString method and use super to also use the inherited toString
toString (String)
// This method should write to screen something like this:
// “Jones, Mary, 21, 544 S Winston St., Kemble, KY 00000, Computer Science, 3”
// Note that you want to use the inherited toString() method here by saying super.toString()
// somewhere in your returned expression.
Problem 4
Create a new class in “HW3” package and call it “testStudent”. Use this class to create multiple student objects and person objects. Test all the methods for accuracy. Make sure you have correct behavior for all of your methods/constructors. Watch out for the copy constructors!!!!
If you have any query then ask me in comment section. Please upvote if you like the answer.
Person Class:
package HW3;
public class Person {
private String firstName;
private String lastName;
private int age;
Address pAddress = new Address();
Date pDOB = new Date();
public Person() {
this.age = 0;
this.firstName = null;
this.lastName = null;
this.pAddress = new Address();
this.pDOB = new Date();
}
public Person(String fn, String ln, int a) {
this.age = a;
this.firstName = fn;
this.lastName = ln;
this.pAddress = new Address();
this.pDOB = new Date();
}
public Person(Person X) {
this.age = X.age;
this.firstName = X.firstName;
this.lastName = X.lastName;
this.pAddress = X.pAddress;
this.pDOB = X.pDOB;
}
public void setFirstName(String fn) {
this.firstName = fn;
}
public void setLastName(String ln) {
this.lastName = ln;
}
public void setName(String fn, String ln) {
this.firstName = fn;
this.lastName = ln;
}
public void setAge(int a) {
this.age = a;
}
public void setAddress(int houseNum, String street, String city, String state, String zip) {
this.pAddress = new Address(houseNum, street, city, state, zip);
}
public void setDOB(int day, int month, int year) {
this.pDOB = new Date(day, month, year);
}
public String toString() {
String temp = "";
temp = this.firstName + ", " + this.lastName + ", " + this.age + ", " + this.pAddress.toString() + ", "
+ this.pDOB.toString();
return temp;
}
public int getAge() {
return this.age;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getAddress() {
return this.pAddress.toString();
}
public String getDOB() {
return this.pDOB.toString();
}
}
testPerson Class:
package HW3;
public class testPerson {
public static void main(String[] args) {
Person obj1 = new Person();
Person obj2 = new Person("Somesh", "Choudhary", 23);
String ob1 = obj1.toString();
String ob2 = obj2.toString();
System.out.println("Values after default constructor (obj1) : ");
System.out.println(ob1);
System.out.println("Values after parameterised constructor (obj2) : ");
System.out.println(ob2);
obj2.setAddress(25, "Jaato ka Mohalla", "Chhan", "Rajastha", "304001");
obj2.setDOB(23, 10, 1998);
System.out.println("Values after setting address and DOB in obj2 : ");
ob2 = obj2.toString();
System.out.println(ob2);
obj2.setAge(21);
System.out.println("Values after setting age to 21 in obj2 : ");
ob2 = obj2.toString();
System.out.println(ob2);
obj2.setFirstName("firstName");
System.out.println("Values after setting first name to firstName in obj2 : ");
ob2 = obj2.toString();
System.out.println(ob2);
obj2.setLastName("lastName");
System.out.println("Values after setting last name to lastName in obj2 : ");
ob2 = obj2.toString();
System.out.println(ob2);
System.out.printf("Age in obj 2 is : %d", obj2.getAge());
System.out.printf("\nFirst name in obj 2 is : %s", obj2.getFirstName());
System.out.printf("\nLast name in obj 2 is : %s", obj2.getLastName());
System.out.printf("\nAddress in obj 2 is : %s", obj2.getAddress());
System.out.printf("\nDOB in obj 2 is : %s", obj2.getDOB());
Person obj3 = new Person(obj2);
String ob3 = obj3.toString();
System.out.println("\nContents of obj3 after copying values of obj2 into it:");
System.out.println(ob3);
}
}
Student Class:
package HW3;
public class Student extends Person {
private String stuID;
private String major;
private int level;
Student(String id, String lastName, String firstName, String major, int level) {
this.stuID = id;
this.setFirstName(firstName);
this.setLastName(lastName);
this.major = major;
this.level = level;
}
Student(Student X) {
this.stuID = X.stuID;
this.major = X.major;
this.level = X.level;
this.setFirstName(X.getFirstName());
this.setLastName(X.getLastName());
this.pAddress = X.pAddress;
this.pDOB = X.pDOB;
}
public void setStuID(String sID) {
this.stuID = sID;
}
public void setMajor(String m) {
this.major = m;
}
public void setLevel(int l) {
if (l != 0 && l != 1 && l != 2 && l != 3 && l != 4) {
System.out.println("\nLevel input should be one of 0, 1, 2, 3 or 4!");
return;
} else
this.level = l;
}
public int getLevel() {
return this.level;
}
public String getStuID() {
return this.stuID;
}
public String getMajor() {
return this.major;
}
public String toString() {
String temp = "";
temp = super.toString() + ", " + this.stuID + ", " + this.major + ", " + this.level;
return temp;
}
}
testStudent Class:
package HW3;
public class testStudent {
public static void main(String[] args) {
Student obj1 = new Student("2015IMSCS020", "Choudhary", "Somesh", "CS", 3);
String ob1 = obj1.toString();
System.out.println("Values in obj1 after initializing with parameters are : ");
System.out.println(ob1);
obj1.setAge(22);
obj1.setAddress(25, "This Street", "Tonk", "jaipur", "302020");
obj1.setDOB(23, 10, 1990);
ob1 = obj1.toString();
System.out.println("Values in obj1 after setting age, DOB and address are : ");
System.out.println(ob1);
obj1.setMajor("Computer Science");
obj1.setStuID("CS00KY9");
/*
* Below Line would generate error case obj1.setLevel(5);
*/
obj1.setLevel(3);
ob1 = obj1.toString();
System.out.println("Values in obj1 after modification are : ");
System.out.println(ob1);
Student obj2 = new Student(obj1);
String ob2 = obj2.toString();
System.out.println("Values in obj2 (obj1 is copied in obj2) : ");
System.out.println(ob2);
}
}