34.3k views
4 votes
Suppose there is an abstract Line class as given below. Suppose there are also three subclasses

(subtypes) of the Line class – SolidLine, DashedLine and DottedLine. Complete the code below for the
underlined sections only that demonstrates the use of polymorphism for these types. (10 pts.)
public abstract class Line {
private XYCoord coord1;
private XYCoord coord2;
// Alternate Constructor
public Line(XYCoord coord1, XYCoord coord2) { }
// Draw Method
public abstract void draw();
}
// Main (demonstrating polymorphism)
Scanner input = new Scanner(System.in);
// variable declaration
int selection; Line line;
// prompt user for line type
System.out.println("Enter 1 – solid line, 2 – dashed line, 3 – dotted line: ");
selection = input.nextInt();
switch(selection) {
case 1: _____________________________________; break;
case 2: _____________________________________; break;
case 3: _____________________________________; break;
}
// draws the selected line type
line.draw();
Create a complete code(with subclasses) with explanation in java.

User Alcolawl
by
7.8k points

1 Answer

5 votes

Final answer:

To demonstrate the use of polymorphism with the Line class and its subclasses, create objects of the subclasses and assign them to the Line class reference variable 'line'. Call the 'draw()' method on the 'line' object to draw the selected line type.

Step-by-step explanation:

To demonstrate the use of polymorphism with the Line class and its subclasses, we can create three subclasses: SolidLine, DashedLine, and DottedLine. In the code provided, we can complete the underlined sections as follows:

  1. line = new SolidLine(coord1, coord2);
  2. line = new DashedLine(coord1, coord2);
  3. line = new DottedLine(coord1, coord2);
  4. This allows us to create objects of different subclasses and assign them to the Line class reference variable 'line'. After the user selects the line type, we can call the 'draw()' method on the 'line' object to draw the selected line type.
User Rapt
by
8.4k points