Question

Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor...

Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor which initializes the data items for a patient object using code within the constructor (use any data). A second constructor with parameters to pass all the data items into the object at the time of instantiation. Create a test main class which instantiates two objects. Instantiate the first object using the default constructor and the second object using the constructor with the parameters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Default constructor with no parameters:

    public Patient(){
        this.name="John";
        this.age=69;
        this.gender='M';
        this.contact="123456";
        this.ailment="Diabetes Mellitus";
    }

Constructor with parameters:

    public Patient(String name, int age, char gender, String contact, String ailment) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.contact = contact;
        this.ailment = ailment;
    }

Test class with main function:

class Test{
    public static void main(String[] args) {
        Patient p1 = new Patient();
        Patient p2 = new Patient("Bob",85,'M',"987654","Arthritis");
        System.out.println("Patient 1 is: "+p1);
        System.out.println("Patient 2 is: "+p2);
    }
}

Refer the screenshot below for better understanding of the code:

7 Patient.java X Patient.java >.. 1 public class Patient{ 2 //demo data fields to store relevant information 3 private String

Output of the above code:

D: >java Test Patient 1 is: { name=John, age=69, gender=M, contact=123456, ailment=Diabetes Mellitus} Patient 2 is:

  • In the default constructor which does not take any parameters, we define the values of fields as some sample data.
  • In the parameterized constructor, we receive all the valus as parameters, and set the corresponding values of the object created, to those values using the this keyword.
  • toString method is overridden to print meaningful information when printing the object.
  • In main method of Test class, we instantiate two oobjects of Patient class using both constuctors and then print their values. We can see the difference in output.

For good practices, create getter and setter methods for the private data fields in Patient class like below:

public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getAge() { re

Add a comment
Know the answer?
Add Answer to:
Problem 3: (15 pts) Modify the Patient class with two overloaded constructors: A new default constructor...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • for Java define a class for a triangle of any size: including two constructors 1) default...

    for Java define a class for a triangle of any size: including two constructors 1) default color 2) accepts 3 parameters (don't forget set and get methods) and include a method for the area of the triangle create a driver program that instantiates two triangles: 1) using the default color 2) using the second constructor sketch the UML diagram

  • LW: Class Constructors Objectives Write a default constructor for a class. Note that this const...

    LW: Class Constructors Objectives Write a default constructor for a class. Note that this constructor is used to build the object anytime an argument is not provided when it is defined. Write a parameterized constructor that takes arguments that are used to initialize the class’s member variables Labwork Download the code associated with this lab: ColorConstructor.zip Main.cpp Color.h Color.cpp Compile and run the code. The first line of output of the program should display nonsensical integers for the values of...

  • Sammy’s Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists....

    Sammy’s Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. Create a Rental class for the company. The class contains: Two public final static fields that hold the number of minutes in an hour and the hourly rental rate ($40) Four private fields that hold a contract number, number of hours for the rental, number of minutes over an hour, and the price. The contract number is stored as a String because Sammy plans...

  • 1. A default constructor takes the same number of parameters as the number of private data...

    1. A default constructor takes the same number of parameters as the number of private data members. Select one: True False 2. Select all that are true regarding passing an object to a function. Select one or more: a. A function cannot take multiple objects as parameters b. Passing by value creates a copy of the object c. Passing an object by pointer is not allowed d. Passing by reference allows the function to modify the object e. Passing by...

  • Overload a relational operator for the Job class: Assume that this operator is not overloaded for...

    Overload a relational operator for the Job class: Assume that this operator is not overloaded for the Salary class. a) Write how the function will be declared in your code. b) Write an external function definition (not inside the class). solve it in C++10 to solve this you nedd question number 1. Create a composition between the classes Job and Salary. Class Salary a. data member:    1. money b. constructors:    1. default constructor    2. user defined constructor with a parameter...

  • (project 4)Write a class for an airplane class with constructors, interface (mutators and accessors)and a test...

    (project 4)Write a class for an airplane class with constructors, interface (mutators and accessors)and a test driver (main). It should be able to change altitude (up,down) and speed. The default constructor will set the altitude to 0 and speed to zero and longitude and latitude to Boston, Massachusetts (42.3631 degrees N, 71.0064 degrees W). The overloaded constructor will set longitude and latitude to Louis Armstrong (29.9933 degrees N, 90.2581 degrees W) by passing that string to the Airplane object when...

  • STORAGE WARS - Create a class with the name and variables/attributes listed below. Create a default...

    STORAGE WARS - Create a class with the name and variables/attributes listed below. Create a default Constructor with no parameters that assigns default values for each variable. Then, create an overloaded Constructor (with one parameter for each variable). Finally, create a method that returns the total number of items in the storage container. NOTE: this method must not print anything. (50 points) Make sure to clearly label the variables/attributes, Constructors and Methods and to indicate which language you are answering...

  • please help me with this in C# language. Constructors The goal for this exercise is to...

    please help me with this in C# language. Constructors The goal for this exercise is to understand what constructors are, how to define them, and how to call them, including ‘default’ constructors, and including the use of overloading to provide multiple constructors. One of the advantages of having a clear separation between the public interface of an object and private internal implementation of an object is that once you've got the data in the object you can then ask the...

  • The Drive‑Rite Insurance Company provides automobile insurance policies for drivers. Design a single class diagram showing...

    The Drive‑Rite Insurance Company provides automobile insurance policies for drivers. Design a single class diagram showing the class, the application program, the relationship between the two, and multiplicity. Insert the completed class diagram into a Word document. Then write the pseudocode as described below. Be sure to follow the CSI 117 Style Criteria (Links to an external site.)Links to an external site. for naming conventions, class diagrams, pseudocode, keywords, and operators. a.      Create a PolicyHolder class that contains a policy number,...

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT