Question

In: Computer Science

Pre-Assignment hom Before you start, create a new Eclipse project and create one package in that...

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

Solutions

Expert Solution

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

}

Related Solutions

Using eclipse IDE, create a java project and call it applets. Create a package and call...
Using eclipse IDE, create a java project and call it applets. Create a package and call it multimedia. Download an image and save it in package folder. In the package, create a java applet where you load the image. Use the drawImage() method to display the image, scaled to different sizes. Create animation in Java using the image. In the same package folder, download a sound. Include the sound in your applets and make it repeated while the applet executes....
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse then write...
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse then write a class P5 with a main method, and put all of the following code into the main method: Instantiate a single Scanner object to read console input. Declare doubles for the gross salary, interest income, and capital gains. Declare an integer for the number of exemptions. Declare doubles for the total income, adjusted income, federal tax, and state tax. Print the prompt shown below...
For this coding exercise, you need to create a new Java project in Eclipse and finish...
For this coding exercise, you need to create a new Java project in Eclipse and finish all the coding in Eclipse. Run and debug your Eclipse project to make sure it works. Then you can just copy and paste the java source code of each file from Eclipse into the answer area of the corresponding box below. The boxes will expand once you paste your code into them, so don’t worry about how it looks J All data members are...
JAVA LANGUAGE Create a new project named BankAccount in Eclipse. Then, implement the following requirements. You...
JAVA LANGUAGE Create a new project named BankAccount in Eclipse. Then, implement the following requirements. You need to create two classes, one for BankAccount (BankAccount.java) and the other for the tester (BankAccountTest.java). Make sure your program compile without errors before submitting. Submit .java files to eCampus by the end of next Thursday, 10/15/2020. Part 1: Create the BankAccount class (template is attached after the project description) in the project. You must add the following to the BankAccount class: An instance...
In Java: 1) Create a new eclipse project. 2) Create a basic SWING window 3) Create...
In Java: 1) Create a new eclipse project. 2) Create a basic SWING window 3) Create a Label Called Name 4) Create a Text Field for entering the name 5) Create a Text Field for entering and email 5) Create a text area to push the results to 6) Create two buttons, one that says submit and the other that says clear. 7) When the user enters their name and email, they should press submit and see the Text area...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java,...
GETTING STARTED Create an Eclipse Java project containing package “bubble”. Import the 3 starter files BubbleSorter.java, BubbleSortTestCaseMaker.java, and Statistician.java. PART 1: Implementing BubbleSorter Implement a very simple BubbleSorter class that records how many array visits and how many swaps are performed. Look at the starter file before reading on. This class has an instance variable called “a”. Its type is int[]. This is the array that will be bubble-sorted in place. Usually a single letter is a bad variable name,...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT AND REQUIREMENTS: You will implement in Java the WIFI troubleshooting program you created in Assignment 1 with the following additional conditions: At the end of the program, you will ask if the client wants to purchase a regular router for $50 or a super high-speed router for $200, adding the cost to the total amount the client needs to pay. Once your program calculates...
open up a new Java project on Eclipse named Review and create a new class called...
open up a new Java project on Eclipse named Review and create a new class called Review.java. Copy and paste the below starter code into your file: /** * @author * @author * CIS 36B */ //write your two import statements here public class Review {        public static void main(String[] args) { //don't forget IOException         File infile = new File("scores.txt");         //declare scores array         //Use a for or while loop to read in...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week...
Create a new Java Project named “Packages” from within Eclipse. Following the example in the Week 6 lecture, create six packages: Main, add, subtract, multiply, divide, and modulo. ALL of these packages should be within the “src” folder in the Eclipse package Explorer. ALL of the packages should be on the same “level” in the file hierarchy. In the “Main” package, create the “Lab04.java” file and add the needed boilerplate (“Hello, World!” style) code to create the main method for...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include...
Create a new Java project called 1410_Recursion. Add a package recursion and a class Recursion. Include the following three static methods described below. However, don't implement them right away. Instead, start by returning the default value followed by a // TODO comment. public static int sumOfDigits(int n) This method returns the sum of all the digits. sumOfDigits(-34) -> 7 sumOfDigits(1038) -> 12 public static int countSmiles(char[] letters, int index) This method counts the number of colons followed by a closing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT