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
2) You have learned in class the major steps that occur when a laptop requests a webpage after connecting to a network. In this assignment, you will apply that knowledge to another scenario: opening and playing a YouTube video that resides in Google's data-center infrastructure. Explain, in as much detail as you can, all the steps involved from your device's initial connection to the home/university network, to DNS resolution, routing across multiple networks, reaching Google's servers, and finally receiving the video data. To support your explanation, use tools such as ipconfig, nslookup, and tracert on your own computer, as well as any online IP-lookup tools of your choice. For each stage, include relevant information such as IP addresses, MAC addresses, router hops, and any other details you can gather. You are not expected to find every piece of information, but be as comprehensive as possible based on what you have learned in class, and justify your reasoning with screenshots from…
Please no use of AI
help me with this project. provide what I should put on each slide (words, example images, etc); example Slide 1: Intro. here are the directions: Submit a report that includes the source code, compiled code, description of the algorithm(s) implemented, data structures used, implementation details including time complexity analysis, sample inputs/outputs, and a conclusion section.

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