Overview: Create Pet And Procedure Classes, Plus A Main Test ✓ Solved
Overview: Create Pet and Procedure classes, plus a main test
Overview: Create Pet and Procedure classes, plus a main test. Part 1 defines two Java classes in a project/package named <your last name>.bcs345.hwk.vet.business: Pet and Procedure. Pet has private members name (String), species (String), and gender (String); public constructors Pet() and Pet(String name, String species, String gender); public getters and setters for all fields; public void Read(Scanner s); public void Write(PrintStream ps); public String GetJSON(); and an overridden toString() method. Part 2 defines a Main class in a project/package named <your last name>.bcs345.hwk.vet.presentation that runs automated tests: in main, create unit tests for all getters/setters of both classes, exercise Read/Write and GetJSON and toString, and demonstrate that data written by Write can be read back by Read. Deliverables: a single ZIP containing two Eclipse projects: <lastname>.bcs345.hwk.vet.business and <lastname>.bcs345.hwk.vet.presentation. Each project should contain the appropriate package structure; example Pet.txt and Procedure.txt input files are provided (e.g., Pet.txt: Snickers Dog Male; Procedure.txt: Rabies Shot 25.50).
Part 1 – Class Pet: Define the Pet class in the business package. The class must declare private member variables for name, species, and gender (all Strings). Provide a default constructor that initializes these fields to reasonable defaults (e.g., empty strings) and a 3-parameter constructor that sets each field from its corresponding parameter. Implement public getters and setters for all fields. Implement the following methods with precise behavior:
- void Read(Scanner s): read the three member variable values from an open Scanner, with each value on its own line. Assume the Scanner is already open and positioned correctly.
- void Write(PrintStream ps): write the three member variable values to the given PrintStream, one per line, with no extra descriptive text (the output must be readable by Read).
- String GetJSON(): return a JSON-formatted string of the form { "name" : "value", "species" : "value", "gender" : "value" } with proper quoting for Strings and no trailing comma after the last pair.
- @Override String toString(): return a descriptive string containing the values of the member variables (e.g., "Name: Snickers; Species: dog; Gender: male").
Part 1 – Class Procedure: Define the Procedure class in the same business package. The class must declare private member variables for name (String) and price (double). Provide a default constructor and a two-parameter constructor that initializes both fields. Implement public getters and setters for all fields. Implement the following methods with precise behavior:
- void Write(PrintStream ps): write the values to the PrintStream, one per line, with no extra descriptive text.
- void Read(Scanner s): read the values from an open Scanner, one per line; assume correct input order and types.
- String GetJSON(): return a JSON-formatted string like { "name" : "Rabies Shot", "price" : 25.5 } with appropriate quoting for Strings and numeric formatting for doubles.
- @Override String toString(): return a descriptive string describing the procedure and its price (e.g., "Name: Rabies Shot; Price: $25.50").
Part 2 – Class Main (Automated Testing): Create a Main class in the presentation package. In main, implement an automated test sequence that exercises all non-private methods or behavior as described. Specifically, include tests that:
- Create Pet and Procedure instances using both constructors and verify field values via getters.
- Call Write to serialize objects to a file, then read the data back using Read to validate round-trip fidelity.
- Call GetJSON on both classes and verify the JSON strings conform to the required formats, including proper quoting for Strings and correct numeric formatting for doubles.
- Call toString on both classes and verify the descriptive output includes all member values.
Input and output formats are important: for Read/Write, the output must be strictly the data values on separate lines (no descriptive text). For GetJSON, ensure correct JSON syntax, including quotes around keys and string values, with a single comma between fields, and no trailing comma. The Pet and Procedure sample input files (Pet.txt and Procedure.txt) illustrate the expected data order and types. Example entries include “Snickers” for name, “Dog” for species, and “Male” for gender in Pet.txt, and “Rabies Shot” for name and 25.50 for price in Procedure.txt.
Deliverables and packaging: Provide a single ZIP file containing two Eclipse projects. The business project should be named <your last name>.bcs345.hwk.vet.business and contain a package named <your last name>.bcs345.hwk.vet.business with Pet.java and Procedure.java. The presentation project should be named <your last name>.bcs345.hwk.vet.presentation and contain a package named <your last name>.bcs345.hwk.vet.presentation with Main.java. Include sample Pet.txt and Procedure.txt input files and note the file formats above. Follow the given naming conventions exactly to ensure correct project structure and packaging during evaluation.
Implementation note: While this assignment can be completed with a standard unit testing framework (e.g., JUnit), the instructions require tests to be exercised within the main method of the presentation project. The tests should demonstrate that every getter/setter works for both classes and that serialization (Write/Read) is robust, including a round-trip read after write. This aligns with common practice in introductory OO design to validate object state through explicit testing steps and to verify JSON formatting and string representations.
References files and example data can be provided by the instructor; ensure your code reads and writes data in the order specified and handles input correctly. The project should compile cleanly in Eclipse with the proper package paths and should be capable of running the main method to execute the automated tests described above. For guidance on Java syntax, object-oriented design, and I/O basics, consult reputable Java resources and tutorials.
Paper For Above Instructions
In this paper, I will address the design and implementation plan for the Pet and Procedure classes, the main testing harness, and considerations for robust input/output handling, JSON formatting, and descriptive string representations.
1) Design rationale
The Pet and Procedure classes reflect a small, cohesive domain model for a veterinary system. Each class encapsulates its data with private fields and exposes public accessors for controlled interaction. This approach follows standard object-oriented design principles described in the Java tutorials and textbooks (Oracle, n.d.; Schildt, 2014). The separation of concerns—business logic in one package and the testing harness in another—facilitates clean builds and reuse in different contexts (Gamma et al., 1994).
2) Data modeling and encapsulation
Pet stores three pieces of information: name, species, and gender. All are Strings to keep parsing and formatting straightforward, especially for I/O and JSON generation. Procedure stores a name and a price (double) to demonstrate numeric formatting and numeric handling in JSON. The constructors provide sensible defaults and convenience creation, while getters and setters ensure encapsulation and future validation can be added without breaking external usage (Bloch, 2018).
3) I/O methods and JSON formatting
The Read method reads one value per line from a Scanner; the Write method writes one value per line to a PrintStream. This line-based format is robust for simple file I/O and easy to verify in tests. The JSON method returns properly quoted keys and string values, with numeric values left unquoted (as per JSON standards). The toString method returns a human-readable description useful for debugging and logs (Oracle, n.d.; JSON.org, n.d.).
4) Automated testing strategy in main
Because the assignment specifies testing within main, tests should instantiate objects using both constructors, verify field values via getters, exercise Read/Write with a temporary file, verify JSON formatting, and confirm toString outputs. A robust sequence includes deliberately writing data to a file, reading it back, and asserting that the read-back object matches the original state. This kind of round-trip validation is a practical approach to verify serialization code (Schildt, 2014; Martin, 2008).
5) Sample code structure
Pet.java would define fields, constructors, getters/setters, and the methods described. Procedure.java mirrors Pet but with a String name and double price. Main.java in the presentation package tests both classes. Below is a concise illustrative outline (for reference only; implement in full in your IDE):
package yourlastname.bcs345.hwk.vet.business;
public class Pet {
private String name;
private String species;
private String gender;
public Pet() { this("", "", ""); }
public Pet(String name, String species, String gender) {
this.name = name;
this.species = species;
this.gender = gender;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSpecies() { return species; }
public void setSpecies(String species) { this.species = species; }
public String getGender() { return gender; }
public void setGender(String gender) { this.gender = gender; }
public void Read(Scanner s) { / read name, species, gender on separate lines / }
public void Write(PrintStream ps) { / write fields on separate lines / }
public String GetJSON() { return "{\"name\" : \"" + name +
"\", \"species\" : \"" + species +
"\", \"gender\" : \"" + gender + "\"}"; }
@Override public String toString() { return "Name: " + name +
" Species: " + species +
" Gender: " + gender; }
}
package yourlastname.bcs345.hwk.vet.business;
public class Procedure {
private String name;
private double price;
public Procedure() { this("", 0.0); }
public Procedure(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public void Read(Scanner s) { / read name and price on separate lines / }
public void Write(PrintStream ps) { / write fields on separate lines / }
public String GetJSON() { return "{\"name\" : \"" + name +
"\", \"price\" : " + price + "}"; }
@Override public String toString() { return "Name: " + name +
" Price: $" + String.format("%.2f", price); }
}
6) Expected outcomes and evaluation
Successful completion should yield two functioning classes with fully implemented methods as specified, plus a main-driven suite of tests that exercises every method. The program should build cleanly in Eclipse with the given package structure, and the automated tests should demonstrate correct serialization (Write and Read) as well as correct JSON generation and string representations. Clear, readable code and robust handling of I/O boundaries are important for maintainability and future extension (Schildt, 2014; Bloch, 2018).
7) References
- Oracle. The Java Tutorials: Classes. https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html (n.d.).
- Oracle. Java I/O Tutorial: Reading and Writing with PrintStream and Scanner (n.d.).
- JSON.org. JSON Data Interchange Format (n.d.).
- Bloch, J. Effective Java (3rd ed.). Addison-Wesley, 2018.
- Sierra, K., Bates, B. Head First Java. O’Reilly Media, 2003/2005 edition.
- Schildt, H. Java: The Complete Reference (11th ed.). McGraw-Hill, 2014.
- Gamma, E., Helm, R., Johnson, J., Vlissides, J. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1994.
- Martin, R. C. Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall, 2008.
- Oracle. Java Platform, Standard Edition Documentation. https://docs.oracle.com/javase/8/docs/ (n.d.).
- Freeman, S., Robson, E. Java for Beginners: A Practical Guide (example reference for OO design and practice). (n.d.).
Appendix: Sample input formats
Pet.txt example format:
Snickers
Dog
Male
Procedure.txt example format:
Rabies Shot
25.50