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
  • 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...

  • 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...

  • 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...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Lab 2 (ADTs) 1) LockADT – Show the interface and all abstract methods LockDataStructureClass – Show the following methods: default constructor, overloaded constructor, copy constructor, setX, setY, s...

    Lab 2 (ADTs) 1) LockADT – Show the interface and all abstract methods LockDataStructureClass – Show the following methods: default constructor, overloaded constructor, copy constructor, setX, setY, setZ, alter (change the lock’s combination to the numbers passed) turn (use for loops to show the dial turning), close (locks the lock), attempt (tries to unlock the lock – calls turn( ), inquire (locked or unlocked), current (returns the number the dial is pointing to), toString LockClientDemoClass – You should have a...

  • a. Create a class named Lease with fields that hold an apartment tenant's name, apartment Number,...

    a. Create a class named Lease with fields that hold an apartment tenant's name, apartment Number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name. "XXX", the apartment number to O, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy...

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
ADVERTISEMENT