Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Java Homework) Hello, having a hard time with this Java HW... Can Anyone help d

ID: 3877440 • Letter: #

Question

(Java Homework)

Hello, having a hard time with this Java HW... Can Anyone help direct me ???

Part 1: Classes and Overloaded Methods

In the provided part1 subdirectory, implement the following classes with these general requirements:

Each instance variable must be private and final.

Provide the appropriate "accessor"/"getter" methods for the instance variables. (see PartOneTestCases.java for names)

You may add methods beyond those required, but any such additional method must be private.

Automaton Warning: You are an intelligent person; you should not just mechanically apply these rules only to satisfy a style checker in order to complete the lab. Instead, consider the reasons for these rules, what violating them exposes to the user/client of the corresponding class, and what following them guarantees to you, the implementer. If you aren't sure, ask your neighbor, they might have thoughts of their own.

Classes

The following assumes your two-dimensional Point implementation from the previous lab. Copy your Point.java into the working directory.

Implement these classes.

Circle.java represents a circle given a Point center and a double radius.

Rectangle.java represents an axis-aligned rectangle given a Point top-left and a Point bottom-right.

Polygon.java represents a polygon given List<Point> points.

Note that as your implementation will be tested with PartOneTestCases, you may want to read exactly what the expected "get" method names are!

Overloaded Method

Define a Util class with three static perimeter methods. Each such method takes, as its single parameter, one of the classes defined in the previous step (i.e., there is one perimeter method that takes a Circle, one perimeter method that takes a Polygon, etc.) and returns the perimeter of the object (as a double).

You can assume for now that all constructors will create correct closed shapes.

A method (or methods) like perimeter is considered overloaded since there is a separate definition for different parameter types. This is also referred to as ad-hoc polymorphism because the (theoretically) single method is effectively defined to work with a small set of parameter types, not for all parameter types (i.e., the method takes "many forms", but only very specific forms are supported). You will likely hear "overloaded" much more often, but "ad-hoc polymorphism" sounds so much cooler (though, admittedly, it is a bit more intimidating).

Note that to call a static method like these, you can invoke: Util.perimeter(new Circle(new Point(0, 0), 1.0));

Deeper Understanding: How does the Java implementation know which version of perimeter to use when the method is invoked (not-so-great hint at this point, the compiler determines the implementation under this scenario)? If the answer is not apparent, then think about the different method invocations and speak with those around you. If the answer seems obvious, then hold on to that belief for the next few weeks to see if it continues to hold.

Methods in Action

Define a Bigger class with one static whichIsBigger method. This helper method will take three parameters a Circle, Rectangle, and Polygon and will return a double representing the largest perimeter of the three objects. You will use this method to determine which is the largest between:

a cirle centered at {1.0, 1.0} with radius of 2.0

a rectangle with the corners {-1.0, 2.0} and {1.0, -1.6}

a polygon defined by {0, 0}, {3, 1}, {1, 4}, and {-1, 4}

You may want to test your perimeter computations first (see next section).

Yes, of course you will want to test all of these operations. Add your tests to the provided PartOneTestCases.java file.

You are expected to write at least one test case for each perimeter and one test case for whichIsBigger. That means you must demo at least 8 tests (as there are some already there for implementation details).

Helper: here is an example of testing the perimeter for a polygon:

Part 2: Methods

Copy your files from part1 into part2 (you will not need the test cases file from part 1).

The definition of perimeter in the first part of this lab does not follow an object-oriented style. This part of the lab asks that you make a few modifications to improve the code (further such improvements can come with later material).Don't worry this part will be easier!

From Util.java, move each perimeter method into the appropriate class (as a non-static method, i.e., instance method) corresponding to perimeter's parameter. The goal is that each object "knows how" to compute its own perimeter. As such, perimeter will no longer need to take a parameter (it acts on this which refers to ... well, there is no universally accepted term for the target of this because computer scientists are not very good at naming things or at agreeing on the meaning of names/terms; you might hear "calling object", "current object", "context object", "target", "callee", "referent", "object on which the method was invoked" (that last one is real, and accurate, but is a sign of giving up on naming)). (You can remove Util.java once this is done).

Be sure to also copy over the Bigger class and revise it to use your new instance methods. Take a moment... do you like one style over the other?

Add tests to the provided PartTwoTestCases.java file. This is where you will also be able to see the changes of having the methods defined within each class (instance methods) versus the static methods this lab started with. Again, there should be an added test for each of the types of perimeters and a test of whichIsBigger

Here is PartOneTestCases :

Here is PartTwoTestCases :  

