Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java program that Has an object class named RegOct that is a concrete subclass of the Shape class from attached chapter 4 attachments that represents

Java program that  Has an object class named RegOct that is a concrete subclass of the Shape class from attached chapter 4 attachments that represents an Regular Octagon. The class must include appropriate implementations of the required abstract methods.  The class must override toString() in a descriptive fashion, displaying any instance variables and the area, each labeled unambiguously. The string must easily fit on one line of the console. Add constructors/accessors/mutators for all instance variables.  Retain the default Comparable logic for Shape and RegOct. 6. Performs the following tasks in the main() of RegOct: a. Declare and instantiate an instance of an RegOct array and insert at least 20 of your octagon instances, each constructed with a random (double) dimension. b. Display the contents of the array after the octagons have been inserted, one instance per line. Precede the output with a descriptive heading. c. Sort the octagons in place in ascending order using a static method you write named Sort that takes a Comparable array as its lone parameter. (More details below.) d. Display the sorted array contents, one instance per line, with a descriptive heading and whitespace separation from the unsorted group. Your sort algorithm can be any standard sort that is neither Bubble Sort nor a Bogosort.

Put both classes in a package.

copy the Shape class from attached chapter 4 attachments and put it in that package

imageimageimage

126 chapter 4 inheritance figure 4.11 1 public class Shape A possible Shape class 2 { 3 public double area( ) 4 { 5 6 7 } return -1; } Too many instanceof opera- tors is a symptom of poor object- oriented design. This suggests a class for Shape, as shown in Figure 4.11. Once we have the Shape class, we can provide others, as shown in Figure 4.12. These classes also include a perimeter method. The code in Figure 4.12, with classes that extend the simple Shape class in Figure 4.11 that returns -1 for area, can now be used polymorphically, as shown in Figure 4.13. A huge benefit of this design is that we can add a new class to the hierar- chy without disturbing implementations. For instance, suppose we want to add triangles into the mix. All we need to do is have Triangle extend Shape, override area appropriately, and now Triangle objects can be included in any Shape[] object. Observe that this involves the following: NO CHANGES to the Shape class NO CHANGES to the Circle, Rectangle, or other existing classes NO CHANGES to the total Area method making it difficult to break existing code in the process of adding new code. Notice also the lack of any instanceof tests, which is typical of good polymorphic code. 4.2.1 abstract methods and classes Although the code in the previous example works, improvements are possi- ble in the Shape class written in Figure 4.11. Notice that the Shape class itself, and the area method in particular, are placeholders: The Shape's area method is never intended to be called directly. It is there so that the compiler and run-time system can conspire to use dynamic dispatch and call an appropriate area method. In fact, examining main, we see that Shape objects themselves are not supposed to be created either. The class exists simply as a common superclass for the others. 5 5. Declaring a private Shape constructor DOES NOT solve the second problem: The constructor is needed by the subclasses.

Step by Step Solution

3.33 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

Heres a Java program that includes the required classes and tasks as described java import javautilArrays Shape class abstract class Shape implements ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

Under what conditions are two qualitative variables independent?

Answered: 1 week ago