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
Objective: The objective of this assignment is to gain practice with pen testing a live web application running on a remote server. The live web application is a known vulnerable web application called DVWA (Damn Vulnerable Web Application) with security settings set to low. The web app is running on an AWS EC2 (Elastic Compute Cloud) instance running Ubuntu 22. Note: The point of this assignment is to step it up a notch, we learnt about different web application vulnerabilities and applied that knowledge, now we are going to pen test and enumerable the vulnerabilities of a web app + the underlying infrastructure it is running on. Before you begin please find out what your IP address is and place it in this sheet so that I can track who is doing what: IP Addresses.docx . Tasks: 1- Start by connecting to the target, I did not install a TLS certificate on purpose that is why you are going to connect via http and not via https: http://3.99.221.134/dvwa/login.php 2- Broken Authentication:…
No AI solutions please
No AI solutions please

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