import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import org.junit.Test; public class PartOneTestCases { public static final double DELTA = 0.00001; @Test public void testCircleImplSpecifics() throws NoSuchMethodException { final List<String> expectedMethodNames = Arrays.asList( "getCenter", "getRadius"); final List<Class> expectedMethodReturns = Arrays.asList( Point.class, double.class); final List<Class[]> expectedMethodParameters = Arrays.asList( new Class[0], new Class[0]); verifyImplSpecifics(Circle.class, expectedMethodNames, expectedMethodReturns, expectedMethodParameters); } @Test public void testRectangleImplSpecifics() throws NoSuchMethodException { final List<String> expectedMethodNames = Arrays.asList( "getTopLeft", "getBottomRight"); final List<Class> expectedMethodReturns = Arrays.asList( Point.class, Point.class); final List<Class[]> expectedMethodParameters = Arrays.asList( new Class[0], new Class[0]); verifyImplSpecifics(Rectangle.class, expectedMethodNames, expectedMethodReturns, expectedMethodParameters); } @Test public void testPolygonImplSpecifics() throws NoSuchMethodException { final List<String> expectedMethodNames = Arrays.asList( "getPoints"); final List<Class> expectedMethodReturns = Arrays.asList( List.class); final List<Class[]> expectedMethodParameters = Arrays.asList( new Class[][] {new Class[0]}); verifyImplSpecifics(Polygon.class, expectedMethodNames, expectedMethodReturns, expectedMethodParameters); } @Test public void testUtilImplSpecifics() throws NoSuchMethodException { final List<String> expectedMethodNames = Arrays.asList( "perimeter", "perimeter", "perimeter"); final List<Class> expectedMethodReturns = Arrays.asList( double.class, double.class, double.class); final List<Class[]> expectedMethodParameters = Arrays.asList( new Class[] {Circle.class}, new Class[] {Polygon.class}, new Class[] {Rectangle.class}); verifyImplSpecifics(Util.class, expectedMethodNames, expectedMethodReturns, expectedMethodParameters); } private static void verifyImplSpecifics( final Class<?> clazz, final List<String> expectedMethodNames, final List<Class> expectedMethodReturns, final List<Class[]> expectedMethodParameters) throws NoSuchMethodException { assertEquals("Unexpected number of public fields", 0, Point.class.getFields().length); final List<Method> publicMethods = Arrays.stream( clazz.getDeclaredMethods()) .filter(m -> Modifier.isPublic(m.getModifiers())) .collect(Collectors.toList()); assertEquals("Unexpected number of public methods", expectedMethodNames.size(), publicMethods.size()); assertTrue("Invalid test configuration", expectedMethodNames.size() == expectedMethodReturns.size()); assertTrue("Invalid test configuration", expectedMethodNames.size() == expectedMethodParameters.size()); for (int i = 0; i < expectedMethodNames.size(); i++) { Method method = clazz.getDeclaredMethod(expectedMethodNames.get(i), expectedMethodParameters.get(i)); assertEquals(expectedMethodReturns.get(i), method.getReturnType()); } } }

Explanation / Answer

The objective of this assignment is to get you to understand the advantages of having an object-oriented approach to programming and how it can help in creating well-structred programs which are easy to modify and maintain. I shall try to explain in detail what the assignment requires of you. Feel free to reach out in the comments in case there are additional clarifications or doubts.

Part 1:

In part 1, you are required to implement a few classes, each with a few member functions and instance variables. There are a few general instructions to keep in mind while doing so:

Now they have already assumed that you have implemented Point.java satisfactorily. I shall assume the same, since I do not have access to the specifications for this class. You are supposed to use this Point.java class to implement the following classes:

final List<String> expectedMethodNames = Arrays.asList(

   "getCenter", "getRadius");

   

   final List<Class> expectedMethodReturns = Arrays.asList(

   Point.class, double.class);

   

   final List<Class[]> expectedMethodParameters = Arrays.asList(

   new Class[0], new Class[0]);

This indicates that the expected public functions to implemented are Point getCenter() and double getRadius(). Class[0] indicates that no method parameters are expected.

With these classes in place, you should now implement the Util class which is used to calculate the perimeter of the given shapes. The Util class will have three static functions, all named 'perimeter'. However, these functions all take different arguments, and hence, are an example of ad-hoc polymorphism. The different expected functions are

You will also need to implement a class named 'Bigger' which would have one static method whichIsBigger, defined like so:

After these classes are defined, you are supposed to write test cases to check if the functions you have implemented so far are, in fact, correct. To do so, you will have to insert functions in PartOneTestCases.java which check for the correctness of your functions. You have to write the following test case functions:

This would complete Part 1

Part 2

Now, for this part, what you would have to do is to move the perimeter functions implemented in the Util class to the respective shapes themselves. This is again, in better accordance with object oriented programming.

First of all, make a duplicate of the files you have created in the part1 subdirectory into the part2 folder.

Now, move each perimeter method defined in the Util class to the appropriate class. Note that the method parameters are no longer needed, since each class "knows" the required information about itself and therefore, "knows" about it's own perimeter. This is the idea behind moving the functions like so.

Also copy over the Bigger.java class and change the function implementation of whichIsBigger to use the changes implemented in the previous step, i.e using the member functions perimeter of the appropriate shape instead of invoking the perimeter functions from the Util.java class.

Now, add tests like in the first part. Make sure that in the tests you have implemented, you use the perimeter functions from the objects themselves rather than the Util.java class. You can delete the Util.java class after this.

I hope these instructions will help you tackle your assignment better! Please let me know if anything was unclear or confusing, and rate if you found it helpful!