Question
Phase 2: Medicines Next, implement the related classes Medicine and MedicineList . The Medicine class should have: Two instance variables: the medicines name (a String
Phase 2: Medicines
Next, implement the related classes Medicine and MedicineList.
The Medicine class should have:
Two instance variables: the medicines name (a String), and a list of the chemicals that it contains (a ChemicalList).
A constructor Medicine(String, Chemical[]) which specifies the name of the medicine, and all of the chemicals that it contains. This should be the only constructor. The array of chemicals will be a full array, not a partially full array. You will have to use this Chemical[] to create the necessary ChemicalList.
A standard toString method that returns only the name of the medicine.
A special String toStringWithIngredients() method which will add a list of the chemicals in this medicine, after the name, surrounded by parentheses. See the sample output below.
The MedicineList class should have:
Instance variables that will allow a list of up to 100 Medicine objects to be stored. Use a simple partially-full array to do this. You may assume that the list will never become full no error checking is needed.
A constructor with no parameters that creates an empty list.
An instance method void addMedicine(Medicine) which will add a medicine to the list.
A standard toString method which will return the names of the medicines, separated by commas. There should be no comma after the last medicine. See the sample output below for an example.
Note that this class, so far, is almost identical to the ChemicalList class. You might think that there should be some easy way to create them without so much identical code. There is. We will cover it later in the course.
Modify the Chemical class as follows:
Add an instance variable so that each chemical will keep a list of the medicines that contain it (a MedicineList).
Make appropriate modifications to the constructor.
Add an instance method void addMedicine(Medicine) which will add a medicine to the list of medicines that contain this chemical. This method should be called from the constructor in the Medicine class.
Modify the toStringExtended method to add one more line that lists the medicines that have this chemical as an ingredient. See the sample output below for an example.
You can test your classes using the supplied test program TestPhase2.java. You should get the output shown below.
Chemicals
---------
aaratine
interacts with boricium
ingredient in MiracleCure,Hydramax,TastyPill
boricium
interacts with aaratine,eekamouse
ingredient in ExxoPlexx
catboxite
interacts with drystophan,eekamouse
ingredient in ExxoPlexx
drystophan
interacts with catboxite
ingredient in Hydramax,TastyPill
eekamouse
interacts with boricium,faroutman,catboxite
ingredient in MiracleCure
faroutman
interacts with eekamouse
ingredient in Hydramax,Buyalot
gooeygood
interacts with
ingredient in TastyPill
Medicines
---------
MiracleCure(aaratine,eekamouse)
ExxoPlexx(catboxite,boricium)
Hydramax(drystophan,aaratine,faroutman)
Buyalot(faroutman)
TastyPill(aaratine,drystophan,gooeygood)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started