Question
---------------------------------------- Chemical.java public class Chemical { private String name; private ChemicalList interactions; public Chemical(String name) { this.name = name; this.interactions = new ChemicalList(); } public
----------------------------------------
Chemical.java
public class Chemical { private String name; private ChemicalList interactions;
public Chemical(String name) { this.name = name; this.interactions = new ChemicalList(); }
public void addInteraction(Chemical c) { interactions.addChemical(c); c.addInteraction(this); }
public String toString() { return name; }
public String toStringExtended() { return name + " \t interacts with " + interactions; }
}
----------------------------------------
ChemicalList.java
public class ChemicalList { private Chemical[] list; private int numChemicals; public ChemicalList() { list = new Chemical[100]; numChemicals = 0; }
public void addChemical(Chemical c) { list[numChemicals] = c; numChemicals++; }
public String toString() { String s = ""; if(numChemicals > 0) { s = list[0].toString(); for(int i = 1; i
----------------------------------
TestPhase2
public class TestPhase2 { public static void main(String[] args) { //Create test chemicals Chemical aar = new Chemical("aaratine"); Chemical bor = new Chemical("boricium"); Chemical cat = new Chemical("catboxite"); Chemical dry = new Chemical("drystophan"); Chemical eek = new Chemical("eekamouse"); Chemical far = new Chemical("faroutman"); Chemical goo = new Chemical("gooeygood"); //Create test interactions aar.addInteraction(bor); cat.addInteraction(dry); eek.addInteraction(bor); eek.addInteraction(far); cat.addInteraction(eek); //Create test medicines Medicine mir = new Medicine("MiracleCure",new Chemical[]{aar,eek}); Medicine exx = new Medicine("ExxoPlexx",new Chemical[]{cat,bor}); Medicine hyd = new Medicine("Hydramax",new Chemical[]{dry,aar,far}); Medicine buy = new Medicine("Buyalot",new Chemical[]{far}); Medicine tas = new Medicine("TastyPill",new Chemical[]{aar,dry,goo}); //Print out chemicals, now with medicines listed System.out.println(" Chemicals ---------"); System.out.println(aar.toStringExtended()); System.out.println(bor.toStringExtended()); System.out.println(cat.toStringExtended()); System.out.println(dry.toStringExtended()); System.out.println(eek.toStringExtended()); System.out.println(far.toStringExtended()); System.out.println(goo.toStringExtended());
//Print out medicines with ingredients System.out.println(" Medicines ---------"); System.out.println(mir.toStringWithIngredients()); System.out.println(exx.toStringWithIngredients()); System.out.println(hyd.toStringWithIngredients()); System.out.println(buy.toStringWithIngredients()); System.out.println(tas.toStringWithIngredients()); } }
Phase 2: Medicines Next, implement the related classes Medicine and MedicineList. The Medicine class should have: Two instance variables: the medicine's 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 addMedi cine (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 exampleStep 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