Based On The Class Diagram You Are Exposed To Produce Progra

Based On The Class Diagram You Are Exposed To Produceprogram The Fol

Based on the class diagram, you are exposed to produce/program the following codes: 1. Add setter/getter methods for 1. One attribute in Category class and 2. One attribute in Product class. The setter should change the attribute value and the getter should read the attribute value. 2. Modify the signature of the calcPrice method to take two inputs, productName as a string and unitCost as float. 3. Modify the signature for the method displayProduct in Product to include one input (productID) and return a boolean. 4. Modify return types for attribute imageFileName to be from a library class (File) rather than a string 5. Create a client class to include the Main method and then instance 3 objects in the main method [ one object from each class: Category, OrderDetail, and Product). 6. Rewrite the code for the first exam to include 3 different types of try catch-exceptions. Do not use: The general exception (Exception), a user-defined exception or throw exception. Make sure you use 3 different types that are relevant based on your code.

Paper For Above instruction

This project focuses on translating a given class diagram into an operational Java program, incorporating essential object-oriented programming principles such as encapsulation, method modification, and exception handling. The task involves creating classes with appropriately defined attributes, methods for interacting with these attributes, and demonstrating their use via a client class with a main method. Additionally, the assignment emphasizes exception handling by incorporating three distinct types of exceptions relevant to typical Java applications, excluding general, user-defined, or throw exceptions.

Class Design and Attribute Access

Initial steps involve defining the class structure based on the diagram, specifically focusing on the Category and Product classes. For the Category class, one attribute—say, 'categoryName'—will be provided with getter and setter methods. These methods should allow reading and updating the value of 'categoryName'. Similarly, in the Product class, an attribute—such as 'productPrice'—will be provided with corresponding getter and setter methods. These methods facilitate encapsulation by controlling access to the class attributes.

Method Signatures and Modifications

The calcPrice method is to be modified to accept two parameters: productName (String) and unitCost (float). This change allows dynamic calculation based on the product's name and its unit cost. For example:

public float calcPrice(String productName, float unitCost) {

// implementation

}

Similarly, the displayProduct method in the Product class needs to be revised to include an input parameter 'productID' (perhaps an int or String) and to return a boolean indicating success or failure. Its signature might look like:

public boolean displayProduct(String productID) {

// implementation

}

Changing Return Types for Image File Attribute

The attribute 'imageFileName' initially stored as a string will now use a library class, specifically java.io.File. This change enhances type safety and leverages Java's file management capabilities, with the getter and setter adjusted accordingly:

private File imageFileName;

public File getImageFileName() {

return imageFileName;

}

public void setImageFileName(File imageFileName) {

this.imageFileName = imageFileName;

}

Client Class with Main Method

A client class, perhaps named 'Main', is created with the main method. Inside it, three objects will be instantiated—one each from the Category, OrderDetail, and Product classes. This demonstrates object creation and basic interactions among objects as per the class design.

Exception Handling with Multiple Try-Catch Blocks

Finally, the code from a previous exam, possibly involving file operations, parsing, or data manipulation, will be rewritten to include three distinct try-catch blocks, each catching a specific and relevant exception type such as:

  • FileNotFoundException – when file operations fail to locate the file.
  • NumberFormatException – during parsing string to numeric types when input is invalid.
  • ArrayIndexOutOfBoundsException – when array access exceeds bounds.

These exceptions are chosen because they are specific, commonly encountered in such code segments, and avoid the restricted types (such as general Exception, user-defined, or throw). The implementation of each try-catch block will show how to handle each case appropriately, such as logging errors or informing users.

Conclusion

This exercise combines object-oriented design, method customization, proper encapsulation, and robust exception handling—forming a comprehensive Java programming task that aligns with advanced coding standards and practical considerations. Proper implementation of these features ensures a maintainable, safe, and functional codebase driven by the provided class diagram and project requirements.

References

  • Arnold, K., Gosling, J., & Holmes, D. (2005). The Java Programming Language. Addison-Wesley.
  • Deitel, P. J., & Deitel, H. M. (2014). Java How to Program. Pearson.
  • Horstmann, C. S., & Cornell, G. (2018). Core Java Volume I--Fundamentals. Prentice Hall.
  • Larman, C. (2004). Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development. Pearson.
  • Java SE Documentation. Oracle. https://docs.oracle.com/en/java/javase/
  • Bloch, J. (2018). Effective Java. Addison-Wesley.
  • Stroustrup, B. (2013). The C++ Programming Language (Note: for comparison in design). Addison-Wesley.
  • Evans, E. (2003). Domain-Driven Design: Tackling Complexity in the Heart of Software. Addison-Wesley.
  • Fowler, M. (2004). Refactoring: Improving the Design of Existing Code. Addison-Wesley.
  • Tayler, J. (2010). Introduction to Java Programming. McGraw-Hill Education.