Design and Testing of Object Oriented Classes and Methods in Java

Verified

Added on  2023/06/12

|8
|1208
|97
AI Summary
This article discusses the design of classes and methods in Java using the object-oriented paradigm, abstraction, encapsulation, and testing. It provides examples of view, controller, and model methods, and explains the visibility of classes, methods, and fields. The article also includes references for further reading. Course code and college/university are not mentioned.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
1
Table of contend
1. Design of classes according to OO paradigm……………………………………………..2
2. Design of a method………………………………………………………………………..4
2.1 Example of a View Method…………………………………………………………..4
2.2 Example of a Controller Method……………………………………………………..4
2.3 Example of a Model Method…………………………………………………………5
3. Testing…………………………………………………………………………………......6
4. Visibility of Classes, Methods and Fields…………………………………………………7
5. References ………………………………………………………………………………...8
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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
Document Page
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
Document Page
4
Section 2: Design of a method
Example of a View Method
Here, in this screenshot, animalBySpecies method 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 method getAnimalBySpecies takes 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.
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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
Document Page
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 for retrieveRecordsBySpecies MainControllerTest
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.
Test for getAnimals RecordTest
Document Page
7
Section 4: Visibility of Classes, Methods and Fields
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.
Animal Class
Watcher Class
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
8
References
Belloc, H. (1967). On. Freeport, N.Y.: Books for Libraries Press.
Deitel, H. and Deitel, P. (2015). Java. Pearson Education UK.
Kousen, K. (2017). Modern Java recipes. Beijing: O'Reilly.
Oey, E. (1997). Java. [Hong Kong]: Periplus Editions.
Pandey, H. (2012). Java Programming. Pearson India.
Schildt, H. (2014). Java. San Francisco: McGraw-Hill Osborne.
Turner, P. (1999). Java. Hawthorn, Vic.: Lonely Planet.
Warburton, R. (2014). Java 8 Lambdas. Beijing: O'Reilly.
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]