Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

---------------------------------------- Chemical.java public class Chemical { private String name; private ChemicalList interactions; public Chemical(String name) { this.name = name; this.interactions = new ChemicalList(); } public

image text in transcribedimage text in transcribed

----------------------------------------

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 example

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

What is Centrifugation?

Answered: 1 week ago

Question

To find integral of ?a 2 - x 2

Answered: 1 week ago

Question

To find integral of e 3x sin4x

Answered: 1 week ago

Question

To find the integral of 3x/(x - 1)(x - 2)(x - 3)

Answered: 1 week ago

Question

What are Fatty acids?

Answered: 1 week ago