Assignment #1 -
Reflection
Context
There will not be much context here. This first week
is meant to get up to speed, refresh the concepts of object orientation, and
learn a bit about reflection. The code to write therefore is fairly simple,
though not easy.
Assignment
Copy the classes given for this assignment to a new
project in order to have a testing domain. The classes are in a separate zip
file.
Create a class with a main method. This class needs
three methods on top of that:
The method disect(String className) accepts
a full classname (for example domain.Address) and prints all details it can
learn using reflection.
The method create(String className) accepts
a full classname, and creates a new object for that class, using reflection
classes to do so. You should not assume knowing what classnames will be passed,
so hard coding a classname is not allowed. The created object is returned. In
order to create an object, this method will use the constructor using the largest number
of parameters, and adds random data to that.
Example one is creating a new Address object. Address
uses ints and Strings in its constructors, so the create method invokes the
constructor having six parameters, using random data.
Example two is creating a Customer object. A Customer
also has an Address, so the create method calls itself again to get an Address
object.
Creating objects this way will inevitably have
exceptions, such as SecurityException and IllegalArgumentException. Catch those
and handle them decently.
The method call(Object target) accepts
an object, and then calls all public methods the object has. The results of
those calls are stored in an array the size of the number of public methods, which
is returned as a result. Use reflection all the time here.
Calling a method often will require passing
parameters. Those parameters are never asked from the user, but generated at
random using the create method.
Here there will be a lot of exceptions as well. Handle
them decently.
Sample output
Giving results:
public Customer extends Person implements IBuyer,
ISeller
Attributes:
private static int currentId
private final int customerId
private CreditRating creditRating
private List orders
Constructors:
public domain.Customer(String, LocalDate, Address)
Methods:
public void decreaseeCreditRating()
public void increaseCreditRating()
public CreditRating getCreditRating()
public boolean buy(Product, ISeller, int)
Get Free Quote!
266 Experts Online