Question

In: Computer Science

I would like to compare my current code with that of someone else to be sure...

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!!!!

Solutions

Expert Solution

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);
        }

}

Related Solutions

I actually would like you to take a look at my code and point it out...
I actually would like you to take a look at my code and point it out if there is anything wrong with it. I think there might be because there are some values that are the same in different arrays. QUESTION: C++ Use the random number generator in class Random to store a list of 1000 random integer values in an array. Create 3 arrays using this method. Apply each of the insertion, bubble, selection and shell sort algorithms to...
I have a problem with my code. It does not run. Please can someone check the...
I have a problem with my code. It does not run. Please can someone check the code and tell me where I went wrong? This is the question: Program Specification: Write a C program that takes the length and width of a rectangular yard, and the length and width of a rectangular house (that must be completely contained in the yard specified) as input values. Assuming that the yard has grass growing every where that the house is not covering,...
I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
How would I work the following problems out? I would like someone to show their work...
How would I work the following problems out? I would like someone to show their work so I could better understand the answers and thought process. Genetics Problems For the first couple of problems, you will be working with guinea pigs. Their coat color shows an example of complete dominance - black (B) is dominant over brown (b). 1. A heterozygous black male is crossed with a heterozygous black female. What would be the resulting phenotypic ratio in the offspring?...
I am working on these questions but I would like someone to review them before I...
I am working on these questions but I would like someone to review them before I submit and add any addition information I may have missed or worse gotten wrong! I provided the questions and then my answers to them. 1) Explain what is blacklisting and whitelisting? 2) iptables: Compare -j DROP vs -j REJECT. Which option would you use to implement a firewall rule that blocks incoming packets and why? 3) State the iptables command you would use to...
I have written this paper for my public speaking class,and I would appreciate it if someone...
I have written this paper for my public speaking class,and I would appreciate it if someone revised it for me to make sure my punctuations are correct as well as everything else.Thank you, Public Speaking…Dealing with It Often when a person thinks of public speaking the thoughts a person may have will vary from the next, because while one person may have experience the other person may not. Being able to communicate effectively requires that a person acknowledges the frame...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
Can someone tell me how to fix warning msg in my code of C ++? I...
Can someone tell me how to fix warning msg in my code of C ++? I got run-time error for this question please help me asap! Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^...
In which folder of XAMPP would I put in my PHP code
In which folder of XAMPP would I put in my PHP code
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would...
Below is my code, Replace the 2 static_cast codes to a simpler C++ code. I would like to find another way to compile the program without using static_cast in my code. Thank you #include <iostream> #include <iomanip> using namespace std; class Population { private: int population, birth, death; public: Population() { population = 2; birth = 0; death = 0; } Population(int x, int y, int z) { if (x < 2) population = 0; else population = x; if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT