Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
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
Need Java method please. Thank you.
Need Java method please. Thank you.
3. Write two nested loops to generate the following output. (Note: There is one space between each number, and any extra line shown is intentional.) 12 10 8 6 18 15 12 24 20 30 2 3 3 6 48 12 5 10 15 20 6 12 18 24 30

Chapter 8 Solutions

Java: An Introduction to Problem Solving and Programming (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