Hello for an Java exercise i have to do the following: Write an inheritance hier
ID: 672341 • Letter: H
Question
Hello for an Java exercise i have to do the following:
Write an inheritance hierarchy for the classes Quadrilateral, Trapezoid,
Parallelogram, Rectangle, and Square and then code it up. Create and use a
Point class that has instance variables: x-coordinate and y-coordinate (each
quadrilateral will have 4 of these, declared in the Quadrilateral class as
protected).
Write one or more constructors, get and set methods for each class, and
another method that computes the area of the object of each class (except for
Point and Quadrilateral). For simplicity require that one side of any
quadrilateral lie along the x-axis (and any other figure with parallel sides will
have a base lying along the x-axis); reject any input that fails to meet these
conditions. Test all of these methods.
My main class:
Point code:
My Quad Class:
My Parallelogram Class:
My Trap Class:
My Rectangle Class:
My Square Class:
My question is what changes do i need to make or alter in my code so far to make it as close to the assignment description as possible. I think im close but would love a fresh look! Thank you for your time!!!!
Explanation / Answer
One of the main concern regarding the program is the Access Specifier which has been used in fornt of the class.
This is to be kept in the mind that only one class can be declared as public.It is in the adherence to the Oracle Standards.So only our main class should be public.
Now,coming down to the main method we have defined the 5 objects of different classes which will call the constructors of their own class.
1) The quadrilateral function is working fine and there is no problem with it.
2)Trapezoid class is also also working fine and doing its job.
3) For the Parallelogram class we can see that the constructor is being called first,and then after that the the super function is called.
The main issue with this getWidth() function is that it is calling the getLengthSideA() method directly.We need to use the super keyword to call the method of the upper class.SO the syntax of the class should be like:-
super.getLengthSideA();
Now coming to getArea() method on the same class,the getHeight() has been used which is again the method of the superclass and hence we have to use the super keyowrd to get the height value.
We can create the getter method as similar to the one which is used to calculate the Length of side.SO our peice of the code should be like:
public double getHeight()
{
return super.getHeight();
}
4) The rectangle class has declared the method getArea(),so we need to put just the super() inside the method and nothing else.
Rest of all is working properly.
5) For Square class in the getSide() method the called function should be like super.getWidth().
Check whether the points provided clear the problems in the programs above.Also we should be clear in mind that there should not be multiple inheritance in java.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.