In: Computer Science
I would like to compare my current code with that of someone else to be sure I did this assignment correctly. My main concern is with Problems 2 and 4. I sometimes struggle with testing the methods I have created. If you can, please answer all of the questions so that I may reference them if I am still lost on Problems 2 and 4. Each problem requires the one before it and so seeing the entire assignment as opposed to one problem would be immensely helpful. Thank you!
Introduction: This assignment covers the creation and use of basic inheritance in classes.
Submission: Complete each of the problems given below using Java and the Eclipse IDE. You will export your entire project solution as a .jar file for submission on Canvas. Follow the video tutorials for Eclipse on the Canvas site for guidance. The deadline for submission is posted on Canvas.
Pre-Assignment
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!
Address Class
package composition1;
public class Address {
private int houseNum;
private String street;
private String state;
private String city;
private String zip;
// Define constructors for class
// Default constructor
public Address()
{
this.houseNum = 0;
this.street = "";
this.state = "";
this.city = "";
this.zip = "";
}
// Alternate constructor
public Address(int pNum, String pStreet, String pCity,
String pState, String pZip)
{
this.houseNum = pNum;
this.street = pStreet;
this.state = pState;
this.city = pCity;
this.zip = pZip;
}
// toString Method
public String toString()
{
String temp = "";
temp= houseNum + " " + street + ",
" + city + ", " + state + " " + zip;
return temp;
}
// Getters
// Get houseNum
public int getHouseNum()
{
return this.houseNum;
}
// Get street
public String getStreet()
{
return this.street;
}
// Get state
public String getState()
{
return this.state;
}
// Get city
public String getCity()
{
return this.city;
}
// Get zip
public String getZip()
{
return this.zip;
}
// Setters
// Set the house number
public void setHouseNum(int pH)
{
this.houseNum = pH;
}
// Set the state value
public void setState(String pState)
{
this.state = pState;
}
// Set the city
public void setCity(String pCity)
{
this.city = pCity;
}
// Set the street value
public void setStreet(String pStreet)
{
this.street = pStreet;
}
// Set the zip code
public void setZip(String pZip)
{
this.zip = pZip;
}
}
Date Class
package composition1;
public class Date {
// Data member of class Date
private int day;
private int month;
private int year;
// Constructors for Date class
// Default constructor
public Date()
{
this.day = 1;
this.month = 1;
this.year = 1959;
}
// Alternate constructor
public Date(int y, int m, int d)
{
this.day = d;
this.month = m;
this.year = y;
}
// Getter methods
// toString method to print out the date in a common
format
public String toString()
{
return month + "/" + day + "/" +
year;
}
// Get year
public int getYear()
{
return this.year;
}
// Get month
public int getMonth()
{
return this.month;
}
// Get day
public int getDay()
{
return this.day;
}
// Setter Methods
// Set year value
public void setYear(int y)
{
this.year = y;
}
// Set day value
public void setDay(int d)
{
this.day = d;
}
// Set month value
public void setMonth(int m)
{
this.month = m;
}
}
// Constructors
Person() // Default with age = 0, names null, and DOB and Address default constructor 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.
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!!!!
Here is your solution. If you have any query then ask me in comment section. Please upvote if you like the answer.
Below is the 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;
}
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);
Student obj2 = new Student(obj1);
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);
}
}