Design of classes and methods in Java for Desklib online library
Verified
Added on 2023/06/12
|8
|1208
|324
AI Summary
This article discusses the design of classes and methods in Java for Desklib online library. It covers the use of abstraction, encapsulation, enums, and overloading. Examples of view, controller, and model methods are provided, along with testing and visibility of classes, methods, and fields.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
1 Table of contend 1.Design of classes according to OO paradigm……………………………………………..2 2.Design of a method………………………………………………………………………..4 2.1Example of a View Method…………………………………………………………..4 2.2Example of a Controller Method……………………………………………………..4 2.3Example of a Model Method…………………………………………………………5 3.Testing…………………………………………………………………………………......6 4.Visibility of Classes, Methods and Fields…………………………………………………7 5.References ………………………………………………………………………………...8
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
2 Section 1 : Design of classes according to OO paradigm 1)I have used interfaces for my class design for enabling abstraction. The controller is accessible to the outside world by my Controller Interface. All the requests that come from the user for any controller first goes to my Interface. Then, Main Controller implements this interface to carry out the main functionality. The main controller do not carries any functionality on its own. But passes requests to Charity controller or Watcher controller according to what is required. So a complete segregation of Control is there for each and every controller. Interface Screenshot Main Controller implementing Interface
3 2)In all object oriented languages abstraction and encapsulation plays a very crucial role. I have used enums in my project for Regions and animal species. Also Method overloading is used for overloading the methods of the base class. I have overloaded equals and hashcode method in the screenshot given below for easy comparision of Objects. Also constructors are used for Object initialization. Each instance variable in the model is of private scope and getteres and setters are used for their access for security and limited access. The instance variables of the object are accessible only through getters and setters or can be initialized by the constructor. Direct access to the variables is restricted. Also, Coupling and cohesion is used. In the diagram Animal class has an instance variable AnimalClassification. So every animal has a Classification which is other external ENUM. Enum,Abstraction,Encapsulation and Overloading used in Classes
4 Section 2: Design of a method Example of a View Method Here, in this screenshot,animalBySpeciesmethod takes a Scanner object for input as argument. This method prints the list of all the available species of animals and ask the user to select an Animal species. If the user enters any out of range choice, the program asks the user to try again and returns from the loop. If the entered choice is valid, this method calls the controller for the list of animals with the entered species. The controller returns a list of animals which is then printed to the user. animalBySpecies View Example of a Controller Method This methodgetAnimalBySpeciestakes species of animal as argument in the Charity Controller and creates a new empty set, so that no redundant animal is returned. Then, a loop iterates over every watcher, and an inner loop iterates over each record of the current watcher. If the species of the record matches the passed parameter then all the animals of the record are added to the set. This is done for each record of every watcher. Then, an arraylist of Animal is made from this set and it is sorted by the name of the animal which is returned to the calling function.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
5 GetAnimalBySpecies Charity Controller Example of a Model Method This method addRecord takes an animal Argument and adds that animal to the list of animals for this record object. It first checks that the current animals list for the record is null or not.If the list of animals is null. It initializes the list object to an arraylist and then adds the passed animal to the list. addAnimal Record Model
6 Section 3: Testing a)This method given in screenshot below intends to test the retieval of list of animals for a particular watcher sorted by species. This Junit method tests retrieveRecord function of the main Controller which takes two arguments. The first argument is the id of the watcher and the second argument is the choice that we want to sort by the species . In this method we are adding three records with different species of animals. Then we are changing the result returned by the controller call to an array and comparing these results with the actual results. We are using assertArrayEquals method of Junit for this. Test forretrieveRecordsBySpeciesMainControllerTest b)This second method is testing getAnimals method of the record class. Here we are creating a new Record object with a list of three animals passed as argument in constructor. Then we are using assertArrayEquals method to see that the method getAnimals is working correctly or not.
7 Test for getAnimalsRecordTest Section4:VisibilityofClasses,MethodsandFields a)All the classes are public as they are to be accessed outside the packages in which they are present. Each and every class has some methods and some instance variables. The instance variables of every class are private and they can be accessed only inside the class they are present. We have created getters and setters for every instance variable whose values needs to be changed. There are some instance variables whose value must not be altered. These variables can be initialized only inside the constructor of the class. For example id of the watcher. Id of the watcher is not mutable. Once a watcher is created, an id is passed in the constructor the and its id remains constant always. He is allowed to changes all his other information but he can't alter his id. So getters for such methods are provided but there are no setters for such methods. Also, All the methods in model are public as these methods are to be accessed outside the classes in which they are declared. There are some methods in some classes which are private as those methods are only to be used in those classes and are not needed outside those classes. So we have private Instance variables, some private and public methods and public classes.
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.