bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 8, Problem 5E

Explanation of Solution

 Implementation of the derived class “Square”:

The implementation of the derived class “Square” is given below:

Create a derived class “Square” which is extends from the “DrawableShape”.

  • • Declare the “length” variable.
  • • Define the constructor.
    • ○ Call the “super” method.
    • ○ Set the values to the declared variables.
  • • The “getLength” method is used to return the “length”.
  • • The “setLength” method is used to set the length.
  • • The “getArea” method is used to return the area of the square.
  • • The “getPerimeter” method is used to return the perimeter of the square.
  • • Define the “draw” method.
    • ○ Declare the variables and calculate the “xLeft” and “yTop” values.
    • ○ Display the square in center position.
    • ○ The “*” character is used to display the square.
    • ○ Display the characters are taller in vertical position when compare to horizontal position.
  • • Definition of main method.
    • ○ Create an object for the class “Square”.
    • ○ Call the method “draw”.
    • ○ Call and display the length, area, perimeter and color of the square.

The implementation of the base class “DrawableShape” is given below:

  • • Declare the “x”, “y”, and “color” variables.
  • • Define the constructor.
    • ○ Set the values to the declared variables.
  • • The “getX” method is used to return the “x”.
  • • The “getY” method is used to return the “y”.
  • • The “getColor” method is used to return the “color”.
  • • The “move” method is used to calculate the “x” and “y” locations.
  • • The “draw” method will need to be defined in concrete subclasses.

 Program:

 Filename: “Square.java”

//definition of "Square" class

public class Square extends DrawableShape

{

    //declare the integer variable

    private int length;

    //definition of constructor

    public Square(int x, int y, int len, String c)

    {

        //call the method

        super(x, y, c);

        //set the value

        length = len;

    }

    //definition of "getLength" method

    public int getLength()

    {

        //return the length

        return length;

    }

    //definition of "setLength" method

    public void setLength(int len)

    {

        //set the value

        length = len;

    }

    //definition of "getArea" method

    public int getArea()

    {

        //return the area

        return length * length;

    }

    //definition of "getPerimeter" method

    public int getPerimeter()

    {

        //return the perimeter

        return 2 * length;

    }

    //definition of "draw" method

    public void draw()

    {

/*declare and calculate the value to the variable*/

        int xLeft = getX() - length/2;

        int yTop = getY() - length/2;

        // check the condition

        for(int i=1; i<yTop; i++)

        {

            //display the new line

            System.out.println();

        }

        // declare and set the null value

        String offset = "";

        // check the condition

        for(int j=1; j<xLeft; j++)

        {

            //set the value

            offset = offset + " ";

        }

        // declare and set the offset value

        String top = offset;

        // check the condition

        for(int j=0; j<length; j++)

        {

            //set the value

            top = top + "*";

        }

        //display the top line

        System...

Blurred answer
Students have asked these similar questions
Python - Need help! How do I have an input in turtle to display my name below the circle it draws and another input to display my age written below that? Code: import turtlebackground = "#FFFFFF" def draw_circle(radius, line_color, fill_color):    my_turtle.color(line_color)    my_turtle.fillcolor(fill_color)    my_turtle.begin_fill()    my_turtle.circle(radius)    my_turtle.end_fill() def move_turtle(x, y):    my_turtle.penup()    my_turtle.goto(x, y)    my_turtle.pendown()   turtle.done()
Need help fixing my python code! Images attached on the required modficications I dont know how to do. Simpler the better.Code: (in images)
Answer all of the questions with steps by step explanation to every question.

Chapter 8 Solutions

Java: An Introduction to Problem Solving and Programming plus MyProgrammingLab with Pearson eText -- Access Card Package (7th Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Memory Management Tutorial in Java | Java Stack vs Heap | Java Training | Edureka; Author: edureka!;https://www.youtube.com/watch?v=fM8yj93X80s;License: Standard YouTube License, CC-BY